All Projects → boudra → alternate

boudra / alternate

Licence: Apache-2.0 license
Plug and Phoenix helpers to localize your web app via the URL

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to alternate

msgtools
Tools for Developing Diagnostic Messages
Stars: ✭ 18 (-30.77%)
Mutual labels:  i18n, localization, gettext
Node Gettext
A JavaScript implementation of gettext, a localization framework.
Stars: ✭ 175 (+573.08%)
Mutual labels:  i18n, localization, gettext
rails
Rails translation made _('simple').
Stars: ✭ 65 (+150%)
Mutual labels:  i18n, localization, gettext
Weblate
Web based localization tool with tight version control integration.
Stars: ✭ 2,719 (+10357.69%)
Mutual labels:  i18n, localization, gettext
stone.js
gettext-like client-side Javascript Internationalization Library
Stars: ✭ 20 (-23.08%)
Mutual labels:  i18n, localization, gettext
Ngettext
A cross-platform .NET implementation of the GNU/Gettext library.
Stars: ✭ 172 (+561.54%)
Mutual labels:  i18n, gettext
React Translated
A dead simple way to add complex translations (i18n) in a React (DOM/Native) project 🌎🌍🌏
Stars: ✭ 176 (+576.92%)
Mutual labels:  i18n, localization
Localize Router
An implementation of routes localisation for Angular
Stars: ✭ 177 (+580.77%)
Mutual labels:  i18n, localization
Glotpress Wp
🌍 🌎 🌏 GlotPress is a WordPress plugin to let you set up your own collaborative, web-based software translation tool.
Stars: ✭ 205 (+688.46%)
Mutual labels:  i18n, gettext
Jquery.ime
jQuery based input methods library
Stars: ✭ 145 (+457.69%)
Mutual labels:  i18n, localization
Linguist
Easy multilingual urls and redirection support for the Laravel framework
Stars: ✭ 188 (+623.08%)
Mutual labels:  i18n, localization
Tempura
Pure Clojure/Script i18n translations library
Stars: ✭ 211 (+711.54%)
Mutual labels:  i18n, gettext
Es2015 I18n Tag
ES2015 template literal tag for i18n and l10n (translation and internationalization)
Stars: ✭ 171 (+557.69%)
Mutual labels:  i18n, localization
Formatjs
The monorepo home to all of the FormatJS related libraries, most notably react-intl.
Stars: ✭ 12,869 (+49396.15%)
Mutual labels:  i18n, localization
I18n Extract
Manage localization with static analysis. 🔍
Stars: ✭ 152 (+484.62%)
Mutual labels:  i18n, localization
L10ns
Internationalization workflow and formatting
Stars: ✭ 234 (+800%)
Mutual labels:  i18n, localization
Lang.js
🎭 Laravel Translator class in JavaScript!
Stars: ✭ 232 (+792.31%)
Mutual labels:  i18n, localization
Ttag
📙 simple approach for javascript localization
Stars: ✭ 243 (+834.62%)
Mutual labels:  i18n, localization
React Native Globalize
Internationalization (i18n) for React Native
Stars: ✭ 246 (+846.15%)
Mutual labels:  i18n, localization
Punic
PHP translation and localization made easy!
Stars: ✭ 133 (+411.54%)
Mutual labels:  i18n, localization

Hex.pm


Alternate

A library to serve your Phoenix app in different locales.

Installation

The package can be installed as:

Add alternate to your list of dependencies in mix.exs:

def deps do
  [{:alternate, "~> 0.1.0"}]
end

Ensure alternate is configured in config/config.exs:

config :alternate,
    locales: %{
        "en-GB" => %{ path_prefix: "gb" },
        "en-US" => %{ path_prefix: "us" }
    },
    locale_assign_key: :locale,
    gettext_module: YourAppModule.Gettext
  • 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.
    • path_prefix is the prefix that will be used in your urls, for example: http://example.com/gb will load the en-GB locale.
  • locale_assign_key is the key that will be used to store the loaded locale in the assigns
  • gettext_module is the Gettext module to use, it will most probably be {YourAppModule}.Gettext

Router

You'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:

import Alternate

this will let you be able to use the localize macro in your routes like this:

localize(get("/", PageController, :index))

if we run mix phoenix.routes we'll see that it created all the routes for our defined locales:

$ mix phoenix.routes
page_path  GET  /gb  AlternateExample.PageController [action: :index, locale: "en-GB"]
page_path  GET  /us  AlternateExample.PageController [action: :index, locale: "en-US"]

Now 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:

plug Alternate.Plug

Now 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.

If we want to specify route translations we can do it like this:

localize(get("/start", PageController, :index), translations: %{
  "es-ES" => "/empezar"
})

The locales that you don't define in the translations map will use the default route to match (/start in this case).

Controller

We'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.

use Alternate.Controller

Route helpers

To generate localized routes we'll need to add this:

import Alternate.Helpers

to our controller and view sections of our web/web.ex.

Now similarly to the routing, we can use localize to generate translated paths and urls:

localize(Routes.page_path(conn, :index))

This will automatically translate and add the right prefix depending on the locale the user is on.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].