shahryarjb

shahryarjb

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 GitHub - shahryarjb/ESOGTIPH: Easy sample of gettext in Phoenix 1.3 · GitHub

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 (Translating Phoenix Applications with GNU gettext | Phrase),but there was no use for me .

please help me for changing it.

Thanks

Showing Posts 1 to 10

idi527

idi527

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

shahryarjb

shahryarjb OP

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 GitHub - shahryarjb/ESOGTIPH: Easy sample of gettext in Phoenix 1.3 · GitHub

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.

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

shahryarjb

shahryarjb OP

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?

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.

idi527

idi527

Or not, I’m a bit confused at what GitHub - smeevil/set_locale: elixir phoenix plug that can extract locale from browser headers or paths for redirection and setting of get text locales · GitHub actually does.

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

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

thib

thib

Hi

Had the same problem here ; I found few things on Google and ended up with that. Default language will be used until the user sends a locale in params, then it will be stored in a simple cookie. Just use it in your plug and you should be fine.

I tried to make it work with get_req_header(conn, “accept-language”), but never found how to do nor why I should use that…

@locales Gettext.known_locales(MyappWeb.Gettext)

def call(conn, _opts) do
case locale_from_params(conn) || locale_from_cookies(conn) do
  nil ->
    Gettext.put_locale("mr") # Language by default no handled by Gettext
    conn
      |> persist_locale("mr")
  locale ->
    Gettext.put_locale(locale)
    conn
      |> persist_locale(locale)
   end
end

defp persist_locale(conn, new_locale) do
  conn |> put_resp_cookie("locale", new_locale, max_age: 10 * 24 * 60 * 60)
end
defp locale_from_params(conn) do
  conn.params["locale"] |> validate_locale
end
defp locale_from_cookies(conn) do
   conn.cookies["locale"] |> validate_locale
end
defp validate_locale(locale) when locale in @locales, do: locale
defp validate_locale(_locale), do: nil
Kurisu

Kurisu

I followed this tutorial and it works for me: Phoenix I18n.
The difference I noticed at first glance between the way you’re setting default locale in config is that you don’t provide the full list of supported locales.
For example
config :demo, I18ndemoWeb.Gettext, default_locale: ru, locales: ~w(en ru)
I hope it will heelp…
That works for me.

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 Using Gettext to internationalize a Phoenix application « Plataformatec Blog that gives a very helpful explanation about using Gettext.

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews