fix: plurals and tag strips in app

pull/3206/head
Ryan Miller 8 months ago
parent c0d9410094
commit f7b21ab262

@ -1,4 +1,5 @@
import { import {
getJoinedGroupUpdateChangeStr,
getKickedGroupUpdateStr, getKickedGroupUpdateStr,
getLeftGroupUpdateChangeStr, getLeftGroupUpdateChangeStr,
} from '../../../../models/groupUpdate'; } from '../../../../models/groupUpdate';
@ -22,7 +23,7 @@ const ChangeItemJoined = (added: Array<string>): string => {
// this is not ideal, but also might not be changed as part of Strings but, // this is not ideal, but also might not be changed as part of Strings but,
// we return a string containing style tags (<b> etc) here, and a SessionHtmlRenderer is going // we return a string containing style tags (<b> etc) here, and a SessionHtmlRenderer is going
// to render them correctly. // to render them correctly.
return getLeftGroupUpdateChangeStr(added, groupName, false); return getJoinedGroupUpdateChangeStr(added, groupName, false);
}; };
const ChangeItemKicked = (kicked: Array<string>): string => { const ChangeItemKicked = (kicked: Array<string>): string => {

@ -1,5 +1,6 @@
import { getConversationController } from '../session/conversations'; import { getConversationController } from '../session/conversations';
import { UserUtils } from '../session/utils'; import { UserUtils } from '../session/utils';
import { getI18nFunction } from '../util/i18n';
// to remove after merge with groups // to remove after merge with groups
function usAndXOthers(arr: Array<string>) { function usAndXOthers(arr: Array<string>) {
@ -20,28 +21,31 @@ export function getKickedGroupUpdateStr(
const othersNames = others.map( const othersNames = others.map(
getConversationController().getContactProfileNameOrShortenedPubKey getConversationController().getContactProfileNameOrShortenedPubKey
); );
const getString = getI18nFunction(stripTags);
if (us) { if (us) {
switch (others.length) { switch (others.length) {
case 0: case 0:
return window.i18n('groupRemovedYou', { group_name: groupName }); return getString('groupRemovedYou', { group_name: groupName });
case 1: case 1:
return window.i18n('groupRemovedYouTwo', { other_name: othersNames[0] }); return getString('groupRemovedYouTwo', { other_name: othersNames[0] });
default: default:
return window.i18n('groupRemovedYouMultiple', { count: othersNames.length }); return getString('groupRemovedYouMultiple', { count: othersNames.length });
} }
} }
switch (others.length) { switch (others.length) {
case 0: case 0:
throw new Error('kicked without anyone in it.'); throw new Error('kicked without anyone in it.');
case 1: case 1:
return window.i18n('groupRemoved', { name: othersNames[0] }); return getString('groupRemoved', { name: othersNames[0] });
case 2: case 2:
return window.i18n('groupRemovedTwo', { return getString('groupRemovedTwo', {
name: othersNames[0], name: othersNames[0],
other_name: othersNames[1], other_name: othersNames[1],
}); });
default: default:
return window.i18n('groupRemovedMore', { return getString('groupRemovedMore', {
name: others[0], name: others[0],
count: othersNames.length - 1, count: othersNames.length - 1,
}); });
@ -55,13 +59,15 @@ export function getLeftGroupUpdateChangeStr(
) { ) {
const { others, us } = usAndXOthers(left); const { others, us } = usAndXOthers(left);
const getString = getI18nFunction(stripTags);
if (left.length !== 1) { if (left.length !== 1) {
throw new Error('left.length should never be more than 1'); throw new Error('left.length should never be more than 1');
} }
return us return us
? window.i18n('groupMemberYouLeft') ? getString('groupMemberYouLeft')
: window.i18n('groupMemberLeft', { : getString('groupMemberLeft', {
name: getConversationController().getContactProfileNameOrShortenedPubKey(others[0]), name: getConversationController().getContactProfileNameOrShortenedPubKey(others[0]),
}); });
} }
@ -75,28 +81,31 @@ export function getJoinedGroupUpdateChangeStr(
const othersNames = others.map( const othersNames = others.map(
getConversationController().getContactProfileNameOrShortenedPubKey getConversationController().getContactProfileNameOrShortenedPubKey
); );
const getString = getI18nFunction(stripTags);
if (us) { if (us) {
switch (others.length) { switch (others.length) {
case 0: case 0:
return window.i18n('groupMemberNew', { name: window.i18n('you') }); return getString('groupMemberNew', { name: window.i18n('you') });
case 1: case 1:
return window.i18n('groupMemberYouAndOtherNew', { other_name: othersNames[0] }); return getString('groupMemberYouAndOtherNew', { other_name: othersNames[0] });
default: default:
return window.i18n('groupMemberYouAndMoreNew', { count: othersNames.length }); return getString('groupMemberYouAndMoreNew', { count: othersNames.length });
} }
} }
switch (others.length) { switch (others.length) {
case 0: case 0:
throw new Error('joined without anyone in it.'); throw new Error('joined without anyone in it.');
case 1: case 1:
return window.i18n('groupMemberNew', { name: othersNames[0] }); return getString('groupMemberNew', { name: othersNames[0] });
case 2: case 2:
return window.i18n('groupMemberTwoNew', { return getString('groupMemberTwoNew', {
name: othersNames[0], name: othersNames[0],
other_name: othersNames[1], other_name: othersNames[1],
}); });
default: default:
return window.i18n('groupMemberMoreNew', { return getString('groupMemberMoreNew', {
name: others[0], name: others[0],
count: othersNames.length - 1, count: othersNames.length - 1,
}); });

@ -15,7 +15,9 @@ type ArgsRecord<T extends Token> = Record<DynamicArgs<Dictionary[T]>, DynamicArg
export type PluralKey = 'count'; 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<Dictionary, PluralString>;
/** The dynamic arguments in a localized string */ /** The dynamic arguments in a localized string */
type DynamicArgs<LocalizedString extends string> = type DynamicArgs<LocalizedString extends string> =

8
ts/window.d.ts vendored

@ -42,9 +42,13 @@ declare global {
* window.i18n('greeting', { name: 'Alice' }); * window.i18n('greeting', { name: 'Alice' });
* // => 'Hello, Alice!' * // => 'Hello, Alice!'
*/ */
i18n: <T extends LocalizerToken, R extends LocalizerDictionary[T]>( i18n: (<T extends LocalizerToken, R extends LocalizerDictionary[T]>(
...[token, args]: GetMessageArgs<T> ...[token, args]: GetMessageArgs<T>
) => R; ) => R) & {
stripped: <T extends LocalizerToken, R extends LocalizerDictionary[T]>(
...[token, args]: GetMessageArgs<T>
) => R;
};
getLocale: () => Locale; getLocale: () => Locale;
log: any; log: any;
sessionFeatureFlags: { sessionFeatureFlags: {

Loading…
Cancel
Save