fnando / i18n-js

It's a small library to provide the I18n translations on the Javascript. It comes with Rails support.
MIT License
3.77k stars 520 forks source link

Fallback behaving unexpectedly #595

Closed NoahKr closed 3 years ago

NoahKr commented 3 years ago

Hi, I'm using i18n-js@3.8.0 in react-native installed via npm install i18n-js@^3.8.0. I have the following configuration:

import i18n from 'i18n-js'

import en from './translations/en.json'
import nl from './translations/nl.json'
import nl_override from './translations/special/override/nl.json'
import en_override from './translations/special/override/en.json'

i18n.defaultLocale = 'en'
i18n.locale = "nl_override"
i18n.fallbacks = {
    nl: "nl_override",
    en: 'nl',
    nl_override: ["nl", "en_override", "en"],
    en_override: ["en", "nl_override", "nl"]
}
i18n.translations = { en, nl, nl_override, en_override }

What I expect to happen: 1 If key is present in nl_override translation will be taken from the nl_override file.

  1. Translations for keys that are not present in nl_override will be taken from nl, if it's not found there either, look for it in en_override and so on.

What happens:

  1. Expected behaviour
  2. Translations for keys that are not present in nl_override are taken from en

Anyone knows what I'm doing wrong? Thanks in advance.

NoahKr commented 3 years ago

I figured out what I was doing wrong. i18n.fallbacks is a boolean and should just be true.

The following does work:

// Set fallback chain
_.assign(i18n.locales, {
    nl: ["nl", "nl_override", 'en'],
    en: ["en", "en_override", 'nl'],
    nl_override: ["nl_override", "nl", "en_override", "en"],
    en_override: ["en_override", "en", "nl_override", "nl"]
})

You might want to update the README, but other than that very nice library :)

PikachuEXE commented 3 years ago

Thanks for your feedback I think we do have some text in README mentioning how to enable fallback and config custom fallback rules (Search for Translation fallback can be enabled) image But if you think we could use some formatting / content update feel free to edit and submit PR ;)