shahryarjb

shahryarjb

How to change default_locale in Gettext

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

Most Liked

idi527

idi527

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

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

kip

kip

ex_cldr Core Team

If you try the following, what do you see?

iex -S mix
iex> Gettext.known_locales MultiLangWeb.Gettext
iex> Gettext.default_locale MultiLangWeb.Gettext

The last function call should return fa in your configured case (you shouldn’t need to configure the :locales key).

If for some reason you aren’t seeing fa then I think there is a different problem.

Joep

Joep

Just wanted to mention http://blog.plataformatec.com.br/2016/03/using-gettext-to-internationalize-a-phoenix-application/ that gives a very helpful explanation about using Gettext.

It’s the third hit and first article googling “phoenix gettext” but still… :wink:

idi527

idi527

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.

idi527

idi527

Maybe in a plug somewhere before the controller action.

You seem to be doing it with ESOGTIPH/lib/multi_lang_web/router.ex at master · shahryarjb/ESOGTIPH · GitHub already. Now you need to provide the appropriate accept-language headers in your request for it to work.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement