Locale Selection Strategies in Rails Applications

TML SDK for Rails offers a number of ways for telling your Rails application about which language to use for rendering your views. You can use locale parameter, pre-path and pre-domain configurations and even provide custom domains for every language. This post will describe each approach and show you how to configure it in a Rails application using the TML for Rails gem.

Locale Parameter

This is the default and simplest way to tell your application which locale to use. If you don’t provide custom locale configuration, this approach will be used out of the box.

You can customize the parameter name and indicate if you want to store the locale in the cookie. Although we offer cookie support, we recommend passing the locale in every url. So in case your users want to share the URL with anyone, the language will be passed with the URL itself.

To pass the locale in every URL, simply add default_url_options to your application controller as in the following example:

Now you need to surround all your routes with a locale scope:

If you want your default language not to appear in the URL path, you can add a “skip_default” parameter to the locale configuration.

The library will handle all the automatic redirections for you. For instance, if user has Russian as the default locale in the browser, then visiting:

http://myapp.com

will automatically redirect the user to:

http://myapp.com/ru

If you want to handle redirection yourself, then add a “redirect” parameter and set it to false. In this case you should check the tml_current_locale in your before_filter method and perform any redirections that you need.

Pre-Domain

This approach is similar to the pre-path method, but instead of using path fragments it uses the first element of the subdomain.

http://en.lvh.me
http://ru.lvh.me/welcome

To make this approach work, change the locale strategy in the TML init file:

You will need to configure your subdomains to share cookies. This can be done in your session store:

By settings “domain” to “all”, your cookies will be shared and accessible by all your subdomains.

Similarly to the pre-path approach, you can specify if you want the user to be redirect to the appropriate locale. For example, if user navigates to your main site, but the user’s browser preferred locale is set to a supported non-default language, the user will be navigated to the appropriate subdomain.

Custom Domains

If you prefer to use TLDs or completely custom domains for different languages, you can use the custom-domain strategy.

Simply map your domains for every locale, and the language will be selected for you automatically.

User Selected Locale

On top of all the selected strategies, you may want to store locale selection per user. You can customize the TML library to handle that as well.

For any of the above strategy, simply add:

And define the method in your ApplicationController, as in the following example:

Now any time you store the locale in your user’s account, that locale will automatically be used and the user will be redirected to the appropriate URL.

You would also need to make sure to store the new locale once user changes it. You can do that in a “before_filter” method as well.

If you have any questions, please don’t hesitate to contact us.