Skip to content

Commit

Permalink
Added lost_in_translation and merge_keys rake tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Marklund committed Feb 6, 2009
1 parent 29133ee commit 034ff25
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
12 changes: 12 additions & 0 deletions README
Expand Up @@ -11,6 +11,18 @@ To get the translation UI to write the YAML files in UTF8 you need to install th

The translation UI finds all I18n keys by extracting them from I18n lookups in your application source code. In addition it adds all :en and default locale keys from the I18n backend.

Rake Tasks
=========

In addition to the web UI this plugin adds the following two rake tasks:

translate:lost_in_translation
translate:merge_keys

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.

Installation
=========
Obtain the source with:
Expand Down
72 changes: 72 additions & 0 deletions tasks/translate.rake
@@ -0,0 +1,72 @@
require 'yaml'

class Hash
def deep_merge(other)
# deep_merge by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809
merger = proc { |key, v1, v2| (Hash === v1 && Hash === v2) ? v1.merge(v2, &merger) : v2 }
merge(other, &merger)
end

def set(keys, value)
key = keys.shift
if keys.empty?
self[key] = value
else
self[key] ||= {}
self[key].set keys, value
end
end
end

namespace :translate do
desc "Show I18n keys that are missing in the config/locales/default_locale.yml YAML file"
task :lost_in_translation => :environment do
LOCALE = I18n.default_locale
keys = []; result = []; locale_hash = {}
Dir.glob(File.join("config", "locales", "**","#{LOCALE}.yml")).each do |locale_file_name|
locale_hash = locale_hash.deep_merge(YAML::load(File.open(locale_file_name))[LOCALE])
end
lookup_pattern = Translate::Keys.new.send(:i18n_lookup_pattern)
Dir.glob(File.join("app", "**","*.{rb,rhtml}")).each do |file_name|
File.open(file_name, "r+").each do |line|
line.scan(lookup_pattern) do |key_string|
result << "#{key_string} in \t #{file_name} is not in any locale file" unless key_exist?(key_string.first.split("."), locale_hash)
end
end
end
puts !result.empty? ? result.join("\n") : "No missing translations for locale: #{LOCALE}"
end

def key_exist?(key_arr,locale_hash)
key = key_arr.slice!(0)
if key
key_exist?(key_arr, locale_hash[key]) if (locale_hash && locale_hash.include?(key))
elsif locale_hash
true
end
end

desc "Merge I18n keys from log/translations.yml into config/locales/*.yml (for use with the Rails I18n TextMate bundle)"
task :merge_keys => :environment do
I18n.backend.send(:init_translations)
new_translations = YAML::load(IO.read(File.join(Rails.root, "log", "translations.yml")))
raise("Can only merge in translations in single locale") if new_translations.keys.size > 1
locale = new_translations.keys.first

overwrites = false
Translate::Keys.new.send(:extract_i18n_keys, new_translations[locale]).each do |key|
new_text = key.split(".").inject(new_translations[locale]) { |hash, sub_key| hash[sub_key] }
existing_text = I18n.backend.send(:lookup, locale.to_sym, key)
if existing_text && new_text != existing_text
puts "ERROR: key #{key} already exists with text '#{existing_text.inspect}' and would be overwritten by new text '#{new_text}'. " +
"Set environment variable OVERWRITE=1 if you really want to do this."
overwrites = true
end
end

if !overwrites || ENV['OVERWRITE']
I18n.backend.store_translations(locale, new_translations[locale])
Translate::Storage.new(locale).write_to_file
end
end
end
4 changes: 0 additions & 4 deletions tasks/translate_tasks.rake

This file was deleted.

0 comments on commit 034ff25

Please sign in to comment.