Skip to content

Commit

Permalink
Update :placeholders share example
Browse files Browse the repository at this point in the history
- previously, we mistakenly assumed that in languages which do not use
plurals, the translation would always contain placeholders matching
the English singular form
- for languages, which do not use plurals, we'll be watching for
mistakenly added plural forms
  • Loading branch information
mzazrivec committed Aug 30, 2019
1 parent 8c51965 commit e88d1bc
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions spec/shared/i18n/placeholders.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
shared_examples :placeholders do |dir|
it "translations preserve placeholders in strings" do
errors = {}
false_negatives = {}
incorrect_plurals = []

Pathname.glob(File.join(dir, "**", "*.po")).each do |po_file|
po = FastGettext::PoFile.new(po_file)
locale = po_file.dirname.basename.to_s
false_negatives[po_file.to_s] = []
next if locale == 'en' # There's no need to test english .po

po.data.each do |original, translation|
next if translation.nil?
# Chinese translations do not have plural forms
original = original.split("\u0000").first if locale == 'zh_CN' && original.present?
placeholders = original.scan(/%{\w+}/)
placeholders.sort!
next if placeholders.empty?
translated_placeholders = translation.scan(/%{\w+}/)
translated_placeholders.sort!
if placeholders.uniq != translated_placeholders.uniq
errors.store_path(po_file.to_s, original, translation)
next if translation.nil? || !original.include?("%{") # Skip if string is not translated or original does not contain %{}

if %w[ja zh_CN].include?(locale) && original.include?("\u0000") # Chinese and Japanese translations do not have plural forms
if translation.include?("\u0000") # there should be only one translated form
incorrect_plurals << translation
next
end
singular, plural = original.split("\u0000").map { |str| str.scan(/%{\w+}/).sort.uniq }
translated_placeholders = translation.scan(/%{\w+}/).sort!.uniq
if singular != translated_placeholders && plural != translated_placeholders
errors.store_path(po_file.to_s, original, translation)
end
false_negatives[po_file.to_s].append(original.split("\u0000").first)
else
origin = original.scan(/%{\w+}/).sort.uniq
transl = translation.scan(/%{\w+}/).sort.uniq
if origin != transl && !false_negatives[po_file.to_s].include?(original)
errors.store_path(po_file.to_s, original, translation)
end
end
end
end
Expand All @@ -31,13 +44,15 @@
end
end
expect(errors).to be_empty
expect(incorrect_plurals).to be_empty
end

it "gettext strings do not contain interpolations" do
errors = []
Pathname.glob(File.join(dir, "**", "*.pot")).each do |pot_file|
File.open(pot_file).each do |line|
next unless line =~ /^.*(".*\#\{[.\w]+\}.*")$/

errors.push($1)
end
end
Expand Down

0 comments on commit e88d1bc

Please sign in to comment.