-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathi18nUtils.js
227 lines (214 loc) · 8.06 KB
/
i18nUtils.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import i18next from "i18next";
import { initReactI18next } from "react-i18next";
import ShopifyFormat from "@shopify/i18next-shopify";
import resourcesToBackend from "i18next-resources-to-backend";
import { match } from "@formatjs/intl-localematcher";
import { shouldPolyfill as shouldPolyfillLocale } from "@formatjs/intl-locale/should-polyfill";
import { shouldPolyfill as shouldPolyfillPluralRules } from "@formatjs/intl-pluralrules/should-polyfill";
import {
DEFAULT_LOCALE as DEFAULT_POLARIS_LOCALE,
SUPPORTED_LOCALES as SUPPORTED_POLARIS_LOCALES,
} from "@shopify/polaris";
/**
* The default locale for the app.
*/
const DEFAULT_APP_LOCALE = "en";
/**
* The supported locales for the app.
*
* These should correspond with the JSON files in the `locales` folder.
*
* @example
* en.json
* de.json
* fr.json
* @see Available Shopify Admin languages in the Shopify Help Center:
* https://help.shopify.com/en/manual/your-account/languages#available-languages
*/
const SUPPORTED_APP_LOCALES = ["en", "de", "fr"];
let _userLocale, _polarisTranslations;
/**
* Retrieves the user's locale from the `locale` request parameter and matches it to supported app locales.
*
* Returns the default app locale if the user locale is not supported.
*
* @see https://shopify.dev/docs/apps/best-practices/internationalization/getting-started#step-2-get-access-to-the-users-locale
*
* @returns {string} User locale
*/
export function getUserLocale() {
if (_userLocale) {
return _userLocale;
}
const url = new URL(window.location.href);
const locale = url.searchParams.get("locale") || DEFAULT_APP_LOCALE;
_userLocale = match([locale], SUPPORTED_APP_LOCALES, DEFAULT_APP_LOCALE);
return _userLocale;
}
/**
* Returns Polaris translations that correspond to the user locale.
*
* Returns Polaris translations for the default locale if the user locale is not supported.
*
* @see https://polaris.shopify.com/components/utilities/app-provider#using-translations
*
* @returns {TranslationDictionary} Polaris translations
*/
export function getPolarisTranslations() {
return _polarisTranslations;
}
/**
* @async
* Asynchronously initializes i18next and loads Polaris translations.
*
* Intended to be called before rendering the app to ensure translations are present.
*/
export async function initI18n() {
await loadIntlPolyfills();
await Promise.all([initI18next(), fetchPolarisTranslations()]);
}
/**
* @private
* @async
* Asynchronously loads Intl polyfills for the default locale and user locale.
*/
async function loadIntlPolyfills() {
if (shouldPolyfillLocale()) {
await import("@formatjs/intl-locale/polyfill");
}
const promises = [];
if (shouldPolyfillPluralRules(DEFAULT_APP_LOCALE)) {
await import("@formatjs/intl-pluralrules/polyfill-force");
promises.push(loadIntlPluralRulesLocaleData(DEFAULT_APP_LOCALE));
}
if (
DEFAULT_APP_LOCALE !== getUserLocale() &&
shouldPolyfillPluralRules(getUserLocale())
) {
promises.push(loadIntlPluralRulesLocaleData(getUserLocale()));
}
await Promise.all(promises);
}
/**
* A subset of the available plural rules locales
* that match available Shopify Admin languages
* @see Available Shopify Admin languages in the Shopify Help Center:
* https://help.shopify.com/en/manual/your-account/languages#available-languages
*/
const PLURAL_RULES_LOCALE_DATA = {
cs: () => import("@formatjs/intl-pluralrules/locale-data/cs"),
da: () => import("@formatjs/intl-pluralrules/locale-data/da"),
de: () => import("@formatjs/intl-pluralrules/locale-data/de"),
en: () => import("@formatjs/intl-pluralrules/locale-data/en"),
es: () => import("@formatjs/intl-pluralrules/locale-data/es"),
fi: () => import("@formatjs/intl-pluralrules/locale-data/fi"),
fr: () => import("@formatjs/intl-pluralrules/locale-data/fr"),
it: () => import("@formatjs/intl-pluralrules/locale-data/it"),
ja: () => import("@formatjs/intl-pluralrules/locale-data/ja"),
ko: () => import("@formatjs/intl-pluralrules/locale-data/ko"),
nb: () => import("@formatjs/intl-pluralrules/locale-data/nb"),
nl: () => import("@formatjs/intl-pluralrules/locale-data/nl"),
pl: () => import("@formatjs/intl-pluralrules/locale-data/pl"),
pt: () => import("@formatjs/intl-pluralrules/locale-data/pt"),
"pt-PT": () => import("@formatjs/intl-pluralrules/locale-data/pt-PT"),
sv: () => import("@formatjs/intl-pluralrules/locale-data/sv"),
th: () => import("@formatjs/intl-pluralrules/locale-data/th"),
tr: () => import("@formatjs/intl-pluralrules/locale-data/tr"),
vi: () => import("@formatjs/intl-pluralrules/locale-data/vi"),
zh: () => import("@formatjs/intl-pluralrules/locale-data/zh"),
};
async function loadIntlPluralRulesLocaleData(locale) {
return (await PLURAL_RULES_LOCALE_DATA[locale]()).default;
}
/**
* @private
* @async
* Asynchronously initializes i18next.
* @see https://www.i18next.com/overview/configuration-options
* @returns Promise of initialized i18next instance
*/
async function initI18next() {
return await i18next
.use(initReactI18next)
.use(ShopifyFormat)
.use(localResourcesToBackend())
.init({
debug: process.env.NODE_ENV === "development",
lng: getUserLocale(),
fallbackLng: DEFAULT_APP_LOCALE,
supportedLngs: SUPPORTED_APP_LOCALES,
interpolation: {
// React escapes values by default
escapeValue: false,
},
react: {
// Wait for the locales to be loaded before rendering the app
// instead of using a Suspense component
useSuspense: false,
},
});
}
function localResourcesToBackend() {
return resourcesToBackend(async (locale, _namespace) => {
return (await import(`../locales/${locale}.json`)).default;
});
}
/**
* @private
* @async
* Asynchronously loads Polaris translations that correspond to the user locale.
*
* Loads Polaris translations for the default locale if the user locale is not supported.
* @returns {Promise<TranslationDictionary>} Promise of Polaris translations
*/
async function fetchPolarisTranslations() {
if (_polarisTranslations) {
return _polarisTranslations;
}
// Get the closest matching default locale supported by Polaris
const defaultPolarisLocale = match(
[DEFAULT_APP_LOCALE],
SUPPORTED_POLARIS_LOCALES,
DEFAULT_POLARIS_LOCALE
);
// Get the closest matching user locale supported by Polaris
const polarisLocale = match(
[getUserLocale()],
SUPPORTED_POLARIS_LOCALES,
defaultPolarisLocale
);
_polarisTranslations = await loadPolarisTranslations(polarisLocale);
return _polarisTranslations;
}
/**
* Polaris imports are declared explicitly because
* dynamic imports with variables are only supported
* for files with relative paths, not packages.
* @see https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations
*/
const POLARIS_LOCALE_DATA = {
cs: () => import("@shopify/polaris/locales/cs.json"),
da: () => import("@shopify/polaris/locales/da.json"),
de: () => import("@shopify/polaris/locales/de.json"),
en: () => import("@shopify/polaris/locales/en.json"),
es: () => import("@shopify/polaris/locales/es.json"),
fi: () => import("@shopify/polaris/locales/fi.json"),
fr: () => import("@shopify/polaris/locales/fr.json"),
it: () => import("@shopify/polaris/locales/it.json"),
ja: () => import("@shopify/polaris/locales/ja.json"),
ko: () => import("@shopify/polaris/locales/ko.json"),
nb: () => import("@shopify/polaris/locales/nb.json"),
nl: () => import("@shopify/polaris/locales/nl.json"),
pl: () => import("@shopify/polaris/locales/pl.json"),
"pt-BR": () => import("@shopify/polaris/locales/pt-BR.json"),
"pt-PT": () => import("@shopify/polaris/locales/pt-PT.json"),
sv: () => import("@shopify/polaris/locales/sv.json"),
th: () => import("@shopify/polaris/locales/th.json"),
tr: () => import("@shopify/polaris/locales/tr.json"),
vi: () => import("@shopify/polaris/locales/vi.json"),
"zh-CN": () => import("@shopify/polaris/locales/zh-CN.json"),
"zh-TW": () => import("@shopify/polaris/locales/zh-TW.json"),
};
async function loadPolarisTranslations(locale) {
return (await POLARIS_LOCALE_DATA[locale]()).default;
}