From a74cef5b9cb48a2b1c5ded0686c6197b2961f512 Mon Sep 17 00:00:00 2001 From: Ihor Breza Date: Thu, 15 Dec 2016 18:36:29 +0200 Subject: [PATCH] App can configure used locales (#135) --- CHANGELOG.md | 2 ++ README.rdoc | 17 +++++++++++++++++ lib/numbers_and_words/i18n/initialization.rb | 15 ++++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8b8d32a..6157129f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features * Your contribution here. + * App can configure used locales. \[[#135](https://github.com/kslazarev/numbers_and_words/issues/135)\] \([@seniorihor](https://github.com/seniorihor)\) \(assignee: [@seniorihor](https://github.com/seniorihor)\) + * Add option `precision` to `to_words`. \[[#134](https://github.com/kslazarev/numbers_and_words/issues/134)\] \([@neodelf](https://github.com/neodelf)\) \(assignee: [@neodelf](https://github.com/neodelf)\) ### Bugs diff --git a/README.rdoc b/README.rdoc index 992e5d68..7d5c19ad 100644 --- a/README.rdoc +++ b/README.rdoc @@ -250,6 +250,23 @@ I18n kütüphanesi ile sayıları yazıya çevirir. I18n.with_locale(:ru) { 0.1.to_words precision: 3 } => ноль целых и десять сотых +== Locales configuration / Конфигурация локалей + +You may optionally set locales you want to work with in your application. + +First, add to your Gemfile ("require: false" needed for config to take effect) + + gem 'numbers_and_words', require: false + +Then create your configuration in config/initializers/numbers_and_words.rb + + module NumbersAndWords + module I18n + APP_LOCALES = ["en", "ru"] # or use I18n.available_locales + end + end + require "numbers_and_words" + == Requirements / Требования / Configuration Requise * 1.9.3 <= Ruby (compatible with/совместимость с/compatible avec Ruby 1.9, JRuby and/и/et Rubinius); diff --git a/lib/numbers_and_words/i18n/initialization.rb b/lib/numbers_and_words/i18n/initialization.rb index 3be3ac13..de13660c 100644 --- a/lib/numbers_and_words/i18n/initialization.rb +++ b/lib/numbers_and_words/i18n/initialization.rb @@ -4,9 +4,22 @@ module Initialization extend self def init - I18n.locale_files.each { |locale_file| ::I18n.load_path << locale_file} + I18n.locale_files.each { |locale_file| ::I18n.load_path << locale_file if app_locale?(locale_file) } NumbersAndWords::I18n::Pluralization.init end + + private + + def app_locale?(file) + return true unless app_locales + + locale = file.split('.')[-2] + app_locales.include?(locale) + end + + def app_locales + @app_locales ||= (I18n::APP_LOCALES if I18n.const_defined?(:APP_LOCALES)) + end end end end