How to change default_locale in Gettext

gettext
Tags: #<Tag:0x00007fc836b2f808>

#1

Hello,

I am supposed to create multi language website, therefore I am using gettext for this.
At first I created 3 language in my project and I made translations in it, but I couldn’t change default_locale like its document.

my project gited in https://github.com/shahryarjb/ESOGTIPH

for example I have 3 lang ["ru", "en", "fa"]

my router :

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
    plug SetLocale, gettext: MultiLangWeb.Gettext, default_locale: "fa", cookie_key: "project_locale" #cookie_key is optional
  end

  pipeline :api do
    plug :accepts, ["json"]
  end

  scope "/", MultiLangWeb do
    pipe_through :browser # Use the default browser stack

    get "/", PageController, :dummy
  end

and my project config :

config :multi_lang, MultiLangWeb.Gettext,
    default_locale: "fa"

As you can see, I changed fa insted of en in my config , but it allways loads en default .

How can I changed default_locale to fa or ru?

I had read (https://phraseapp.com/blog/posts/i18n-for-phoenix-applications-with-gettext/),but there was no use for me .

please help me for changing it.

Thanks


#2

:default_locale is set in :gettext app config, I think.

https://hexdocs.pm/gettext/Gettext.html#module-default-locale


#3

Unfortunately, It does not work for me

I have tested this before

config :multi_lang, MultiLangWeb.Gettext,
    default_locale: "fa"

config :gettext, :default_locale, "fa"

or just

config :gettext, :default_locale, "fa"

my project gited in https://github.com/shahryarjb/ESOGTIPH


#4

Unfortunately, It does not work for me

Maybe you need to change the process’s locale and not the default locale for the app? Then you need to set it in one of your plugs probably.

https://hexdocs.pm/gettext/Gettext.html#put_locale/1

It will store locale info in the process dictionary and all subsequent operations in that process would have access to it.


#5

Hello ,

may you test my project in github because I can’t fix it? I think Gettext loads everything on config file and doesn’t show any reaction to put_locale/1 .

I have entered ‍Gettext.get_locale(MultiLangWeb.Gettext) in terminal and Gettext.put_locale(MultiLangWeb.Gettext, "fa"), after that I have tested ‍Gettext.get_locale(MultiLangWeb.Gettext) and it shows me fa, but I have a problem yet.

The en is still default page.

May I not know where I can enter the put_locale?


#6

Maybe in a plug somewhere before the controller action.

You seem to be doing it with https://github.com/shahryarjb/ESOGTIPH/blob/master/lib/multi_lang_web/router.ex#L12 already. Now you need to provide the appropriate accept-language headers in your request for it to work.


#7

Or not, I’m a bit confused at what https://github.com/smeevil/set_locale actually does.

Maybe try https://github.com/kipcole9/cldr with its plugs.

I personally find it easier to write these little plugs myself …