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

Get currently used translation #714

Closed kaladivo closed 1 year ago

kaladivo commented 1 year ago

Description

Let's say your app supports english and german language. User with czech locale logs in and the app fallbacks to english.

Currently, if you setup i18n like this:

export const i18n = new I18n(
  enableHiddenFeatures
    ? translations
    : {
        en:  translations.en,
        de: translations.de,
      }
)
i18n.locale = 'cs'
i18n.defaultLocale = 'en'
i18n.enableFallback = true

i18n.locale returns "cs" despite en translations being used.

Describe the solution

Add function getUsedLocale() that will return locale that is currently used by the library.

Alternatives you considered

In my example above, we can test keys(translations).include(I18n.locale), but parsing language code is not that straight forward usually it might be in format like da_DK or de_DE. This logic is already included in the library and it does not seem right to duplicate it to find out what language is used.

kaladivo commented 1 year ago

Oh sorry, this is the ruby package. Will recreate it in it's js alternative.