Cochonours

Cochonours

I’m a newbie so it may seem like a silly question, but I can’t find the right way to do the following…

What I want : urls with locale parameter followed by whatever path. Important: paths are localised too (I’d like to add “of course”, but it’s not often the case), so " /fr/photos" and “/en/pictures” should point to the same controller and action (the locale information should just be in the params).

If I use in my router scope "/:locale/", I get the locale in the params but I cannot specify that the path “photos” should be only when the locale is “fr”.

If I use the root scope, I can write the full paths but I won’t get the locale set in params (and it would be a pain to write different ctrlers)

What I’m thinking is the best option is to declare one scope per locale like this scope "/fr/", scope "/en/", … so every scope defines its own localised paths, and write one function per locale that would set the “locale” param that I would call via pipe_through.

I’d be glad to know the recommended way, as I do not know enough to make a good judgement!

Showing Posts 1 to 10

pedromvieira

pedromvieira

You can create your own plug to set locale for you, but a easy way is to use set_locale.

So in your router you define your basic locale and scopes.

router.ex

...
  pipeline :app do
    ...
    plug SetLocale, gettext: MyApp.Gettext, default_locale: "en"
    ...
  end
...
  scope "/:locale", MyAppWeb do
    pipe_through [:browser, :app, :browser_session]
    get "/", PageController, :index
  end
...

This will add locale as a params to your controller.
At this point you have a locale (fr, en, any…), so it’s up to you to use (to translate with gettext) or not (to provide photos) inside your controller.

controller.ex

  def new(conn, params) do
    locale =
      params["locale"]
    ...
    conn
    |> render("new.html", changeset: changeset,
      locale: locale)
  end
OvermindDL1

OvermindDL1

As a note, there is a standard header (Accept-Language) passed in for the browser to request a locale, why not use that instead of encoding it into the path?

LostKobrakai

LostKobrakai

The header is nice as a default, but people might want so view a different language than the default one.

NobbZ

NobbZ

Exactly this. My browser sends a German request, and i do not bother for much resources, but when I hear some technical documentation, I prefer to read it in English as the quality of the text is usually better, as well as it’s usually more complete as well.

This is especially true when I’m browsing Microsoft documentation, which is partially translated by humans and by machines or simply leaving out complete paragraphs…

allyraza

allyraza

as a general approach I usually try to avoid hard coding locale params in the urls, I use the language header or a cookie if the user need to view the page in a different language

kip

kip

ex_cldr Core Team

You might also find Cldr.Plug.SetLocale in the package ex_cldr useful. (I’m the author). It can:

  1. Set locale for gettext and cldr (either or both)
  2. Identify locale from the Accept-Languge header, the url, the session, a cookie or a query param or any combination in any priority
Cochonours

Cochonours OP

That’s good to guess the default locale, but if you are not using your own browser you will not want to have the same links open in a locale you do not know. Also, as I want the urls to be different based on the locale, this wouldn’t do (aside from “/”).

Cochonours

Cochonours OP

This looks like my first solution (I do use cldr’s setlocale in my pipeline btw).

But with

  scope "/:locale", MyAppWeb do
    pipe_through [:browser, :app, :browser_session]
    get "/", PageController, :index
  end

there is no way to define localised urls like get "/photos", PageController, :index and get "/pictures", PageController, :index, as both “/fr/pictures” and “/en/photos” would resolve and I do not want that.

Cochonours

Cochonours OP

When urls are not localised that’s a good possibility, until you travel and have to use browsers with chinese locale by default :wink:

@kip (I can’t reply to your post as I have 3 consecutive posts..)

I am using it for the root url in order to guess the locale! It works like a charm.

In order to make it to work with the url, I have to use the option “:path”, with a parameter name right? So as I cannot define my routes with a scope like scope "/:locale", MyAppWeb do, I cannot set this parameter name. It worked until I decided to have localised urls :wink: (UNLESS there is a way to restrict routes depending on the value of :locale (e.g. define get "/pictures", PageController, :index only when :locale == "en").

pedromvieira

pedromvieira

You can still have scopes / pipeplines with locale or not (/photos) in a correct order in your router. In my case I have one that dosen’t need (llike /) and other that check via plug the accepted one and redirect the user to default or available language (gettext).

example without pipeline / locale

  scope "/", MyAppWeb do
    pipe_through :browser
    get "/", Tracker.Controller, :index
  end

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews