|
|
Line 1: |
Line 1: |
| Internationalization in Rails:
| |
| Internationalization support was extended from Rails version 2.2. So in order to internationalize a rails application, it has to be migrated to Rails 2.2 or above. Every static string in the Rails framework — e.g. Active Record validation messages, time and date formats — has been internationalized
| |
| Internationalization features provided in each version if Rails.
| |
| Ruby 2.2 Internationalization support was extended from this version of Rails.
| |
| Ruby 2.3 Additional feature for Localized Views was added in this version. The view files were rendered with an extension of the locale name. I18n#available_locales and I18n::SimpleBackend#available_locales is available to retrieve an array of available locales.
| |
| Ruby 3.0 Some additional features were added to the I18n gem for speed improvements like default translations for attributes,automatic pull for translations on form submit, etc.
| |
| Ruby 3.1 I18n namespace lookup support removed.
| |
|
| |
|
| Following needs to be done in order to internationalize a rails application:
| |
| 1. Ensure support for internationalization by migrating to appropriate version of rails if needed.
| |
| 2. Tell rails where to find the appropriate translations files
| |
| 3. Tell rails how to switch locales
| |
| YAML (.yml) or plain Ruby (.rb) files are used for storing translations.
| |
| Setting up the rails application for internationalization:
| |
| 1. Configure the I18n module : Rails adds all .rb and .yml files from the config/locales directory to your translations load path, automatically. This is the default setting provided by the I18n(https://github.com/svenfuchs/i18n) gem. In order to override this setting we have to make changes in the application.rb files that has instructions on how to add locales from another directory and how to set a different default locale. Just uncomment and edit the specific lines.
| |
| # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
| |
| config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
| |
| config.i18n.default_locale = :de
| |
|
| |
| 2. Custom I18n Configuration Setup
| |
| Optionally, the above code can be placed anywhere in the application preferably in initializers
| |
|
| |
| 3. Setting and passing locale:
| |
| The above two steps help setup a default locale throughout the application. But in case we have to provide our application in different languages, we can setup the application default locale in the application.rb and then set the locale using the application-controller.rb before_action as
| |
| before_action :set_locale
| |
|
| |
| def set_locale
| |
| I18n.locale = params[:locale] || I18n.default_locale
| |
| end
| |
|
| |
| We can then pass the locale we want as a query param for example
| |
| http://example.com/books?locale=pt
| |
|
| |
| 4. Setting the locale from the domain name:
| |
| Similar to option 3 but instead of passing the locale as a query param, the locale is a part of the domain name itself.
| |
| Example http://example.de
| |
| In this case,
| |
| The set_locale function would look somethong like this
| |
| def set_locale
| |
| I18n.locale = extract_locale_from_tld || I18n.default_locale
| |
| end
| |
| def extract_locale_from_tld
| |
| parsed_locale = request.host.split('.').last
| |
| I18n.available_locales.include?(parsed_locale.to_sym) ? parsed_locale : nil
| |
| End
| |
|
| |
| If the application supports different locales, the menu would look something like this
| |
| link_to("Deutsch", "#{APP_CONFIG[:deutsch_website_url]}#{request.env['REQUEST_URI']}")
| |
|
| |
| 5. Setting the locale from the URL params:
| |
| • This can be a tedious task as we have to pass the locale on each request. The link would look something like this
| |
| link_to( books_url(locale: I18n.locale)))
| |
| • Rails provides us an alternative for "centralizing dynamic decisions about the URLs" in its ApplicationController#default_url_options and helper methods dependent on it (by implementing/overriding this method). This will now automatically include the locale param in the query string
| |
| • You probably want URLs to look like this: www.example.com/en/books (which loads the English locale) and www.example.com/nl/books (which loads the Dutch locale). This is achievable with the "over-riding default_url_options" strategy from above: you just have to set up your routes with scoping option in this way:
| |
| config/routes.rb
| |
| scope "/:locale" do
| |
| resources :books
| |
| end
| |
| This would need appending the locale in the URL path
| |
|
| |
| • If you don’t want to force the inclusion of locale in the URL path we can write
| |
| scope "(:locale)", locale: /en|nl/ do
| |
| resources :books
| |
| end
| |
| 6. Setting the locale from client supplied information
| |
| This can be done in 3 ways
| |
| • Using the default locale of the client browser
| |
| • Using client location to select the locale
| |
| • Saving the users choice of locale as a part of user profile
| |
|
| |
| Internationalizing Rails application:
| |
| After your application is prepared for internationalization, we need to use the feature in our application. This can be done in the following ways:
| |
| 1. Adding Translations: In order to internationalize rails code, replace the strings with calls to Rails' #t helper with a key that makes sense for the translation.
| |
| For example:
| |
| class HomeController < ApplicationController
| |
| def index
| |
| flash[:notice] = t(:hello_flash)
| |
| end
| |
| end
| |
|
| |
| <h1><%=t :hello_world %></h1>
| |
| <p><%= flash[:notice] %></p>
| |
|
| |
|
| |
|
| |
| References:
| |
| http://guides.rubyonrails.org/3_0_release_notes.html
| |
|
| |
| Resources
| |
| http://rubylearning.com/blog/2012/07/24/minimal-i18n-with-rails-3-2/
| |
| http://blog.lingohub.com/developers/2013/08/internationalization-for-ruby-i18n-gem/
| |