Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Note: i18n is a singleton that will be shared between all server requests!
// Disable switching locale on the server
if ( typeof document === 'undefined' ) {
return;
}
const { langSlug: targetLocaleSlug, parentLangSlug } = language;
// variant lang objects contain references to their parent lang, which is what we want to tell the browser we're running
const domLocaleSlug = parentLangSlug || targetLocaleSlug;
lastRequestedLocale = targetLocaleSlug;
if ( isDefaultLocale( targetLocaleSlug ) ) {
i18n.configure( { defaultLocaleSlug: targetLocaleSlug } );
setLocaleInDOM( domLocaleSlug, !! language.rtl );
} else {
getLanguageFile( targetLocaleSlug ).then(
// Success.
body => {
if ( body ) {
// Handle race condition when we're requested to switch to a different
// locale while we're in the middle of request, we should abandon result
if ( targetLocaleSlug !== lastRequestedLocale ) {
return;
}
i18n.setLocale( body );
setLocaleInDOM( domLocaleSlug, !! language.rtl );