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

What message file is used by default and why? #29

Open
krisztianb opened this issue Nov 19, 2019 · 5 comments
Open

What message file is used by default and why? #29

krisztianb opened this issue Nov 19, 2019 · 5 comments

Comments

@krisztianb
Copy link

We are using GetText.js in our application. Our default language is German. At the start of our application we load the English translations like this:

setMessages("messages", "en", jsonMessagesEn);

We never call setLocale to set the language, yet when calling the translation methods like this:

_("Guten Morgen")

We get an English text. My expected behaviour would be to return the default text, when no locale has been set. What is your opinion?

@guillaumepotier
Copy link
Owner

Hi @krisztianb,

Thanks for using gettext and for this issue :)

You're right, If I recall clearly (it's been a long time I haven't worked on this lib, yet using it everyday) I internally call setLocale when you add a catalog.

I think I did that because I did not saw a practical use case where you load a catalog without wanting to use it for translations. I also assumed that msgid (templates default keys) would be in english (I did not see on the Internet a case where it was in French or German, though it should work for every locale that has only 2 plural forms).

In my usage, I even don't load any en catalog file since its already in the template (and it avoid an unnecessary call to a json/js catalog file), and lazy load the other translation files when needed. But maybe this is my use case that is weird.

Tell me a bit more about yours?

Best

@krisztianb
Copy link
Author

Hi @guillaumepotier,

thanks for the great library. We are using it in our "word processing" application to display messages and for the GUI elements as well. Since our company is located in Austria, all the messages are in German by default.

The technical use case is basically:

  1. Loading all message files when the application loads.
  2. When the user specifies a language setLocale is called. If he/she doesn't, we don't call setLocale.

If I understand you correctly, this would mean that when we load 10 message files at step one and the last is for Japanese, the messages in the application would all be in Japanese. :-) This seems a bit odd to me. I mean setting the last loaded language as "default" doesn't really make sense. But maybe this is just my opinion.

We currently fixed the issue by adding a lang="de" attribute to our HTML element.

I don't know if you are willing to change this behaviour, since it could also effect other users who expect the current behaviour.

@guillaumepotier
Copy link
Owner

Hum,

I went through the code source of setMessages here and it does not seem that it calls internally setLocale. So what I wrote above is just false ^^

Local is set here, maybe you should look here?

@krisztianb
Copy link
Author

Thanks for looking into it.

So there are three ways to set the default locale:

  1. Pass an object to the factory function eg. { locale: "de" }
  2. Set the lang="de" attribute on the html element
  3. Call setLocale in the initalization phase of the application

The "problem" is this line: https://github.com/guillaumepotier/gettext.js/blob/master/lib/gettext.js#L9 which sets "en" as the default locale, if none of the ways above is used.

Couldn't you just simply remove the || 'en' from that line?

@guillaumepotier
Copy link
Owner

I think this should be doable.

I may find some time next week to look into it and ensure it does not breaks too much things in the test suite (except the get default locale test ;))

It makes sense, if no language is specified nor detected, it should simply render the default strings in the templates.

Keep you posted

Best

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