Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/wojtekmaj/react-t. Simple translation module for React applications.
Something went wrong. Try again.
JavaScript
at react-args
1234567891011121314151617181920212223242526272829303132333435363738394041import once from 'lodash.once';import { getUserLocales } from 'get-user-locale';
function altLanguageCode(languageCode) { return (languageCode.includes('-') ? languageCode.split('-')[0] : `${languageCode}-${languageCode.toUpperCase()}` );}
function getMatchingSupportedLocale(userLocale, supportedLocales) { return supportedLocales.find((el) => ( // First, try and find exact match el === userLocale // If not found, try and alter user locale || el === altLanguageCode(userLocale) // If not found, try and alter supported locale instead || altLanguageCode(el) === userLocale ));}
function getMatchingLocaleInternal(supportedLocales) { const userLocales = getUserLocales();
let matchingLocale;
userLocales.some((userLocale) => { matchingLocale = getMatchingSupportedLocale(userLocale, supportedLocales); return matchingLocale; });
return matchingLocale;}
/** * Finds a locale which both we support and user prefers. * * @param {string[]} supportedLocales Supported locales */export const getMatchingLocale = once(getMatchingLocaleInternal);