How to set locale for errors.po?

How to set the locale in Phoenix to use priv / gettext / {lang} /LC_MESSAGES/errors.po?

As a test, I built a locale file using mix gettext.merge priv/gettext --locale ja and translated a few words into it.

It works if I explicitly call put_locale/2 in web/views/error_helpers.ex and <%= translate_error(message) %> in the template file, but it is a little ugly at the DRY point.

 def translate_error(msg) do Gettext.put_locale(LoginStudy.Gettext, "ja") Gettext.dgettext(LoginStudy.Gettext, "errors", msg) end 

Is there a better way to set the default locale? I have specified default_locale in config/config.ex , but it does not work.

 config :login_study, LoginStudy.Endpoint, default_locale: "ja", 

Best wishes,

+5
source share
1 answer

Great questions @hykw! Since Phoenix's Gettext support is new, the documentation is just starting to appear.

A good starting point is Rebecca Skinner's excellent blog post: http://sevenseacat.net/2015/12/20/i18n-in-phoenix-apps.html

For example, if you want to set the language standard to Japanese only for some of your web requests, you can define a plug-in, as was the case with MyApp.Locale , and run it at the beginning of the request life cycle. I simply do not recommend storing the locale in the session, but I save it as part of the URL or some other parameter.

However, if you want the locale to always be Japanese, you can write to your configuration file:

 config :my_app, MyApp.Gettext, default_locale: "ja" 

Further information on this can be found in the Gettext docs: http://hexdocs.pm/gettext/Gettext.html

+5
source

Source: https://habr.com/ru/post/1239528/


All Articles