|
| 1 | +import {setDefaultOptions} from 'date-fns'; |
| 2 | +import type {Locale as DateUtilsLocale} from 'date-fns'; |
| 3 | +import Onyx from 'react-native-onyx'; |
| 4 | +import extractModuleDefaultExport from '@libs/extractModuleDefaultExport'; |
| 5 | +import {LOCALES} from '@src/CONST/LOCALES'; |
| 6 | +import type {Locale} from '@src/CONST/LOCALES'; |
| 7 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 8 | +import type DynamicModule from '@src/types/utils/DynamicModule'; |
| 9 | +import type de from './de'; |
| 10 | +import type en from './en'; |
| 11 | +import type es from './es'; |
| 12 | +import flattenObject from './flattenObject'; |
| 13 | +import type fr from './fr'; |
| 14 | +import type it from './it'; |
| 15 | +import type ja from './ja'; |
| 16 | +import type nl from './nl'; |
| 17 | +import type pl from './pl'; |
| 18 | +import type ptBR from './pt-BR'; |
| 19 | +import type {FlatTranslationsObject, TranslationPaths} from './types'; |
| 20 | +import type zhHans from './zh-hans'; |
| 21 | + |
| 22 | +// This function was added here to avoid circular dependencies |
| 23 | +function setAreTranslationsLoading(areTranslationsLoading: boolean) { |
| 24 | + // eslint-disable-next-line rulesdir/prefer-actions-set-data |
| 25 | + Onyx.set(ONYXKEYS.ARE_TRANSLATIONS_LOADING, areTranslationsLoading); |
| 26 | +} |
| 27 | + |
| 28 | +class IntlStore { |
| 29 | + private static currentLocale: Locale | undefined = undefined; |
| 30 | + |
| 31 | + /** |
| 32 | + * Cache for translations |
| 33 | + */ |
| 34 | + private static cache = new Map<Locale, FlatTranslationsObject>(); |
| 35 | + |
| 36 | + /** |
| 37 | + * Cache for localized date-fns |
| 38 | + * @private |
| 39 | + */ |
| 40 | + private static dateUtilsCache = new Map<Locale, DateUtilsLocale>(); |
| 41 | + |
| 42 | + /** |
| 43 | + * Set of loaders for each locale. |
| 44 | + * Note that this can't be trivially DRYed up because dynamic imports must use string literals in metro: https://github.com/facebook/metro/issues/52 |
| 45 | + */ |
| 46 | + private static loaders: Record<Locale, () => Promise<[void, void]>> = { |
| 47 | + [LOCALES.DE]: () => |
| 48 | + this.cache.has(LOCALES.DE) |
| 49 | + ? Promise.all([Promise.resolve(), Promise.resolve()]) |
| 50 | + : Promise.all([ |
| 51 | + import('./de').then((module: DynamicModule<typeof de>) => { |
| 52 | + this.cache.set(LOCALES.DE, flattenObject(extractModuleDefaultExport(module))); |
| 53 | + }), |
| 54 | + import('date-fns/locale/de').then((module) => { |
| 55 | + this.dateUtilsCache.set(LOCALES.DE, module.de); |
| 56 | + }), |
| 57 | + ]), |
| 58 | + [LOCALES.EN]: () => |
| 59 | + this.cache.has(LOCALES.EN) |
| 60 | + ? Promise.all([Promise.resolve(), Promise.resolve()]) |
| 61 | + : Promise.all([ |
| 62 | + import('./en').then((module: DynamicModule<typeof en>) => { |
| 63 | + this.cache.set(LOCALES.EN, flattenObject(extractModuleDefaultExport(module))); |
| 64 | + }), |
| 65 | + import('date-fns/locale/en-GB').then((module) => { |
| 66 | + this.dateUtilsCache.set(LOCALES.EN, module.enGB); |
| 67 | + }), |
| 68 | + ]), |
| 69 | + [LOCALES.ES]: () => |
| 70 | + this.cache.has(LOCALES.ES) |
| 71 | + ? Promise.all([Promise.resolve(), Promise.resolve()]) |
| 72 | + : Promise.all([ |
| 73 | + import('./es').then((module: DynamicModule<typeof es>) => { |
| 74 | + this.cache.set(LOCALES.ES, flattenObject(extractModuleDefaultExport(module))); |
| 75 | + }), |
| 76 | + import('date-fns/locale/es').then((module) => { |
| 77 | + this.dateUtilsCache.set(LOCALES.ES, module.es); |
| 78 | + }), |
| 79 | + ]), |
| 80 | + [LOCALES.FR]: () => |
| 81 | + this.cache.has(LOCALES.FR) |
| 82 | + ? Promise.all([Promise.resolve(), Promise.resolve()]) |
| 83 | + : Promise.all([ |
| 84 | + import('./fr').then((module: DynamicModule<typeof fr>) => { |
| 85 | + this.cache.set(LOCALES.FR, flattenObject(extractModuleDefaultExport(module))); |
| 86 | + }), |
| 87 | + import('date-fns/locale/fr').then((module) => { |
| 88 | + this.dateUtilsCache.set(LOCALES.FR, module.fr); |
| 89 | + }), |
| 90 | + ]), |
| 91 | + [LOCALES.IT]: () => |
| 92 | + this.cache.has(LOCALES.IT) |
| 93 | + ? Promise.all([Promise.resolve(), Promise.resolve()]) |
| 94 | + : Promise.all([ |
| 95 | + import('./it').then((module: DynamicModule<typeof it>) => { |
| 96 | + this.cache.set(LOCALES.IT, flattenObject(extractModuleDefaultExport(module))); |
| 97 | + }), |
| 98 | + import('date-fns/locale/it').then((module) => { |
| 99 | + this.dateUtilsCache.set(LOCALES.IT, module.it); |
| 100 | + }), |
| 101 | + ]), |
| 102 | + [LOCALES.JA]: () => |
| 103 | + this.cache.has(LOCALES.JA) |
| 104 | + ? Promise.all([Promise.resolve(), Promise.resolve()]) |
| 105 | + : Promise.all([ |
| 106 | + import('./ja').then((module: DynamicModule<typeof ja>) => { |
| 107 | + this.cache.set(LOCALES.JA, flattenObject(extractModuleDefaultExport(module))); |
| 108 | + }), |
| 109 | + import('date-fns/locale/ja').then((module) => { |
| 110 | + this.dateUtilsCache.set(LOCALES.JA, module.ja); |
| 111 | + }), |
| 112 | + ]), |
| 113 | + [LOCALES.NL]: () => |
| 114 | + this.cache.has(LOCALES.NL) |
| 115 | + ? Promise.all([Promise.resolve(), Promise.resolve()]) |
| 116 | + : Promise.all([ |
| 117 | + import('./nl').then((module: DynamicModule<typeof nl>) => { |
| 118 | + this.cache.set(LOCALES.NL, flattenObject(extractModuleDefaultExport(module))); |
| 119 | + }), |
| 120 | + import('date-fns/locale/nl').then((module) => { |
| 121 | + this.dateUtilsCache.set(LOCALES.NL, module.nl); |
| 122 | + }), |
| 123 | + ]), |
| 124 | + [LOCALES.PL]: () => |
| 125 | + this.cache.has(LOCALES.PL) |
| 126 | + ? Promise.all([Promise.resolve(), Promise.resolve()]) |
| 127 | + : Promise.all([ |
| 128 | + import('./pl').then((module: DynamicModule<typeof pl>) => { |
| 129 | + this.cache.set(LOCALES.PL, flattenObject(extractModuleDefaultExport(module))); |
| 130 | + }), |
| 131 | + import('date-fns/locale/pl').then((module) => { |
| 132 | + this.dateUtilsCache.set(LOCALES.PL, module.pl); |
| 133 | + }), |
| 134 | + ]), |
| 135 | + [LOCALES.PT_BR]: () => |
| 136 | + this.cache.has(LOCALES.PT_BR) |
| 137 | + ? Promise.all([Promise.resolve(), Promise.resolve()]) |
| 138 | + : Promise.all([ |
| 139 | + import('./pt-BR').then((module: DynamicModule<typeof ptBR>) => { |
| 140 | + this.cache.set(LOCALES.PT_BR, flattenObject(extractModuleDefaultExport(module))); |
| 141 | + }), |
| 142 | + import('date-fns/locale/pt-BR').then((module) => { |
| 143 | + this.dateUtilsCache.set(LOCALES.PT_BR, module.ptBR); |
| 144 | + }), |
| 145 | + ]), |
| 146 | + [LOCALES.ZH_HANS]: () => |
| 147 | + this.cache.has(LOCALES.ZH_HANS) |
| 148 | + ? Promise.all([Promise.resolve(), Promise.resolve()]) |
| 149 | + : Promise.all([ |
| 150 | + import('./zh-hans').then((module: DynamicModule<typeof zhHans>) => { |
| 151 | + this.cache.set(LOCALES.ZH_HANS, flattenObject(extractModuleDefaultExport(module))); |
| 152 | + }), |
| 153 | + import('date-fns/locale/zh-CN').then((module) => { |
| 154 | + this.dateUtilsCache.set(LOCALES.ZH_HANS, module.zhCN); |
| 155 | + }), |
| 156 | + ]), |
| 157 | + }; |
| 158 | + |
| 159 | + public static getCurrentLocale() { |
| 160 | + return this.currentLocale; |
| 161 | + } |
| 162 | + |
| 163 | + public static load(locale: Locale) { |
| 164 | + if (this.currentLocale === locale) { |
| 165 | + return Promise.resolve(); |
| 166 | + } |
| 167 | + const loaderPromise = this.loaders[locale]; |
| 168 | + setAreTranslationsLoading(true); |
| 169 | + return loaderPromise() |
| 170 | + .then(() => { |
| 171 | + this.currentLocale = locale; |
| 172 | + // Set the default date-fns locale |
| 173 | + const dateUtilsLocale = this.dateUtilsCache.get(locale); |
| 174 | + if (dateUtilsLocale) { |
| 175 | + setDefaultOptions({locale: dateUtilsLocale}); |
| 176 | + } |
| 177 | + }) |
| 178 | + .then(() => { |
| 179 | + setAreTranslationsLoading(false); |
| 180 | + }); |
| 181 | + } |
| 182 | + |
| 183 | + public static get<TPath extends TranslationPaths>(key: TPath, locale?: Locale) { |
| 184 | + const localeToUse = locale && this.cache.has(locale) ? locale : this.currentLocale; |
| 185 | + if (!localeToUse) { |
| 186 | + return null; |
| 187 | + } |
| 188 | + const translations = this.cache.get(localeToUse); |
| 189 | + return translations?.[key] ?? null; |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +export default IntlStore; |
0 commit comments