Skip to content

Commit

Permalink
Merge pull request #122 from e-travel/gettext_plural_keys_access
Browse files Browse the repository at this point in the history
Access to Gettext.plural_keys
  • Loading branch information
radar committed May 14, 2017
2 parents 1ba0ac3 + 30553d0 commit b04a982
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/i18n/gettext.rb
Expand Up @@ -8,11 +8,12 @@ module Gettext
@@plural_keys = { :en => [:one, :other] }

class << self
# returns an array of plural keys for the given locale so that we can
# convert from gettext's integer-index based style
# returns an array of plural keys for the given locale or the whole hash
# of locale mappings to plural keys so that we can convert from gettext's
# integer-index based style
# TODO move this information to the pluralization module
def plural_keys(locale)
@@plural_keys[locale] || @@plural_keys[:en]
def plural_keys(*args)
args.empty? ? @@plural_keys : @@plural_keys[args.first] || @@plural_keys[:en]
end

def extract_scope(msgid, separator)
Expand Down
20 changes: 20 additions & 0 deletions test/i18n/gettext_plural_keys_test.rb
@@ -0,0 +1,20 @@
require 'test_helper'

class I18nGettextPluralKeysTest < I18n::TestCase
def setup
super
I18n::Gettext.plural_keys[:zz] = [:value1, :value2]
end

test "Returns the plural keys of the given locale if present" do
assert_equal I18n::Gettext.plural_keys(:zz), [:value1, :value2]
end

test "Returns the plural keys of :en if given locale not present" do
assert_equal I18n::Gettext.plural_keys(:yy), [:one, :other]
end

test "Returns the whole hash with no arguments" do
assert_equal I18n::Gettext.plural_keys, { :en => [:one, :other], :zz => [:value1, :value2] }
end
end

0 comments on commit b04a982

Please sign in to comment.