Skip to content

Commit

Permalink
Add test for locales - to find problematic keys (#752)
Browse files Browse the repository at this point in the history
  • Loading branch information
benkoshy authored Dec 8, 2024
1 parent 1472557 commit 5df4074
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
2 changes: 2 additions & 0 deletions gem/locales/sw.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ sw:
pagy:
# please add a comment in the https://github.com/ddnexus/pagy/issues/603
# posting the translation of the following "Page"/"Pages" with the plurals for this locale
# Please change the final test in 18n_loacles_test.rb to remove the sw.yml exclusion
# after you make these changes.
aria_label:
nav: "Pages"
# one: ""
Expand Down
2 changes: 2 additions & 0 deletions gem/locales/ta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ ta:
aria_label:
# please add a comment in the https://github.com/ddnexus/pagy/issues/604
# posting the translation of the following "Page"/"Pages" with the plurals for this locale
# Please change the final test in 18n_loacles_test.rb to remove the ta.yml exclusion
# after you make these changes.
nav: "Pages"
# one: ""
# other: ""
Expand Down
28 changes: 22 additions & 6 deletions test/pagy/i18n_locales_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
Pagy.root.join('locales').each_child do |f|
next unless f.extname == '.yml'

message = "locale file #{f}"
locale = f.basename.to_s[0..-5]
comment = f.readlines.first.to_s.strip
rule = comment.to_s.split[1][1..].to_s.to_sym
message = "locale file #{f}"
locale = f.basename.to_s[0..-5] # e.g. de
comment = f.readlines.first.to_s.strip # e.g. :one_other pluralization (see https://github.com/ddnexus/pagy/blob/master/gem/lib/pagy/i18n.rb)
rule = comment.to_s.split[1][1..].to_s.to_sym # e.g. one_other
language_yml = YAML.safe_load(f.read)

it 'includes a comment with the pluralization rule and the i18n.rb reference' do
_(rules).must_include rule, message
Expand All @@ -34,8 +35,7 @@
_(Pagy::I18n::P11n::LOCALE[locale]).must_equal Pagy::I18n::P11n::RULE[rule], message
end
it 'pluralizes item_name according to the rule' do
hash = YAML.safe_load(f.read)
item_name = hash[locale]['pagy']['item_name']
item_name = language_yml[locale]['pagy']['item_name']
case item_name
when String
_(rule).must_equal :other
Expand All @@ -45,5 +45,21 @@
raise StandardError, "item_name must be Hash or String"
end
end
it "ensures #{locale}.yml has the correct aria_label,nav and item_name keys per the declared (#{rule}) rule" do
skip if %w[ta sw].include?(locale) # ta.yml and sw.yml do not have the requisite keys yet

pluralizations = counts[rule]

if rule == :other
# For the :other rules, we should not have any keys under the
# ['pagy']['item_name'] and ['pagy']['aria_label']['nav'] hierarchies.
# We should just have a String.
_(language_yml[locale]['pagy']['item_name']).must_be_instance_of(String)
_(language_yml[locale]['pagy']['aria_label']['nav']).must_be_instance_of(String)
else
_(language_yml[locale]['pagy']['item_name'].keys.sort).must_equal pluralizations.sort, "In #{message} - check that ['pagy']['item_name'] does not have keys inconsistent with #{rule}"
_(language_yml[locale]['pagy']['aria_label']['nav'].keys.sort).must_equal pluralizations.sort, "In #{message} - check that ['pagy']['aria_label']['nav'] does not have keys inconsistent with #{rule}"
end
end
end
end

0 comments on commit 5df4074

Please # to comment.