8000 GitHub - muhammadyana/mini_i18n: 🌐 Minimalistic I18n library for Ruby · GitHub
[go: up one dir, main page]

Skip to content
muhammadyanaPublic
forked from markets/mini_i18n

About

🌐 Minimalistic I18n library for Ruby

Resources

Stars

0 stars

Watchers

1 watching

Forks

 
 

Latest commit

 

History

24 Commits

Folders and files

Repository files navigation

MiniI18n

Gem Build Status

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"

Installation

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

Usage

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
end

You can also use the following format:

MiniI18n.load_translations(__dir__ + '/translations/*.yml')
MiniI18n.default_locale = :en

Basic example usage:

>> MiniI18n.t(:hello)
=> "Hello"
>> MiniI18n.t(:hello, locale: :fr)
=> "Bonjour"
>> MiniI18n.locale = :fr
=> :fr
>> MiniI18n.t(:hello)
=> "Bonjour"
>> MiniI18n.t(:hellooo)
=> nil

The 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"

Interpolation

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!"

Pluralization

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"

Development

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

License

Copyright (c) Marc Anguera. MiniI18n is released under the MIT License.

About

🌐 Minimalistic I18n library for Ruby

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

0