diff options
| author | Adrien Hopkins <[email protected]> | 2022-08-16 20:16:38 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-08-16 20:16:38 +0000 |
| commit | fb8c38fec81de1afbdb00337d0af4c1c871cb3c3 (patch) | |
| tree | ba2df8ee23c7b17abd3cf290f488ab470f9b3a6e /src/lib | |
| parent | 36ff76c00dcd8aa52cf82fdf41c1feed669d2a6a (diff) | |
| parent | e99c489d61c3100d952dfedb799505122e36d33c (diff) | |
| download | coinos-ui-fb8c38fec81de1afbdb00337d0af4c1c871cb3c3.tar.gz coinos-ui-fb8c38fec81de1afbdb00337d0af4c1c871cb3c3.zip | |
Merge pull request #1 from coinos/i18n
Add Internationalization to CoinOS
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/i18n.js | 48 |
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 +}); |
