Skip to content

Commit a301278

Browse files
committed
Allow registering dynamic translations
In some use cases, other i18n solutions manage translations (e.g. Tolgee). In these cases, it is desirable to resolve the translations dynamically
1 parent 2f7d23d commit a301278

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/translations/utils.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ export type TranslationsType = {
1717
minute: string
1818
}
1919

20-
let translationsPerLocale: Record<string, TranslationsType> = {}
20+
export type TranslationResolver = (
21+
locale: string | undefined
22+
) => TranslationsType
23+
24+
let translationsPerLocale: Record<
25+
string,
26+
TranslationsType | TranslationResolver
27+
> = {}
2128

2229
export function getTranslation<K extends keyof TranslationsType>(
2330
locale: string | undefined,
@@ -32,7 +39,10 @@ export function getTranslation<K extends keyof TranslationsType>(
3239
)
3340
return fallback || key
3441
}
35-
const translation = translationsPerLocale[l][key]
42+
const translation: TranslationsType[K] =
43+
typeof translationForLocale === 'function'
44+
? translationForLocale(locale)[key]
45+
: translationForLocale[key]
3646
if (!translation) {
3747
console.warn(
3848
`[react-native-paper-dates] The locale ${locale} is registered, but ${key} is missing`
@@ -43,7 +53,7 @@ export function getTranslation<K extends keyof TranslationsType>(
4353

4454
export function registerTranslation(
4555
locale: string,
46-
translations: TranslationsType
56+
translations: TranslationsType | TranslationResolver
4757
) {
4858
translationsPerLocale[locale] = translations
4959
}

0 commit comments

Comments
 (0)