All Projects → markets → mini_i18n

markets / mini_i18n

Licence: MIT license
🌐 Minimalistic I18n library for Ruby

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to mini i18n

pH7-Internationalization
🎌 pH7CMS Internationalization (I18N) package 🙊 Get new languages for your pH7CMS website!
Stars: ✭ 17 (-81.72%)
Mutual labels:  i18n, translations, internationalization
I18n Editor
GUI for editing your i18n translation files
Stars: ✭ 290 (+211.83%)
Mutual labels:  i18n, translations, internationalization
Eslint Plugin I18n Json
Fully extendable eslint plugin for JSON i18n translation files.
Stars: ✭ 101 (+8.6%)
Mutual labels:  i18n, translations, internationalization
Mojito
An automation platform that enables continuous localization.
Stars: ✭ 256 (+175.27%)
Mutual labels:  i18n, translations, internationalization
Flutter translate
Flutter Translate is a fully featured localization / internationalization (i18n) library for Flutter.
Stars: ✭ 245 (+163.44%)
Mutual labels:  i18n, translations, internationalization
react-mobx-router
Create React App with React Router 4 and MobX + Internationalization
Stars: ✭ 90 (-3.23%)
Mutual labels:  i18n, internationalization
doorkeeper-i18n
Translation files for Doorkeeper OAuth 2 provider
Stars: ✭ 30 (-67.74%)
Mutual labels:  i18n, internationalization
go-localize
i18n (Internationalization and localization) engine written in Go, used for translating locale strings.
Stars: ✭ 45 (-51.61%)
Mutual labels:  i18n, internationalization
typesafe-i18n
A fully type-safe and lightweight internationalization library for all your TypeScript and JavaScript projects.
Stars: ✭ 1,227 (+1219.35%)
Mutual labels:  i18n, internationalization
msgtools
Tools for Developing Diagnostic Messages
Stars: ✭ 18 (-80.65%)
Mutual labels:  i18n, internationalization
deepl-php-lib
🧠 DeepL API Client Library supporting PHP >= 7.3
Stars: ✭ 50 (-46.24%)
Mutual labels:  i18n, translations
ad localize
ADLocalize is a simple way to manage your localization files. Supported wording sources : CSVs and Google Sheets. Localization file generation available for iOS, Android, JSON (i18next), YAML and Java properties
Stars: ✭ 22 (-76.34%)
Mutual labels:  i18n, internationalization
laminas-i18n
Provide translations for your application, and filter and validate internationalized values
Stars: ✭ 40 (-56.99%)
Mutual labels:  i18n, internationalization
awesome-i18n
🌍 A curated list of i18n resources for all kind of languages and frameworks
Stars: ✭ 205 (+120.43%)
Mutual labels:  i18n, internationalization
I18N-Portable
Simple and cross platform internationalization/translations for Xamarin and .NET
Stars: ✭ 102 (+9.68%)
Mutual labels:  translations, internationalization
React Native Globalize
Internationalization (i18n) for React Native
Stars: ✭ 246 (+164.52%)
Mutual labels:  i18n, internationalization
i18n lazy scope
Use lazy lookup with custom i18n scopes.
Stars: ✭ 11 (-88.17%)
Mutual labels:  i18n, internationalization
rails
Rails translation made _('simple').
Stars: ✭ 65 (-30.11%)
Mutual labels:  i18n, internationalization
Domino-English-Translation
🌏 Let's translate Domino, a Japanese MIDI editor!
Stars: ✭ 29 (-68.82%)
Mutual labels:  i18n, translations
A18n
Automated I18n solution for JavaScript/TypeScript/React
Stars: ✭ 244 (+162.37%)
Mutual labels:  i18n, internationalization

MiniI18n

Gem Build Status Maintainability

Minimalistic I18n library for Ruby

MiniI18n is a simple, flexible and fast Ruby Internationalization library. It supports localization, interpolations, pluralization, fallbacks, nested keys and more.

Translations should be stored in YAML or JSON files and they will be loaded in an 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/*')

  # Default locale.
  config.default_locale = :pt

  # Available locales in your application.
  config.available_locales = [:en, :es, :fr, :pt]

  # If given key is empty, defaults to the default_locale.
  config.fallbacks = true

  # Custom separator for nested keys.
  config.separator = '/'

  # Custom pluralization rules, by locale.
  config.pluralization_rules = {
    es: -> (n) { n == 0 ? 'zero' : 'other' },
    fr: -> (n) { ... }
  }
end

You can also use the following format:

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

Examples usage:

>> MiniI18n.t(:hello)
=> "Hello"
>> MiniI18n.t(:hello, locale: :fr)
=> "Bonjour"
>> MiniI18n.locale = :fr
=> :fr
>> MiniI18n.t(:hello)
=> "Bonjour"
>> MiniI18n.t(:non_existent_key)
=> nil
>> MiniI18n.t([:hello, :bye])
=> ["Hello", "Bye"]
>> MiniI18n.t('app.controllers.not_found')
=> "Not found!"

The t method can be also used as translate:

MiniI18n.translate(:hello)

It accepts the following options:

  • locale
>> MiniI18n.t(:hello, locale: :es)
=> "Hola"

You can also get multiple locales at once by passing an array:

>> MiniI18n.t(:hello, locale: [:en, :fr, :es])
=> ["Hello", "Bonjour", "Hola"]
  • scope
>> MiniI18n.t('application.views.welcome')
=> "Welcome"
>> MiniI18n.t('welcome', scope: 'application.views')
=> "Welcome"

Read more details about nested keys in this section.

  • default
>> MiniI18n.t(:non_existent_key, default: 'default value')
=> "default value"
  • count
>> MiniI18n.t('notifications', count: 0)
=> "no unread notifications"

Read more details in the Pluralization section.

Nested Keys

You can use a custom separator when accessing nested keys (default separator is .):

en:
  app:
    controllers:
      not_found: "Not found!"
MiniI18n.t('app.controllers.not_found')
MiniI18n.separator = '/'
MiniI18n.t('app/controllers/not_found')

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 (default pluralization rule accepts the keys: zero, one and other):

en:
  notifications:
    zero: 'good job! no new notifications'
    one: '1 unread notification'
    other: '%{count} unread notifications'

Then, you should call the method with the count option:

>> MiniI18n.t('notifications', count: 0)
=> "good job! no new notifications"
>> MiniI18n.t('notifications', count: 1)
=> "1 unread notification"
>> MiniI18n.t('notifications', count: 5)
=> "5 unread notifications"

Custom pluralization rules

You are also able to customize how plurals are handled, by locale, defining custom pluralization rules. Example:

MiniI18n.pluralization_rules = {
  es: -> (n) {
    if n == 0
      'zero'
    elsif (1..3).include?(n)
      'few'
    elsif (4..10).include?(n)
      'many'
    else
      'other'
    end
  }
}

Now, in your translation files, you should define content for those keys:

es:
  notifications:
    zero: 'no tienes nuevas notificaciones'
    few: 'tienes algunas notificaciones pendientes ...'
    many: 'tienes %{count} notificaciones!'
    other: 'alerta!! %{count} notificaciones!'

And then, you get:

>> MiniI18n.t('notifications', count: 0)
=> "no tienes nuevas notificaciones"
>> MiniI18n.t('notifications', count: 2)
=> "tienes algunas notificaciones pendientes ..."
>> MiniI18n.t('notifications', count: 5)
=> "tienes 5 notificaciones!"
>> MiniI18n.t('notifications', count: 20)
=> "alerta!! 20 notificaciones!"

Localization

You can also use the MiniI18n.l (or the long version MiniI18n.localize) method to localize your dates, time and numbers.

Dates and time

It uses strftime under the hood. You should provide your localizations using the following format:

en:
  date:
    formats:
      default: "%A %d, %B, %Y"
      short: "%d %b %y"
>> MiniI18n.l(Date.new(2018, 8, 15))
=> "Wednesday 15, August, 2018"
>> MiniI18n.l(Date.new(2018, 8, 15), format: :short)
=> "15 Aug 18"

You can check a full example of all necessary and useful keys in this file.

Numbers

To localize your numbers, you can provide the following keys:

en:
  number:
    format:
      delimiter: ','
      separator: '.'
    as:
      currency: '%{number} $'
>> MiniI18n.l(1000.25)
=> "1,000.25"
>> MiniI18n.l(1000, as: :currency)
=> "1,000 $"
>> MiniI18n.l(1000, as: :currency, locale: :es)
=> "1.000 €"

TIP By using the :as option you can build custom full sentences with formatted numbers, like:

en:
  number:
    as:
      final_price: 'Final price: %{number} $'
      percentage: '%{number}%'
>> MiniI18n.l(1000, as: :final_price)
=> "Final price: 1,000 $"
>> MiniI18n.l(70.5, as: :percentage)
=> "70.5%"

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].