Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions ts/components/conversation/composition/CharacterCount.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import styled from 'styled-components';
import { Constants } from '../../../session';
import { Localizer } from '../../basic/Localizer';
import { useFeatureFlag } from '../../../state/ducks/types/releasedFeaturesReduxTypes';
import { SessionTooltip } from '../../SessionTooltip';
import { SessionIcon } from '../../icon';
Expand All @@ -12,6 +11,7 @@ import {
import { formatNumber } from '../../../util/i18n/formatting/generics';
import { useHasPro } from '../../../hooks/useHasPro';
import { useIsProAvailable } from '../../../hooks/useIsProAvailable';
import { localize } from '../../../localization/localeTools';

export type CharacterCountProps = {
count: number;
Expand Down Expand Up @@ -68,13 +68,9 @@ export function CharacterCount({ count }: CharacterCountProps) {
<SessionTooltip
horizontalPosition="center"
verticalPosition="bottom"
content={
pastLimit ? (
<Localizer token="remainingCharactersOverTooltip" args={{ count: remaining * -1 }} />
) : (
<Localizer token="remainingCharactersTooltip" args={{ count: remaining }} />
)
}
content={localize(
pastLimit ? 'remainingCharactersOverTooltip' : 'remainingCharactersTooltip'
).withArgs({ count: pastLimit ? remaining * -1 : remaining })}
dataTestId="tooltip-character-count"
>
<StyledRemainingNumber pastLimit={pastLimit}>
Expand Down
16 changes: 9 additions & 7 deletions ts/localization/localeTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ function deSanitizeHtmlTags(str: string, identifier: string): string {
.replace(new RegExp(`${identifier}&gt;${identifier}`, 'g'), '>');
}

const pluralKey = 'count' as const;

class LocalizedStringBuilder<T extends MergedLocalizerTokens> extends String {
private readonly token: T;
private args?: ArgsFromToken<T>;
Expand Down Expand Up @@ -437,8 +439,6 @@ class LocalizedStringBuilder<T extends MergedLocalizerTokens> extends String {
}

private resolvePluralString(): string {
const pluralKey = 'count' as const;

let num: number | string | undefined = this.args?.[pluralKey as keyof ArgsFromToken<T>];

if (num === undefined) {
Expand Down Expand Up @@ -494,17 +494,19 @@ class LocalizedStringBuilder<T extends MergedLocalizerTokens> extends String {
}
}

return pluralString.replaceAll('#', `${num}`);
return pluralString;
}

private formatStringWithArgs(str: string): string {
/** Find and replace the dynamic variables in a localized string and substitute the variables with the provided values */
return str.replace(/\{(\w+)\}/g, (match, arg: string) => {
const matchedArg = this.args
? this.args[arg as keyof ArgsFromToken<T>]?.toString()
: undefined;
const matchedArg = this.args ? this.args[arg as keyof ArgsFromToken<T>] : undefined;

if (arg === pluralKey && typeof matchedArg === 'number' && Number.isFinite(matchedArg)) {
return new Intl.NumberFormat(this.crowdinLocale).format(matchedArg);
}

return matchedArg ?? match;
return matchedArg?.toString() ?? match;
});
}
}
Expand Down