Skip to content

Commit

Permalink
Pulling in latest translate plugin changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kieran Pilkington committed Nov 13, 2009
2 parents c66c7c4 + 1f8ebd6 commit 0a0a15c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 16 deletions.
3 changes: 3 additions & 0 deletions README
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
2 changes: 2 additions & 0 deletions spec/controllers/translate_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def files

describe "translate" do
it "should store translations to I18n backend and then write them to a YAML file" do
session[:from_locale] = :sv
session[:to_locale] = :en
translations = {
:articles => {
:new => {
Expand Down
44 changes: 29 additions & 15 deletions spec/keys_spec.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,24 @@ namespace :translate do
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

desc 'Create a new translation based on the English translation (pass in LOCALE_CODE - a two letter country code, and LOCALE_NAME - the translated name of the language you\'re adding.)'
task :create do
raise "LOCALE_CODE (two letter country code) is not set. Please set one before running the rake task." unless ENV['LOCALE_CODE']
Expand Down

0 comments on commit 0a0a15c

Please sign in to comment.