Phoenix Localized Routes
Localize your Phoenix website with multilingual URL's and custom template assigns; enhancing user engagement and content relevance.
=> /products/:id/edit @loc.contact = "int@company.com"
/products/:id/edit => /nl/producten/:id/bewerken @loc.contact = "netherlands@company.com"
=> /es/producto/:id/editar @loc.contact = "spain@company.com"Features and Benefits
- URL matches language of content which can enhance user engagement and content relevance.
- Works with Phoenix View and Phoenix LiveView
- Unlimited nesting
-
Supports dynamic nesting (eg.
/{continent}/{county}/{page}and/{country}/{page}) -
Less boilerplate in your
routes.ex - Generates routes at compile time; no performance penalty!
- Easily add custom assigns to your (localized) routes
- Easily generate links to matching pages in other locales
-
Can be used for non-locale alternate routing (eg.
/{sport}/{activity})
Status
This lib will soon be published on Hex. In the meantime the documentation can be found at Github Pages
Usage Summary
-
Add a few line of code to your
Phoenixapplication. - Add a configuration file describing which alternate routes to generate.
-
Optionally:
-
Run
mix gettext.extract --merge. - Translate the URL parts like any other translatable text.
-
Run
-
Run
mix phx.routesto verify the result.
All links in your application will now automatically use the routes for the set locale.
The full guide is written in the Usage Guide.
Requirements
- Elixir >=1.11
- Phoenix >= 1.6.0
- Phoenix LiveView >= 0.16 (optional)
Example
An example Phoenix application showing the (nested) routes and custom assigns.
git clone https://github.com/BartOtten/phoenix_localized_routes_example.git
cd phoenix_localized_routes_example
iex -S mix phx.server
http://localhost:4000/
http://localhost:4000/europe
http://localhost:4000/products
http://localhost:4000/europe/nl/producten/Installation
You can install this library by adding it to your list of dependencies in mix.exs:
def deps do
[
{:phoenix_localized_routes, "~> 0.1.0"}
]
endTo configure your routes have a look at the Usage Guide.
Technical Notes
Phoenix Localized Routes makes use of Macro's to wrap Phoenix Router and Phoenix Router Helpers. It generates alternate helpers and paths based on the routes defined in [YourApp].Routes. Alternate routes are generated at compile time; making them just as fast as the explicitly defined routes.
If a wrapped function call fails, the original function will be called to ensure your application always links to an available page.
To set the custom assigns for use in templates, a Conn.Plug and a helper module with on_mount callback are included.