Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use different locale than the selected one #33

Closed
tbragaf opened this issue May 17, 2016 · 5 comments
Closed

Use different locale than the selected one #33

tbragaf opened this issue May 17, 2016 · 5 comments

Comments

@tbragaf
Copy link

tbragaf commented May 17, 2016

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
Copy link
Owner

eligrey commented May 17, 2016

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
Copy link
Owner

eligrey commented May 17, 2016

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 added a commit that referenced this issue May 17, 2016
@eligrey
Copy link
Owner

eligrey commented May 17, 2016

@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
Copy link
Owner

eligrey commented May 17, 2016

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
Copy link
Author

tbragaf commented May 17, 2016

@eligrey Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants