Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
How to set i18n.js fallback language?

I am using the i18n-js library.

In order to set its configuration, I am doing the following:

export function setI18nConfig(locale = undefined) {
  // Fallback if no available language fits
  const fallback = DEFAULT_APP_LANGUAGE;
  const language =
    APP_LANGUAGES.find((language) => language.locale === locale) ?? fallback;

  const { locale: _locale, isRTL } = language;

  // Clear the translation cache
  translate.cache.clear();

  // Update the layout direction
  I18nManager.forceRTL(isRTL);

  // Set the i18n-js config
  i18n.locale = _locale;
  i18n.translations = { [_locale]: translationGetters[_locale]() };

  // Set moment config
  moment.locale(_locale);

  // Return the appropiate locale
  return _locale;
}

As you can see, in order to set the translations dictionary, I am calling this getter:

 export const translationGetters = {
  en: () => require("../../../assets/languages/en/translations.json"),
  es: () => require("../../../assets/languages/es/translations.json"),
  fr: () => require("../../../assets/languages/fr/translations.json"),
  it: () => require("../../../assets/languages/it/translations.json"),
  pt: () => require("../../../assets/languages/pt/translations.json"),
};

For establishing the fallback language, I am just trying to find the given locale (function argument) in my array of app languages.

export const APP_LANGUAGES = [
  {
    name: "English",
    locale: "en",
    isRTL: false,
  },
  {
    name: "French",
    locale: "fr",
    isRTL: false,
  },
  {
    name: "Italian",
    locale: "it",
    isRTL: false,
  },
  {
    name: "Portuguese",
    locale: "pt",
    isRTL: false,
  },
  {
    name: "Spanish",
    locale: "es",
    isRTL: false,
  },
];

If it is not in the list, then I use the DAFAULT_APP_LANGUAGE as translation.

export const DEFAULT_APP_LANGUAGE = APP_LANGUAGES[0];

Is this the typical or correct way to set i18n.js fallbacks?

Isn't there any simple way to do that?

I mean, something like:

  i18n.fallbackLanguage = "en";
  i18n.locale = locale;
  i18n.translations = {
     [locale]: translationGetters[locale](), 
     [fallbackLanguage]: translationGetters[fallbackLanguage]() 
  };

Note: I also need to set the isRTL config for the fallback!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!