Created
June 25, 2018 15:39
-
-
Save thescientician/f3f21f30aa6521fe58692449d8150665 to your computer and use it in GitHub Desktop.
Compare two different i18n translation files for missing values
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
LOCALE_1 = "config/locales/fr.yml" | |
LOCALE_2 = "config/locales/en.yml" | |
require 'yaml' | |
def flatten_keys(hash, prefix="") | |
keys = [] | |
hash.keys.each do |key| | |
if hash[key].is_a? Hash | |
current_prefix = prefix + "#{key}." | |
keys << flatten_keys(hash[key], current_prefix) | |
else | |
keys << "#{prefix}#{key}" | |
end | |
end | |
prefix == "" ? keys.flatten : keys | |
end | |
def compare(locale_1, locale_2) | |
yaml_1 = YAML.load(File.open(File.expand_path(locale_1))) | |
yaml_2 = YAML.load(File.open(File.expand_path(locale_2))) | |
keys_1 = flatten_keys(yaml_1[yaml_1.keys.first]) | |
keys_2 = flatten_keys(yaml_2[yaml_2.keys.first]) | |
missing = keys_2 - keys_1 | |
file = locale_1.split('/').last | |
if missing.any? | |
puts "Missing from #{file}:" | |
missing.each { |key| puts " - #{key}" } | |
else | |
puts "Nothing missing from #{file}." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment