I18nKit
You can import XML files that were exported using Excel, or standard i18n hierarchical YML files. Both will be normalized as I18nKit::Document objects so that they can be compared with each other. You need implement the comparison method yourself, however. I18nKit only provides the Iterator.
Importing
Say you have an Excel file like this
1 | key | english | swedish | german | comments |
2 | customer.invoice.create | Create invoice | Skapa intyg | Rechnung anlegen | |
3 | customer.invoice.delete | Delete invoice | Radera intyg | Rechnung löschen | Wow... |
and you export it as XML. Then you can import it with I18nKit like this:
require 'i18n_kit'
documents = I18nKit::Importer::ExcelXML.new('my_export.xml').parse
# Or use the shortcut:
documents = I18nKit.import_excel("'my_export.xml')
you could also pass the file as a String:
documents = I18nKit::Importer::ExcelXML.new(File.read('my_export.xml')).parse
The first row will be used to identify the locale language. Note that there are more options for how to import Excel files depending on whether there is a headline or not. Or whether you want to import only the first X columns, like so:
importer = I18nKit::Importer::ExcelXML.new(XML_PATH, :ignore_headline => true, :locales => [:sv, :en, :fi])
That will ignore the last column (comments) and use “sv” instead of “swedish”, for example.
At any rate, documents
will hold an Array of I18nKit::Document
objects. Each representing one language. You might want to give them names so that you can compare them better later:
documents[0] = 'Excel: English'
documents[1] = 'Excel: Swedish'
documents[2] = 'Excel: German'
# Note: By default they're called :excel_en, :excel_sv, etc...
Now let’s import a YML file from your Rails project:
more_documents = I18nKit::Importer::YAML.new('config/locales/fi.yml').parse
more_documents[0].name = 'My Rails App: Finnish'
# Or just use the shortcut:
I18nKit.import_yaml("config/locales/fi.yml")
# Note: The default document name is: :yaml_fi
What can you do with a I18nKit::Document
entity? If you used the Excel import, then you have e.g. these instance methods:
document.name => 'Excel: German'
document.locale => 'de'
document.to_flat_hash => { 'customer.invoice.create' => 'Rechnung anlegen',
'customer.invoice.delete' => 'Rechnung löschen' }
document.keys => ['customer.invoice.create', 'customer.invoice.delete']
You can do the same with files imported from YML, but additionally you have:
document.to_hierarchical_hash => { 'customer' => { 'invoice' => { 'create' => 'Rechnung anlegen', etc... } } }
document.to_i18n_hash => { 'de' => { 'customer' => { 'invoice' => { etc... } } }
# You can even access and update values directly:
document['customer.invoice.create'] => 'Rechnung anlegen'
document['customer.invoice.create'] = 'New Value'
document.write_i18n_file('path/to/output_file.yml')
As you may notice, the whole idea is to update a Rails app according to a Excel file that is out-of-sync. In the future there could be lots more handy features, but for now, you can perform some good deal of tasks a lot easier than manually. For instance, you could quickly compare two YML I18n files with each other and automate some synchronization.
I18nKit:Comparer
will compare each I18nKit::Document
with each document:
# Loading an Excel file and a YML file
documents = ::I18nKit::Importer::ExcelXML.new(XML_PATH, :ignore_headline => true, :locales => [:sv, :en, :fi]).parse +
::I18nKit::Importer::YAML.new(YML_FI_HIERARCHICAL_PATH).parse
# Giving the Documents smart names
documents[0].name = 'First'
documents[1].name = 'Second'
documents[2].name = 'Third'
documents[3].name = 'Fourth'
# Load the comparer
comparer = I18nKit::Comparer.new(documents)
# Iterate the comparison
comparer.documents_to_compare.each do |left, right|
puts "#{left.name} <=> #{right.name}"
end
will output:
First <=> Second
First <=> Third
First <=> Fourth
Second <=> Third
Second <=> Fourth
Third <=> Fourth
You can also access individual documents by their name by using [] on the Comparer:
comparer[:excel_sv].keys.each do |key|
if comparer[:yaml_sv].has_key?(key)
# Do something because :excel_sv has a key that also exists in :yaml_sv
end
end
Of course you can then compare them as you wish and merge them in some specific way (depending on your needs).
Development
Fork the i18n_kit project from Github and run the tests with
bundle install
rake spec
Copyright
External library in lib/yamlator.rb
: YAMLator by Henrik Nyh http://henrik.nyh.se 2010-02-03 under the MIT license.
All other files are as well free for all under the MIT license. See LICENSE.txt for further details.