forked from pagopa/io-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n.ts
63 lines (53 loc) · 1.81 KB
/
i18n.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import I18n from "react-native-i18n";
// If the following import is missing, generate it by running:
// npm run generate:locales
import * as locales from "../locales/locales";
// Should the app fallback to English if user locale doesn't exists
// eslint-disable-next-line
I18n.fallbacks = true;
// Define the supported translations
// eslint-disable-next-line
I18n.translations = {
en: locales.localeEN,
it: locales.localeIT
};
export const translations = Object.keys(I18n.translations);
export const availableTranslations: ReadonlyArray<locales.Locales> = translations
.map(k => k as locales.Locales)
.sort();
export function setLocale(lang: locales.Locales) {
// eslint-disable-next-line
I18n.locale = lang;
}
type TranslateT = {
// allow unsafe translations only when a defaultValue gets passed
// allows the use of implicit pluralization of translations, use count as numeral variable
// how-to use pluralization explained here https://github.com/pagopa/io-app/pull/2366
(
scope: string,
options: { defaultValue: string; count?: number } & Omit<
I18n.TranslateOptions,
"defaultValue"
>
): string;
// or else, the lookup must be safe
(scope: locales.TranslationKeys, options?: I18n.TranslateOptions): string;
};
/**
* Replacement for the I18n type for making the
*
* We can't simply add our definition for "t" as it will be
* merged with the existing one (overloaded) keeping it unsafe by
* making it accept any key.
*/
interface TypedI18n {
readonly t: TranslateT;
readonly translate: TranslateT;
readonly locale: locales.Locales;
readonly currentLocale: () => locales.Locales;
readonly toNumber: typeof I18n.toNumber;
readonly toCurrency: typeof I18n.toCurrency;
readonly strftime: typeof I18n.strftime;
}
const TypedI18n = I18n as TypedI18n;
export default TypedI18n;