{"id":19204204,"url":"https://github.com/boudra/alternate","last_synced_at":"2025-12-11T23:48:52.196Z","repository":{"id":57478820,"uuid":"64066880","full_name":"boudra/alternate","owner":"boudra","description":"Plug and Phoenix helpers to localize your web app via the URL","archived":false,"fork":false,"pushed_at":"2021-01-14T12:24:22.000Z","size":60,"stargazers_count":27,"open_issues_count":2,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-17T12:07:42.566Z","etag":null,"topics":["elixir","gettext","i18n","localization","phoenix","plug"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/boudra.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-07-24T13:34:33.000Z","updated_at":"2023-07-24T01:56:17.000Z","dependencies_parsed_at":"2022-08-29T00:40:57.616Z","dependency_job_id":null,"html_url":"https://github.com/boudra/alternate","commit_stats":null,"previous_names":["boudra/polygot"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boudra%2Falternate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boudra%2Falternate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boudra%2Falternate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boudra%2Falternate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/boudra","download_url":"https://codeload.github.com/boudra/alternate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253767566,"owners_count":21961135,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["elixir","gettext","i18n","localization","phoenix","plug"],"created_at":"2024-11-09T13:06:36.737Z","updated_at":"2025-12-11T23:48:52.149Z","avatar_url":"https://github.com/boudra.png","language":"Elixir","readme":"[![Hex.pm](https://img.shields.io/hexpm/v/alternate.svg)](https://hex.pm/packages/alternate)\n\n------------------------------------\n\n# Alternate\n\n\nA library to serve your Phoenix app in different locales.\n\n## Installation\n\nThe package can be installed as:\n\nAdd `alternate` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [{:alternate, \"~\u003e 0.1.0\"}]\nend\n```\n\nEnsure `alternate` is configured in `config/config.exs`:\n\n```elixir\nconfig :alternate,\n    locales: %{\n        \"en-GB\" =\u003e %{ path_prefix: \"gb\" },\n        \"en-US\" =\u003e %{ path_prefix: \"us\" }\n    },\n    locale_assign_key: :locale,\n    gettext_module: YourAppModule.Gettext\n```\n\n* `locales` this is a map of the locales you want to support, the key will also be used as the name for your Gettext locale.\n    * `path_prefix` is the prefix that will be used in your urls, for example: `http://example.com/gb` will load the `en-GB` locale.\n* `locale_assign_key` is the key that will be used to store the loaded locale in the `assigns`\n* `gettext_module` is the Gettext module to use, it will most probably be `{YourAppModule}.Gettext`\n\n## Router\n\nYou'll need to import `Alternate` to your router(s), it is recommended that you do so in the `def router do` section in `web/web.ex`:\n\n```elixir\nimport Alternate\n```\n\nthis will let you be able to use the `localize` macro in your routes like this:\n\n```elixir\nlocalize(get(\"/\", PageController, :index))\n```\n\nif we run `mix phoenix.routes` we'll see that it created all the routes for our defined locales:\n\n```bash\n$ mix phoenix.routes\npage_path  GET  /gb  AlternateExample.PageController [action: :index, locale: \"en-GB\"]\npage_path  GET  /us  AlternateExample.PageController [action: :index, locale: \"en-US\"]\n```\n\nNow all that's left to do is to add Alternate's plug into your pipeline, so that it can set the appropiate locale based on the requested path:\n\n```elixir\nplug Alternate.Plug\n```\n\nNow when you load `http://exmple.com/gb` the `:locale` assign will be equal to `\"en-GB\"`, and your Gettext locale will be set to `\"en-GB\"` automatically.\n\nIf we want to specify route translations we can do it like this:\n\n```elixir\nlocalize(get(\"/start\", PageController, :index), translations: %{\n  \"es-ES\" =\u003e \"/empezar\"\n})\n```\n\nThe locales that you don't define in the `translations` map will use the default route to match (`/start` in this case).\n\n## Controller\n\nWe'll need to add an extra `init/1` function in or controllers so that they can support localised actions, you can add this to the `controller` section of your `web/web.ex`.\n\n```elixir\nuse Alternate.Controller\n```\n\n## Route helpers\n\nTo generate localized routes we'll need to add this:\n\n```elixir\nimport Alternate.Helpers\n```\n\nto our `controller` and `view` sections of our `web/web.ex`.\n\nNow similarly to the routing, we can use `localize` to generate translated paths and urls:\n\n```elixir\nlocalize(Routes.page_path(conn, :index))\n```\n\nThis will automatically translate and add the right prefix depending on the locale the user is on.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboudra%2Falternate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fboudra%2Falternate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboudra%2Falternate/lists"}