@ -11,6 +11,14 @@ import { ResourceLoader, Resources, TFunction, TransProps, TransType } from './t
let tFunc : I18NextTFunction < string [ ] , undefined > | undefined ;
let transComponent : TransType ;
const VALID_LANGUAGES = [
. . . LANGUAGES ,
{
name : 'Pseudo' ,
code : PSEUDO_LOCALE ,
} ,
] ;
function initTFuncAndTransComponent ( { id , ns } : { id? : string ; ns? : string [ ] } = { } ) {
if ( id ) {
tFunc = getI18nInstance ( ) . getFixedT ( null , id ) ;
@ -28,13 +36,15 @@ export async function loadPluginResources(id: string, language: string, loaders?
return ;
}
const resolvedLanguage = language === PSEUDO_LOCALE ? DEFAULT_LANGUAGE : language ;
return Promise . all (
loaders . map ( async ( loader ) = > {
try {
const resources = await loader ( language ) ;
addResourceBundle ( language , id , resources ) ;
const resources = await loader ( reso lvedL anguage) ;
addResourceBundle ( reso lvedL anguage, id , resources ) ;
} catch ( error ) {
console . error ( ` Error loading resources for plugin ${ id } and language: ${ language } ` , error ) ;
console . error ( ` Error loading resources for plugin ${ id } and language: ${ reso lvedL anguage} ` , error ) ;
}
} )
) ;
@ -108,7 +118,7 @@ async function initTranslations({
returnEmptyString : false ,
// Required to ensure that `resolvedLanguage` is set property when an invalid language is passed (such as through 'detect')
supportedLngs : LANGUAGES.map ( ( language ) = > language . code ) ,
supportedLngs : VALID_ LANGUAGES.map( ( lang ) = > lang . code ) ,
fallbackLng : DEFAULT_LANGUAGE ,
ns ,
@ -123,7 +133,7 @@ async function initTranslations({
const detection : DetectorOptions = { order : [ 'navigator' ] , caches : [ ] } ;
options . detection = detection ;
} else {
options . lng = LANGUAGES . find ( ( lang ) = > lang . code === language ) ? . code ? ? undefined ;
options . lng = VALID_ LANGUAGES. find ( ( lang ) = > lang . code === language ) ? . code ? ? undefined ;
}
if ( module ) {
@ -132,7 +142,7 @@ async function initTranslations({
getI18nInstance ( ) . use ( initReactI18next ) ; // passes i18n down to react-i18next
}
if ( process . env . NODE_ENV === 'development' ) {
if ( language === PSEUDO_LOCALE ) {
const { default : Pseudo } = await import ( 'i18next-pseudo' ) ;
getI18nInstance ( ) . use (
new Pseudo ( {
@ -165,7 +175,7 @@ export function getNamespaces() {
}
export async function changeLanguage ( language? : string ) {
const validLanguage = LANGUAGES . find ( ( lang ) = > lang . code === language ) ? . code ? ? DEFAULT_LANGUAGE ;
const validLanguage = VALID_ LANGUAGES. find ( ( lang ) = > lang . code === language ) ? . code ? ? DEFAULT_LANGUAGE ;
await getI18nInstance ( ) . changeLanguage ( validLanguage ) ;
}