From d717b1522dec10527ddc96485111f3d922501be6 Mon Sep 17 00:00:00 2001 From: Ryan Miller Date: Mon, 19 Aug 2024 15:35:53 +1000 Subject: [PATCH] fix: plurals not triggering every second time --- ts/util/i18n.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/ts/util/i18n.ts b/ts/util/i18n.ts index fd5880a7d..1be483037 100644 --- a/ts/util/i18n.ts +++ b/ts/util/i18n.ts @@ -99,15 +99,6 @@ const timeLocaleMap = { export type Locale = keyof typeof timeLocaleMap; -const cardinalPluralRegex: Record = { - zero: /zero \[(.*?)\]/g, - one: /one \[(.*?)\]/g, - two: /two \[(.*?)\]/g, - few: /few \[(.*?)\]/g, - many: /many \[(.*?)\]/g, - other: /other \[(.*?)\]/g, -}; - function getPluralKey(string: PluralString): R { const match = /{(\w+), plural, one \[.+\] other \[.+\]}/g.exec(string); return (match?.[1] ?? undefined) as R; @@ -117,7 +108,17 @@ function getStringForCardinalRule( localizedString: string, cardinalRule: Intl.LDMLPluralRule ): string | undefined { - const match = cardinalPluralRegex[cardinalRule].exec(localizedString); + // TODO: investigate if this is the best way to handle regex like this + const cardinalPluralRegex: Record = { + zero: /zero \[(.*?)\]/g, + one: /one \[(.*?)\]/g, + two: /two \[(.*?)\]/g, + few: /few \[(.*?)\]/g, + many: /many \[(.*?)\]/g, + other: /other \[(.*?)\]/g, + }; + const regex = cardinalPluralRegex[cardinalRule]; + const match = regex.exec(localizedString); return match?.[1] ?? undefined; }