eligrey / l10n.js

Passive localization JavaScript library
http://purl.eligrey.com/l10n.js
Other
392 stars 68 forks source link

Use different locale than the selected one #33

Closed tbragaf closed 8 years ago

tbragaf commented 8 years ago

Hi all!

First of all, tthis is not an issue, but rather a question. But since I couldn't find an answer, here goes: Is it possible to programatically translate a resource?

In the current app I am developing, we give the user the option to change the current locale. However it seems easy at first, we need to warn the user about the language being changed (not in its current locale, but rather in the option's locale).

Something like <locale>: <locale_warning>

English: Warning, the application will be set to english.
Português: Aviso, o idioma da aplicação mudará para português.

Best regards, tbragaf

eligrey commented 8 years ago

Yep, use String.locale to manually set the localization language.

After setting String.locale = "pt";, l10n.js will give Português translations. Likewise, setting it to "en" will make l10n.js give you English localizations.

eligrey commented 8 years ago

Actually, I see your issue is that you want a global storage for strings not bound to a language. I'm going to implement that right now.

eligrey commented 8 years ago

@tbragaf You can now store default strings under "". For example:

{
    "": {
        "foo": "default"
    },
    "en": {
        "foo": "English"
    },
    "en-US": {
        "foo": "English (US)"
    },
    "pt": {
        "foo": "Português"
    }
}

"foo".toLocalString() === "default" if the user doesn't match any of the supported locales.

eligrey commented 8 years ago

Note that you can also set String.defaultLocale to set any language as the default (e.g. String.defaultLocale = "en"), and then that language will behave like the new "" default I just introduced.

tbragaf commented 8 years ago

@eligrey Thank you very much!