Set Current Locale in your Rails App

You are probably used to having a before_action somewhere in your controller hierarchy to set the current locale (I18n.locale) based on a value from the URL, the session, a cookie, etc.

The translation gem ships with a generic implementation that can be called directly in your ApplicationController:

before_action :set_locale

But, if you need a custom behaviour, you can of course continue to use your own method by redefining it. For instance:

before_action :set_locale

def set_locale
  I18n.locale = # custom logic to get the locale code
end

Another common use case is to let registered users set their preferred locale in their profile (saved in the database).

Here is what it might look like in such a case:

before_action :set_locale

def set_locale
  if current_user
    I18n.locale = current_user.locale || I18n.default_locale
  else
    super
  end
end

Better Google Translate Suggestions

Some of you may have noticed that Google Translate suggestions of interpolated strings were not very good. Some additional spaces were sometimes inserted.

A sentence like Hello %{user} was translated as Bonjour% {user} by Google.

And sometimes it can get even worse :

Google Translate is just wrong with interpolated variables

We improved that mechanism to help Google Translate to deal with interpolated variables. And we're happy with the results :

Hello %{user} Bonjour %{user}

Hello %{world} Bonjour %{world}

It should prevent your translators from making unfortunate mistakes.

Adding and Removing Target Languages

Here's a question we frequently receive from new users: “How do I add a new target language to my project?

The quick answer is trivial: update the config.target_locales parameter in your project's translation.rb file and perform a sync with rake translation:sync.

TranslationIO.configure do |config|
  ...
  config.target_locales = ['nl', 'fr']
  # e.g., change to ['nl', 'fr', 'de']
  ...
end

It's easy if you already know the language codes you need. But if you don't know what codes to use, don't worry: we added a language picker to the Manage languages page of each project.

Change translation language

Pick your target languages

Just pick all the target languages you wish to have in your project, and we'll generate the line of code for you to copy/paste in the initializer.

Copy. Paste. Sync

New Feature: Copywriting

Many of our users repeatedly asked for it, so we decided to implement a new feature: it’s now possible to edit the source text (in the source language, thus) directly in the translation interface.

Thanks to this new feature, there’s no need anymore for your client or marketing team to come up with all the final text for the website before you can start developing it. Now you can simply define "placeholder" texts in your application using YAML keys and then let your client or marketing team “copywrite” the final text directly in the Translation.io interface.

This feature can even be used as a kind of minimalist CMS for your client.

Copywriting source text - step 1 Copywriting source text - step 2

Copywriting source text - step 3

Once the source text has been edited in the interface, it will be inserted directly in your application the next time you execute rake translation:sync. And don’t worry about conflicts, we’re taking care of that for you!

Please note that this feature can only be used with YAML keys as we can’t make any change to the GetText calls in your source code. Make sure to use YAML keys if you want your client to be able to edit the text.

GitHub Login with Translation.io

We recently added the possibility for users to sign up and sign in to Translation.io with their GitHub accounts.

GitHub has become the most popular code sharing tool among the Ruby and Rails communities. So we figured... if you're working with Rails, you most probably have a GitHub account! Now you can simply sign up via GitHub to save the hassle of having to enter your password.

GitHub sign up on Translation.io

But... what if you created your account before the introduction of this feature? Don't worry, you can still use the GitHub button to sign in as long as you use the same email address for both your Translation.io and GitHub accounts.

And what if you first signed in with GitHub, but now you'd like to also sign in with your email address and password? Well, we got you covered: simply make sure to first sign out, then use the forgot your password? link to set a new password.