rails-i18n Documentation

repository·master·Indexed 26 days ago

https://github.com/svenfuchs/rails-i18n

A centralized repository for Ruby on Rails locale data providing translations, pluralization rules, transliteration, and ordinals for a wide range of languages. Supports Rails 2.x through 8.1.0, offering configurable modules for locales, ordinals, pluralization, and transliteration.

Tokens
1.3K
Snippets
6
Records
12
Agent score
74%

What's inside rails-i18n

  1. Install the rails-i18n gem

    master

    Install rails-i18n via your Gemfile or the command line based on your Ruby on Rails version. Note that Rails 3.0 or higher is required for gem installation. For Rails 2.x, use the Manual Installation method.

    Gemfile examples

    # For Rails >= 8.1.0
    gem 'rails-i18n', '~> 8.1.0'
    
    # For Rails >= 7.0.0
    gem 'rails-i18n', '~> 7.0.0'
    
    # For 6.x
    gem 'rails-i18n', '~> 6.0'
    
    # For 5.x
    gem 'rails-i18n', github: 'svenfuchs/rails-i18n', branch: 'rails-5-x'

    CLI installation examples

    gem install rails-i18n -v '~> 8.1.0' # For Rails >= 8.1.0
    gem install rails-i18n -v '~> 6.0'   # For 6.x
    gem 'rails-i18n', '~> 8.1.0'
  2. Manually install locale files

    master

    For Rails 2.x or custom setups, you can manually install locale data:

    1. Download the desired locale files from the rails/locale directory in the rails-i18n repository.
    2. Move them into your Rails application's config/locales directory.
    3. Edit or add your own locale files as needed.
  3. Test locale file integrity and completeness

    master

    When contributing to rails-i18n, use the following commands to ensure your locale files are valid and complete. These commands assume you are running within the repository environment (e.g., via bundle exec or Docker).

    Run tests

    bundle exec rake spec

    Check completeness against English

    bundle exec rake i18n-spec:completeness rails/locale/en.yml rails/locale/YOUR_NEW_LOCALE.yml

    Normalize locale files

    thor locales:normalize LOCALE
    # or
    thor locales:normalize_all

    List locale status

    thor locales:complete
    thor locales:incomplete
    thor locales:list
    bundle exec rake i18n-spec:completeness rails/locale/en.yml rails/locale/YOUR_NEW_LOCALE.yml
  4. Use Docker for rails-i18n testing

    master

    You can build and run the rails-i18n test suite using Docker.

    Build the image

    docker build --tag=railsi18n .

    Run tests

    docker run railsi18n

    Run completeness checks via Docker

    docker run railsi18n bundle exec rake i18n-spec:completeness rails/locale/en.yml rails/locale/YOUR_NEW_LOCALE.yml
    docker build --tag=railsi18n .
  5. Configure enabled modules in rails-i18n

    master

    By default, all modules (:locale, :ordinals, :pluralization, and :transliteration) are enabled. You can restrict the gem's loaded features by setting config.rails_i18n.enabled_modules in your Rails configuration.

    Available module names:

    • :locale
    • :ordinals
    • :pluralization
    • :transliteration
    # to enable only pluralization rules, but disable all other features
    config.rails_i18n.enabled_modules = [:pluralization]
    
    # to enable pluralization and ordinals
    config.rails_i18n.enabled_modules = [:pluralization, :ordinals]
  6. Configure available locales

    master

    The rails-i18n gem initially loads all available locale files, pluralization, and transliteration rules. To limit which locales are loaded, use the I18n.available_locales option in your environment configuration (e.g., config/environments/*.rb).

    config.i18n.available_locales = ['es-CO', :de]
  7. Override currency symbols in locales

    master

    Some locales use Unicode currency symbols (e.g., ) while others use codes (e.g., CHF) under the key number.currency.format.unit. To override this behavior, create a custom locale file in config/locales/ (e.g., config/locales/tr.yml) and define the specific key.

    tr:
      number:
        currency:
          format:
            unit: TL
  8. Configure enabled modules for rails-i18n

    master
    By default, rails-i18n enables all available modules: :locale, :pluralization, :ordinals, and :transliteration. You can customize which features are loaded by setting the enabled_modules key in your Rails configuration. This must be done before the rails-i18n initializer runs.
  9. Use Ukrainian transliteration rules

    master
    The RailsI18n::Transliteration::Ukrainian.rule method provides a lambda that implements transliteration rules for the Ukrainian language. It handles complex character mappings (like 'Щ' to 'SHCH'), case sensitivity based on surrounding characters, and specific handling for apostrophes within words.