Skip to content

Commit 233790a

Browse files
authored
Merge pull request Expensify#66154 from callstack-internal/performance/translation-improvement
fix: improve performance for translation
2 parents 818e4a6 + 35e6a98 commit 233790a

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

src/libs/Localize/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Onyx.connect({
2626
// Note: This has to be initialized inside a function and not at the top level of the file, because Intl is polyfilled,
2727
// and if React Native executes this code upon import, then the polyfill will not be available yet and it will barf
2828
let CONJUNCTION_LIST_FORMATS_FOR_LOCALES: Record<string, Intl.ListFormat>;
29+
2930
function init() {
3031
CONJUNCTION_LIST_FORMATS_FOR_LOCALES = Object.values(CONST.LOCALES).reduce((memo: Record<string, Intl.ListFormat>, locale) => {
3132
// eslint-disable-next-line no-param-reassign
@@ -34,6 +35,10 @@ function init() {
3435
}, {});
3536
}
3637

38+
// Memoized function to create PluralRules instances
39+
const createPluralRules = (locale: Locale): Intl.PluralRules => new Intl.PluralRules(locale);
40+
const memoizedCreatePluralRules = memoize(createPluralRules);
41+
3742
/**
3843
* Helper function to get the translated string for given
3944
* locale and phrase. This function is used to avoid
@@ -70,7 +75,7 @@ function getTranslatedPhrase<TKey extends TranslationPaths>(language: Locale, ph
7075
throw new Error(`Invalid plural form for '${phraseKey}'`);
7176
}
7277

73-
const pluralRule = new Intl.PluralRules(language).select(phraseObject.count);
78+
const pluralRule = memoizedCreatePluralRules(language).select(phraseObject.count);
7479

7580
const pluralResult = translateResult[pluralRule];
7681
if (pluralResult) {

0 commit comments

Comments
 (0)