Projects that support several languages often have centralized lists of string translations for each language that they support. In the case of the Globalite plugin, these translates would be in YAML files. By default, of course. Within the views themselves, translatable strings would be replaced by symbols, and each of these symbols would supposedly have a corresponding key-value mapping in each language file.
Dealing with translations without a straightforward system for managing duplicates and additions to the files would commonly require removing duplicate key mappings in a file, finding duplicate key-value mappings between two files, and finding key mappings in one file that are not in a second file.
Hurray, the YAML library:
# Load YAML
require "yaml"
# Load language file to a hash. Hashes store only one value for a key, so if
# there are more than one declarations for a key in a single file, only the
# last one is used.
en_us = YAML::load("lang/ui/en-US.yml")
de_de = YAML::load("lang/ui/de-DE.yml")
# Find common key-value mappings between two files.
duplicates = Hash.new
en_us.each_pair do |key, value|
if de_de[key] == value
duplicates[key] = value
end
end
# Find key declarations in one file that are not in a second file.
missing = Hash.new
en_us.each_pair do |key, value|
if de_de[key].nil?
duplicates[key] = value
end
end
# Write hash to YAML file.
File.open("new.yml", "w") do |out|
YAML.dump(yaml_hash, out)
end
Tags: localization, plugins, Ruby, ruby on rails