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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
csmr
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley: Mutonex gamedesign was finish...
New
Stef-Verniers
Hello I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
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
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews