scratch-www/src/lib/locales.js
2023-02-21 15:36:27 -05:00

44 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Scratch has some locales that are not recognized by Intl. Use an appropriate alternative for these locales.
* @param {string} locale Scratch's locale
* @returns {string} the locale to use in IntlProvider
*/
const scratchLocaleToIntlLocale = locale => {
switch (locale) {
case 'ab':
return 'ru';
case 'an':
case 'rap':
return 'es';
case 'ht':
case 'oc':
return 'fr';
default:
return locale;
}
};
/**
* Gets the locale for the current window.
* @returns {string} locale
*/
const getLocale = () => {
// Get locale from global namespace (see "init.js")
let locale = window._locale || 'en';
if (typeof window._messages !== 'undefined') {
if (typeof window._messages[locale] === 'undefined') {
// Fall back on the split
locale = locale.split('-')[0];
}
if (typeof window._messages[locale] === 'undefined') {
// Language appears to not be supported fall back to 'en'
locale = 'en';
}
}
return locale;
};
module.exports = {
getLocale,
scratchLocaleToIntlLocale
};