Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Configuring i18N Api in rails internationalization

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 159
    Comment on it


    Rails Internationalization : Configure the i18n Module

    As we all aware that whenever we use term web, it refers to globalization of application. For globalization, your application must support multi-language support. In ruby i18n provides the support for translating your application to a single custom language other than English. It is a complex problem to achieve internationalization, rails i18n focuses on:

    • providing support for English and similar languages out of the box.
    • making it easy to customize and extend everything for other languages.

    To achive this Ruby i18n gem spilt in two parts:

    (a) public API of the i18n framework: Ruby module with public methods that define how the library works.
    (b) A default backend: It implements these methods.

    Now lets switch to the real task of how to configure i18n module in rails app. Rails sets up application with reasonable defaults, that you can override. Rails adds all .rb and .yml files from config/locales directory to your translations load path automatically. The default file available there, is en.yml for english. If you look into this you will find:

    en:
      hello: "Hello World"
    


    Here key hello maps to the string "Hello World". If you want other languages, you can add them too here. Thus those languages will also be added to translations load path. You can get a list of those files here at https://github.com/svenfuchs/rails-i18n/tree/master/rails locale. You can also use YAML or standard Ruby Hashes to store translations in the default backend.
    The translation load path is just a ruby array of paths to be automatically loaded and added to the application. To load locals from another directly, application.rb file provides instructions. For that uncomment the given files in application.rb and edit them.

    # 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
    

    There is another way of configuring i18n, without even touching application.rb. You need to set the load path anywhere in the application, where it can run before the translations are looked up. Best thing is to put them into initializers.

    # in config/initializers/locale.rb
     
    # tell the I18n library where to find your translations
    I18n.load_path += Dir[Rails.root.join('lib', 'locale', '*.{rb,yml}')]
     
    # set default locale to something other than :en
    I18n.default_locale = :pt
    


    Hope you liked this, For more on internationalization and rails, Click here

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: