Created
April 1, 2015 18:35
-
-
Save GBH/d601b212e1e473c62e90 to your computer and use it in GitHub Desktop.
Testing for translation consistency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative '../test_helper' | |
class I18nIntegrationTest < ActionDispatch::IntegrationTest | |
def collect_combined_keys(hash, ns = nil) | |
hash.collect do |k, v| | |
keys = [ ] | |
keys << collect_combined_keys(v, "#{ns}.#{k}") if v.is_a?(Hash) | |
keys << "#{ns}.#{k}" | |
end.flatten | |
end | |
def test_translations | |
locales_path = File.expand_path('../../../config/locales', __FILE__) | |
locales = Dir.glob("#{locales_path}/*.yml").collect do |file_path| | |
File.basename(file_path, '.yml') | |
end | |
# collecting all locales | |
locale_keys = { } | |
locales.each do |locale| | |
translations = YAML.load_file("#{locales_path}/#{locale}.yml") | |
locale_keys[locale] = collect_combined_keys(translations[locale]) | |
end | |
# Using en as reference | |
reference = locale_keys[locales.delete('en')] | |
assert reference.present? | |
locale_keys.each do |locale, keys| | |
missing = reference - keys | |
assert missing.blank?, "#{locale} locale is missing: #{missing.join(', ')}" | |
extra = keys - reference | |
assert extra.blank?, "#{locale} locale has extra: #{extra.join(', ')}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment