Internationalization and localization
RubyMine supports various features that help you internationalize and localize Rails applications and plain Ruby projects. You can create or edit locale properties using intention actions right in the editor, run an inspection allowing you to find missing keys, preview localized values, and so on. These capabilities are available for various project file types, including ruby scripts, HTML, ERB, and HAML files.
Following i18n conventions, RubyMine automatically loads dictionary files (YAML or Ruby) stored in the config/locales directory. If necessary, you can provide custom paths. To do this, open the Settings dialog Control+Alt+S, go to the Project Structure page and switch to the I18n tab.
We’ll use the sample Rails application to show internationalization capabilities of the IDE. You can repeat the steps below by checking out this application.
Create locale properties
RubyMine provides an inspection for keys that do not exist in any locale. To see this capability in action, do the following:
Open the app/views/static_pages/home.html.erb file and scroll down to the following code:
<h1><%= t('.welcome') %></h1>Place a caret at the .welcome key and press Control+F1 to see a warning:
Then, press Alt+Enter, choose Create Property and press Enter:
In the invoked dialog, select a locale file and specify the property value:
Use the button to add translations for other locales. Then, click OK to create the specified translations in the dictionary files:
You can navigate from the key called within t method to the corresponding keys in the dictionaries ...
... and vice-versa.
Extract hard-coded strings
RubyMine enables you to extract hard-coded strings into locale files. To try this capabiltity using the sample Rails application, do the following:
Open the app/views/static_pages/home.html.erb project file and scroll down to the following code:
<%= link_to "Sign up now!", signup_path, class: "btn btn-lg btn-primary" %>Place a caret to the “Sign up now!” string, press Alt+Enter and select the I18nize hard-coded string action. Press Enter.
In the Create Property dialog, specify the key and provide the desired translations:
Click OK. RubyMine will add corresponding properties to the locale files and replace the initial string with the t method called for the created key:
<%= link_to t('.signup'), signup_path, class: "btn btn-lg btn-primary" %>
View locale values
RubyMine provides the ability to display localized values instead of I18n keys. To try this capabiltity using the sample Rails application, do the following:
Place a caret at the required key and press Control+. to fold it:
The preview will show the value from the system default locale.
To use another locale to preview values, click Alt+Enter and select Change preview locale:
One more way to see all available translations for the required key is to press Control+Q (Quick Documentation Lookup):