Skip to content
This repository has been archived by the owner on Dec 12, 2019. It is now read-only.

Commit

Permalink
Added rake task translate:changed that can show you from one YAML fil…
Browse files Browse the repository at this point in the history
…e to another which keys have had their texts changed
  • Loading branch information
Peter Marklund committed Aug 29, 2009
1 parent e01c84b commit 1f8ebd6
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 16 deletions.
3 changes: 3 additions & 0 deletions README
Expand Up @@ -22,13 +22,16 @@ In addition to the web UI this plugin adds the following rake tasks:
translate:lost_in_translation
translate:merge_keys
translate:google
translate:changed

The lost_in_translation task shows you any I18n keys in your code that are do not have translations in the YAML file for your default locale, i.e. config/locales/sv.yml.

The merge_keys task is supposed to be used in conjunction with Sven Fuch's Rails I18n TextMate bundle (http://github.com/svenfuchs/rails-i18n/tree/master). Texts and keys extracted with the TextMate bundle end up in the temporary file log/translations.yml. When you run the merge_keys rake task the keys are moved over to the corresponding I18n locale file, i.e. config/locales/sv.yml. The merge_keys task also checks for overwrites of existing keys by warning you that one of your extracted keys already exists with a different translation.

The google task is used for auto translating from one locale to another using Google Translate.

The changed rake task can show you between one YAML file to another which keys have had their texts changed.

Installation
=========
Obtain the source with:
Expand Down
29 changes: 28 additions & 1 deletion lib/translate/keys.rb
Expand Up @@ -26,6 +26,33 @@ def i18n_keys(locale)
extract_i18n_keys(I18n.backend.send(:translations)[locale.to_sym]).sort
end

# Convert something like:
#
# {
# :pressrelease => {
# :label => {
# :one => "Pressmeddelande"
# }
# }
# }
#
# to:
#
# {'pressrelease.label.one' => "Pressmeddelande"}
#
def self.to_shallow_hash(hash)
hash.inject({}) do |shallow_hash, (key, value)|
if value.is_a?(Hash)
to_shallow_hash(value).each do |sub_key, sub_value|
shallow_hash[[key, sub_key].join(".")] = sub_value
end
else
shallow_hash[key.to_s] = value
end
shallow_hash
end
end

# Convert something like:
#
# {'pressrelease.label.one' => "Pressmeddelande"}
Expand All @@ -41,7 +68,7 @@ def i18n_keys(locale)
# }
def self.to_deep_hash(hash)
hash.inject({}) do |deep_hash, (key, value)|
keys = key.split('.').reverse
keys = key.to_s.split('.').reverse
leaf_key = keys.shift
key_hash = keys.inject({leaf_key.to_sym => value}) { |hash, key| {key.to_sym => hash} }
deep_merge!(deep_hash, key_hash)
Expand Down
44 changes: 29 additions & 15 deletions spec/keys_spec.rb
Expand Up @@ -53,21 +53,13 @@ def translations

describe "to_deep_hash" do
it "convert shallow hash with dot separated keys to deep hash" do
Translate::Keys.to_deep_hash({
'pressrelease.label.one' => "Pressmeddelande",
'pressrelease.label.other' => "Pressmeddelanden",
'article' => "Artikel",
'category' => ''
}).should == {
:pressrelease => {
:label => {
:one => "Pressmeddelande",
:other => "Pressmeddelanden"
}
},
:article => "Artikel",
:category => ''
}
Translate::Keys.to_deep_hash(shallow_hash).should == deep_hash
end
end

describe "to_shallow_hash" do
it "converts a deep hash to a shallow one" do
Translate::Keys.to_shallow_hash(deep_hash).should == shallow_hash
end
end

Expand All @@ -77,6 +69,28 @@ def translations
#
##########################################################################

def shallow_hash
{
'pressrelease.label.one' => "Pressmeddelande",
'pressrelease.label.other' => "Pressmeddelanden",
'article' => "Artikel",
'category' => ''
}
end

def deep_hash
{
:pressrelease => {
:label => {
:one => "Pressmeddelande",
:other => "Pressmeddelanden"
}
},
:article => "Artikel",
:category => ''
}
end

def i18n_files_dir
File.join(File.dirname(__FILE__), "files", "translate")
end
Expand Down
18 changes: 18 additions & 0 deletions tasks/translate.rake
Expand Up @@ -139,4 +139,22 @@ namespace :translate do
puts "\nTime elapsed: #{(((Time.now - start_at) / 60) * 10).to_i / 10.to_f} minutes"
Translate::Storage.new(ENV['TO'].to_sym).write_to_file
end

desc "List keys that have changed I18n texts between YAML file ENV['FROM_FILE'] and YAML file ENV['TO_FILE']. Set ENV['VERBOSE'] to see changes"
task :changed => :environment do
from_hash = Translate::Keys.to_shallow_hash(Translate::File.new(ENV['FROM_FILE']).read)
to_hash = Translate::Keys.to_shallow_hash(Translate::File.new(ENV['TO_FILE']).read)
from_hash.each do |key, from_value|
if (to_value = to_hash[key]) && to_value != from_value
key_without_locale = key[/^[^.]+\.(.+)$/, 1]
if ENV['VERBOSE']
puts "KEY: #{key_without_locale}"
puts "FROM VALUE: '#{from_value}'"
puts "TO VALUE: '#{to_value}'"
else
puts key_without_locale
end
end
end
end
end

0 comments on commit 1f8ebd6

Please sign in to comment.