Skip to content

Commit a19d49d

Browse files
authored
Merge pull request #1503 from session-foundation/fix/localize_count_arg_in_plurals
fix: localize count number argument in localized plurals
2 parents 7a91838 + f1836ca commit a19d49d

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

ts/components/conversation/composition/CharacterCount.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import styled from 'styled-components';
22
import { Constants } from '../../../session';
3-
import { Localizer } from '../../basic/Localizer';
43
import { useFeatureFlag } from '../../../state/ducks/types/releasedFeaturesReduxTypes';
54
import { SessionTooltip } from '../../SessionTooltip';
65
import { SessionIcon } from '../../icon';
@@ -12,6 +11,7 @@ import {
1211
import { formatNumber } from '../../../util/i18n/formatting/generics';
1312
import { useHasPro } from '../../../hooks/useHasPro';
1413
import { useIsProAvailable } from '../../../hooks/useIsProAvailable';
14+
import { localize } from '../../../localization/localeTools';
1515

1616
export type CharacterCountProps = {
1717
count: number;
@@ -68,13 +68,9 @@ export function CharacterCount({ count }: CharacterCountProps) {
6868
<SessionTooltip
6969
horizontalPosition="center"
7070
verticalPosition="bottom"
71-
content={
72-
pastLimit ? (
73-
<Localizer token="remainingCharactersOverTooltip" args={{ count: remaining * -1 }} />
74-
) : (
75-
<Localizer token="remainingCharactersTooltip" args={{ count: remaining }} />
76-
)
77-
}
71+
content={localize(
72+
pastLimit ? 'remainingCharactersOverTooltip' : 'remainingCharactersTooltip'
73+
).withArgs({ count: pastLimit ? remaining * -1 : remaining })}
7874
dataTestId="tooltip-character-count"
7975
>
8076
<StyledRemainingNumber pastLimit={pastLimit}>

ts/localization/localeTools.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ function deSanitizeHtmlTags(str: string, identifier: string): string {
350350
.replace(new RegExp(`${identifier}&gt;${identifier}`, 'g'), '>');
351351
}
352352

353+
const pluralKey = 'count' as const;
354+
353355
class LocalizedStringBuilder<T extends MergedLocalizerTokens> extends String {
354356
private readonly token: T;
355357
private args?: ArgsFromToken<T>;
@@ -437,8 +439,6 @@ class LocalizedStringBuilder<T extends MergedLocalizerTokens> extends String {
437439
}
438440

439441
private resolvePluralString(): string {
440-
const pluralKey = 'count' as const;
441-
442442
let num: number | string | undefined = this.args?.[pluralKey as keyof ArgsFromToken<T>];
443443

444444
if (num === undefined) {
@@ -494,17 +494,19 @@ class LocalizedStringBuilder<T extends MergedLocalizerTokens> extends String {
494494
}
495495
}
496496

497-
return pluralString.replaceAll('#', `${num}`);
497+
return pluralString;
498498
}
499499

500500
private formatStringWithArgs(str: string): string {
501501
/** Find and replace the dynamic variables in a localized string and substitute the variables with the provided values */
502502
return str.replace(/\{(\w+)\}/g, (match, arg: string) => {
503-
const matchedArg = this.args
504-
? this.args[arg as keyof ArgsFromToken<T>]?.toString()
505-
: undefined;
503+
const matchedArg = this.args ? this.args[arg as keyof ArgsFromToken<T>] : undefined;
504+
505+
if (arg === pluralKey && typeof matchedArg === 'number' && Number.isFinite(matchedArg)) {
506+
return new Intl.NumberFormat(this.crowdinLocale).format(matchedArg);
507+
}
506508

507-
return matchedArg ?? match;
509+
return matchedArg?.toString() ?? match;
508510
});
509511
}
510512
}

0 commit comments

Comments
 (0)