aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorAdrien Hopkins <[email protected]>2022-08-16 20:16:38 +0000
committerGitHub <[email protected]>2022-08-16 20:16:38 +0000
commitbb40e60e8623ea9a097dc5f7debe97fef94001e0 (patch)
treea611fefe2c47468068d0d13442b5d8df009ed969 /src/lib
parent6392463d4d542385bc515b07407ab462614b16bd (diff)
parent6af45235e86686b2ea1ca54fe061e0f230a644fc (diff)
downloadcoinos-ui-bb40e60e8623ea9a097dc5f7debe97fef94001e0.tar.gz
coinos-ui-bb40e60e8623ea9a097dc5f7debe97fef94001e0.zip
Merge pull request #1 from coinos/i18n
Add Internationalization to CoinOS
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/i18n.js48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/lib/i18n.js b/src/lib/i18n.js
new file mode 100644
index 00000000..3105c709
--- /dev/null
+++ b/src/lib/i18n.js
@@ -0,0 +1,48 @@
+import { register, init, getLocaleFromNavigator } from 'svelte-i18n';
+
+// The list of locales to look for
+// ADD ALL NEW LANGUAGE CODES TO THIS LIST
+const localeCodes = ['en', 'fr'];
+
+// the default locale to use if none was found
+const defaultLocale = 'en';
+
+// Adds a new locale based on its code
+function addLocale(localeCode) {
+ register(localeCode, () => import(`../locales/${localeCode}.json`));
+}
+
+// Picks the default locale based on navigator locale and the list of available locales
+function pickLocale(navigatorLocale, availableLocales) {
+ // locale can be null the first time you load this
+ // return the default locale in that case - reloading should fix this
+ if (!navigatorLocale) return defaultLocale;
+
+ // determine langugage part of locale
+ // (e.g. if navigatorLocale is 'en-US', languageCode is 'en')
+ let dashIndex = navigatorLocale ? navigatorLocale.indexOf('-') : -1;
+ let languageCode; // navigator locale without the regional part
+ if (dashIndex === -1) {
+ languageCode = navigatorLocale;
+ } else {
+ languageCode = navigatorLocale.slice(0, dashIndex);
+ }
+
+ // get locale
+ if (availableLocales.includes(navigatorLocale)) {
+ return navigatorLocale;
+ } else if (availableLocales.includes(languageCode)) {
+ return languageCode;
+ } else {
+ return navigatorLocale;
+ }
+}
+
+// add locales
+localeCodes.forEach(addLocale);
+
+let initialLocale = pickLocale(getLocaleFromNavigator(), localeCodes);
+init({
+ fallbackLocale: defaultLocale,
+ initialLocale: initialLocale
+});