From f7b21ab2629659707da19d240f663058ad30802a Mon Sep 17 00:00:00 2001 From: Ryan Miller Date: Mon, 19 Aug 2024 14:55:43 +1000 Subject: [PATCH] fix: plurals and tag strips in app --- .../message-item/GroupUpdateMessage.tsx | 3 +- ts/models/groupUpdate.ts | 37 ++++++++++++------- ts/types/Localizer.ts | 4 +- ts/window.d.ts | 8 +++- 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/ts/components/conversation/message/message-item/GroupUpdateMessage.tsx b/ts/components/conversation/message/message-item/GroupUpdateMessage.tsx index 5ed237ae8..10e557ec5 100644 --- a/ts/components/conversation/message/message-item/GroupUpdateMessage.tsx +++ b/ts/components/conversation/message/message-item/GroupUpdateMessage.tsx @@ -1,4 +1,5 @@ import { + getJoinedGroupUpdateChangeStr, getKickedGroupUpdateStr, getLeftGroupUpdateChangeStr, } from '../../../../models/groupUpdate'; @@ -22,7 +23,7 @@ const ChangeItemJoined = (added: Array): string => { // this is not ideal, but also might not be changed as part of Strings but, // we return a string containing style tags ( etc) here, and a SessionHtmlRenderer is going // to render them correctly. - return getLeftGroupUpdateChangeStr(added, groupName, false); + return getJoinedGroupUpdateChangeStr(added, groupName, false); }; const ChangeItemKicked = (kicked: Array): string => { diff --git a/ts/models/groupUpdate.ts b/ts/models/groupUpdate.ts index 168001634..ae5de957a 100644 --- a/ts/models/groupUpdate.ts +++ b/ts/models/groupUpdate.ts @@ -1,5 +1,6 @@ import { getConversationController } from '../session/conversations'; import { UserUtils } from '../session/utils'; +import { getI18nFunction } from '../util/i18n'; // to remove after merge with groups function usAndXOthers(arr: Array) { @@ -20,28 +21,31 @@ export function getKickedGroupUpdateStr( const othersNames = others.map( getConversationController().getContactProfileNameOrShortenedPubKey ); + + const getString = getI18nFunction(stripTags); + if (us) { switch (others.length) { case 0: - return window.i18n('groupRemovedYou', { group_name: groupName }); + return getString('groupRemovedYou', { group_name: groupName }); case 1: - return window.i18n('groupRemovedYouTwo', { other_name: othersNames[0] }); + return getString('groupRemovedYouTwo', { other_name: othersNames[0] }); default: - return window.i18n('groupRemovedYouMultiple', { count: othersNames.length }); + return getString('groupRemovedYouMultiple', { count: othersNames.length }); } } switch (others.length) { case 0: throw new Error('kicked without anyone in it.'); case 1: - return window.i18n('groupRemoved', { name: othersNames[0] }); + return getString('groupRemoved', { name: othersNames[0] }); case 2: - return window.i18n('groupRemovedTwo', { + return getString('groupRemovedTwo', { name: othersNames[0], other_name: othersNames[1], }); default: - return window.i18n('groupRemovedMore', { + return getString('groupRemovedMore', { name: others[0], count: othersNames.length - 1, }); @@ -55,13 +59,15 @@ export function getLeftGroupUpdateChangeStr( ) { const { others, us } = usAndXOthers(left); + const getString = getI18nFunction(stripTags); + if (left.length !== 1) { throw new Error('left.length should never be more than 1'); } return us - ? window.i18n('groupMemberYouLeft') - : window.i18n('groupMemberLeft', { + ? getString('groupMemberYouLeft') + : getString('groupMemberLeft', { name: getConversationController().getContactProfileNameOrShortenedPubKey(others[0]), }); } @@ -75,28 +81,31 @@ export function getJoinedGroupUpdateChangeStr( const othersNames = others.map( getConversationController().getContactProfileNameOrShortenedPubKey ); + + const getString = getI18nFunction(stripTags); + if (us) { switch (others.length) { case 0: - return window.i18n('groupMemberNew', { name: window.i18n('you') }); + return getString('groupMemberNew', { name: window.i18n('you') }); case 1: - return window.i18n('groupMemberYouAndOtherNew', { other_name: othersNames[0] }); + return getString('groupMemberYouAndOtherNew', { other_name: othersNames[0] }); default: - return window.i18n('groupMemberYouAndMoreNew', { count: othersNames.length }); + return getString('groupMemberYouAndMoreNew', { count: othersNames.length }); } } switch (others.length) { case 0: throw new Error('joined without anyone in it.'); case 1: - return window.i18n('groupMemberNew', { name: othersNames[0] }); + return getString('groupMemberNew', { name: othersNames[0] }); case 2: - return window.i18n('groupMemberTwoNew', { + return getString('groupMemberTwoNew', { name: othersNames[0], other_name: othersNames[1], }); default: - return window.i18n('groupMemberMoreNew', { + return getString('groupMemberMoreNew', { name: others[0], count: othersNames.length - 1, }); diff --git a/ts/types/Localizer.ts b/ts/types/Localizer.ts index 3b7e5277c..a31621fe0 100644 --- a/ts/types/Localizer.ts +++ b/ts/types/Localizer.ts @@ -15,7 +15,9 @@ type ArgsRecord = Record, DynamicArg export type PluralKey = 'count'; -export type PluralString = `{${string}, plural, one {${string}} other {${string}}}`; +export type PluralString = `{${string}, plural, one [${string}] other [${string}]}`; + +export type DictionaryWithoutPluralStrings = Omit; /** The dynamic arguments in a localized string */ type DynamicArgs = diff --git a/ts/window.d.ts b/ts/window.d.ts index 36701d6a6..6283d7d1c 100644 --- a/ts/window.d.ts +++ b/ts/window.d.ts @@ -42,9 +42,13 @@ declare global { * window.i18n('greeting', { name: 'Alice' }); * // => 'Hello, Alice!' */ - i18n: ( + i18n: (( ...[token, args]: GetMessageArgs - ) => R; + ) => R) & { + stripped: ( + ...[token, args]: GetMessageArgs + ) => R; + }; getLocale: () => Locale; log: any; sessionFeatureFlags: {