Minimalistic I18n library for Ruby
MiniI18n is a simple and flexible Ruby Internationalization library. It supports interpolations, pluralization, fallbacks, nested keys and more.
Translations should be stored in YAML files and they will loaded in a in-memory Hash.
en:
hello: 'Hello'>> MiniI18n.t(:hello)
=> "Hello"Add this line to your application's Gemfile:
gem 'mini_i18n'And then execute:
> bundle install
Or install it yourself as:
> gem install mini_i18n
You should use the configure method to setup your environment:
MiniI18n.configure do |config|
# path to your translation files
config.load_translations(__dir__ + '/translations/*.yml')
# default locale
config.default_locale = :en
# available locales in your application
config.available_locales = [:en, :es, :fr]
# if given key is empty, defaults to the default_locale
config.fallbacks = true
endYou can also use the following format:
MiniI18n.load_translations(__dir__ + '/translations/*.yml')
MiniI18n.default_locale = :enBasic example usage:
>> MiniI18n.t(:hello)
=> "Hello"
>> MiniI18n.t(:hello, locale: :fr)
=> "Bonjour"
>> MiniI18n.locale = :fr
=> :fr
>> MiniI18n.t(:hello)
=> "Bonjour"
>> MiniI18n.t(:hellooo)
=> nilThe t method can be also used as translate:
MiniI18n.translate(:hello)It accepts the following options:
locale
>> MiniI18n.t(:hello, locale: :es)
=> "Hola"scope
>> MiniI18n.t('application.views.welcome')
=> "Welcome"
>> MiniI18n.t('welcome', scope: 'application.views')
=> "Welcome"default
>> MiniI18n.t(:hellooo, default: 'default value')
=> "default value"count
Read more details in the Pluralization section.
>> MiniI18n.t('notifications', count: 0)
=> "no unread notifications"You can also use variables in your translation definitions:
en:
hello_with_name: "Hello %{name}!">> MiniI18n.t(:hello_with_name, name: 'John Doe')
=> "Hello John Doe!"You should define your plurals in the following format (supported keys: zero, one and many):
en:
notifications:
zero: 'no unread notifications'
one: '1 unread notification'
many: '%{count} unread notifications'Then, you should call the method with the count option:
>> MiniI18n.t('notifications', count: 0)
=> "no unread notifications"
>> MiniI18n.t('notifications', count: 1)
=> "1 unread notification"
>> MiniI18n.t('notifications', count: 5)
=> "5 unread notifications"Any kind of feedback, bug report, idea or enhancement are much appreciated.
To contribute, just fork the repo, hack on it and send a pull request. Don't forget to add specs for behaviour changes and run the test suite:
> bundle exec rspec
Copyright (c) Marc Anguera. MiniI18n is released under the MIT License.