diff --git a/CHANGELOG.md b/CHANGELOG.md index e015365..2c4c067 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# Unreleased + +Enables extra plural rules to be used outside of Rails Translation Manager + # 1.5.2 Add missing plurals for Gujarati and Yiddish https://github.com/alphagov/rails_translation_manager/pull/44 diff --git a/config/locales/plurals.rb b/config/locales/plurals.rb deleted file mode 100644 index 80996c1..0000000 --- a/config/locales/plurals.rb +++ /dev/null @@ -1,64 +0,0 @@ -# frozen_string_literal: true - -{ - # Welsh - cy: { i18n: { plural: { keys: %i[zero one two few many other], - rule: - lambda do |n| - case n - when 0 then :zero - when 1 then :one - when 2 then :two - when 3 then :few - when 6 then :many - else :other - end - end } } }, - # Dari - this isn't an iso code. Probably should be 'prs' as per ISO 639-3. - dr: { i18n: { plural: { keys: %i[one other], rule: ->(n) { n == 1 ? :one : :other } } } }, - # Latin America and Caribbean Spanish - "es-419": { i18n: { plural: { keys: %i[one other], rule: ->(n) { n == 1 ? :one : :other } } } }, - # Gujarati - gu: { i18n: { plural: { keys: %i[one other], rule: ->(n) { [0, 1].include?(n) ? :one : :other } } } }, - # Scottish Gaelic - gd: { i18n: { plural: { keys: %i[one two few other], - rule: lambda do |n| - if [1, 11].include?(n) - :one - elsif [2, 12].include?(n) - :two - elsif [3, 4, 5, 6, 7, 8, 9, 10, 13, 14, 15, 16, 17, 18, 19].include?(n) - :few - else - :other - end - end } } }, - # Armenian - hy: { i18n: { plural: { keys: %i[one other], rule: ->(n) { [0, 1].include?(n) ? :one : :other } } } }, - # Kazakh - kk: { i18n: { plural: { keys: %i[one other], rule: ->(n) { n == 1 ? :one : :other } } } }, - # Pashto - ps: { i18n: { plural: { keys: %i[one other], rule: ->(n) { n == 1 ? :one : :other } } } }, - # Punjabi Shahmukhi - "pa-pk": { i18n: { plural: { keys: %i[one other], rule: ->(n) { [0, 1].include?(n) ? :one : :other } } } }, - # Sinhalese - si: { i18n: { plural: { keys: %i[one other], rule: ->(n) { [0, 1].include?(n) ? :one : :other } } } }, - # Somali - so: { i18n: { plural: { keys: %i[one other], rule: ->(n) { n == 1 ? :one : :other } } } }, - # Albanian - sq: { i18n: { plural: { keys: %i[one other], rule: ->(n) { n == 1 ? :one : :other } } } }, - # Norwegian - no: { i18n: { plural: { keys: %i[one other], rule: ->(n) { n == 1 ? :one : :other } } } }, - # Tamil - ta: { i18n: { plural: { keys: %i[one other], rule: ->(n) { n == 1 ? :one : :other } } } }, - # Turkmen - tk: { i18n: { plural: { keys: %i[one other], rule: ->(n) { n == 1 ? :one : :other } } } }, - # Uzbek - uz: { i18n: { plural: { keys: %i[one other], rule: ->(n) { n == 1 ? :one : :other } } } }, - # Yiddish - yi: { i18n: { plural: { keys: %i[one other], rule: ->(n) { [0, 1].include?(n) ? :one : :other } } } }, - # Chinese Hong Kong - 'zh-hk' => { i18n: { plural: { keys: %i[other], rule: -> { :other } } } }, - # Chinese Taiwan - 'zh-tw' => { i18n: { plural: { keys: %i[other], rule: -> { :other } } } } -} diff --git a/lib/rails_translation_manager.rb b/lib/rails_translation_manager.rb index 9100c13..7efc77c 100644 --- a/lib/rails_translation_manager.rb +++ b/lib/rails_translation_manager.rb @@ -18,14 +18,14 @@ require "rails_translation_manager/cleaner" require "rails_translation_manager/exporter" require "rails_translation_manager/importer" +require "rails_translation_manager/plurals" module RailsTranslationManager rails_i18n_path = Gem::Specification.find_by_name("rails-i18n").gem_dir - rails_translation_manager = Gem::Specification.find_by_name("rails_translation_manager").gem_dir I18n.load_path.concat( Dir["#{rails_i18n_path}/rails/pluralization/*.rb"], - ["#{rails_translation_manager}/config/locales/plurals.rb"] + ["lib/rails_translation_manager/plurals.rb"] ) def self.locale_root diff --git a/lib/rails_translation_manager/plurals.rb b/lib/rails_translation_manager/plurals.rb new file mode 100644 index 0000000..0f0d5c9 --- /dev/null +++ b/lib/rails_translation_manager/plurals.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +module RailsTranslationManager + class Plurals + def self.extra_rules + { + # Welsh + cy: { i18n: { plural: { keys: %i[zero one two few many other], + rule: + lambda do |n| + case n + when 0 then :zero + when 1 then :one + when 2 then :two + when 3 then :few + when 6 then :many + else :other + end + end } } }, + # Dari - this isn't an iso code. Probably should be 'prs' as per ISO 639-3. + dr: { i18n: { plural: { keys: %i[one other], rule: ->(n) { n == 1 ? :one : :other } } } }, + # Latin America and Caribbean Spanish + "es-419": { i18n: { plural: { keys: %i[one other], rule: ->(n) { n == 1 ? :one : :other } } } }, + # Gujarati + gu: { i18n: { plural: { keys: %i[one other], rule: ->(n) { [0, 1].include?(n) ? :one : :other } } } }, + # Scottish Gaelic + gd: { i18n: { plural: { keys: %i[one two few other], + rule: lambda do |n| + if [1, 11].include?(n) + :one + elsif [2, 12].include?(n) + :two + elsif [3, 4, 5, 6, 7, 8, 9, 10, 13, 14, 15, 16, 17, 18, 19].include?(n) + :few + else + :other + end + end } } }, + # Armenian + hy: { i18n: { plural: { keys: %i[one other], rule: ->(n) { [0, 1].include?(n) ? :one : :other } } } }, + # Kazakh + kk: { i18n: { plural: { keys: %i[one other], rule: ->(n) { n == 1 ? :one : :other } } } }, + # Pashto + ps: { i18n: { plural: { keys: %i[one other], rule: ->(n) { n == 1 ? :one : :other } } } }, + # Punjabi Shahmukhi + "pa-pk": { i18n: { plural: { keys: %i[one other], rule: ->(n) { [0, 1].include?(n) ? :one : :other } } } }, + # Sinhalese + si: { i18n: { plural: { keys: %i[one other], rule: ->(n) { [0, 1].include?(n) ? :one : :other } } } }, + # Somali + so: { i18n: { plural: { keys: %i[one other], rule: ->(n) { n == 1 ? :one : :other } } } }, + # Albanian + sq: { i18n: { plural: { keys: %i[one other], rule: ->(n) { n == 1 ? :one : :other } } } }, + # Norwegian + no: { i18n: { plural: { keys: %i[one other], rule: ->(n) { n == 1 ? :one : :other } } } }, + # Tamil + ta: { i18n: { plural: { keys: %i[one other], rule: ->(n) { n == 1 ? :one : :other } } } }, + # Turkmen + tk: { i18n: { plural: { keys: %i[one other], rule: ->(n) { n == 1 ? :one : :other } } } }, + # Uzbek + uz: { i18n: { plural: { keys: %i[one other], rule: ->(n) { n == 1 ? :one : :other } } } }, + # Yiddish + yi: { i18n: { plural: { keys: %i[one other], rule: ->(n) { [0, 1].include?(n) ? :one : :other } } } }, + # Chinese Hong Kong + 'zh-hk' => { i18n: { plural: { keys: %i[other], rule: -> { :other } } } }, + # Chinese Taiwan + 'zh-tw' => { i18n: { plural: { keys: %i[other], rule: -> { :other } } } } + } + end + end +end + +RailsTranslationManager::Plurals.extra_rules