This is a quick stab at a problem that the set_locale
package is supposed to solve but seems to be broken and unsupported, or at least that's what its issue list indicates.
We want to extract the locale from the path prefix; i.e. "/en/users"
and "/sv/users"
should correctly set the current Gettext locale for the request. "/users"
should keep the default locale.
We're not considering cookies or Accept-Language
in this scope. We also don't do redirecting; the unlocalized path will hit the action as usual.
Using this solution requires two changes to the router:
- Paths that should be localized need to be wrapped in a
localized/2
macro. It simply repeats its content with and without ascope /:locale
so that both versions are visible to the route inspector and play nicely with verified routes. - The browser pipeline needs to include a plug that extracts the param and sets the current Gettext locale. It also creates an assign for convenience.