Skip to content

Commit

Permalink
Added untranslated rake task that shows keys that needs translating.
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Marklund committed Feb 4, 2010
1 parent c8f46ff commit 42e562b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 20 deletions.
1 change: 1 addition & 0 deletions README
Expand Up @@ -19,6 +19,7 @@ Rake Tasks

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

translate:untranslated
translate:lost_in_translation
translate:remove_obsolete_keys
translate:merge_keys
Expand Down
13 changes: 13 additions & 0 deletions lib/translate/keys.rb
Expand Up @@ -25,6 +25,19 @@ def i18n_keys(locale)
I18n.backend.send(:init_translations) unless I18n.backend.initialized?
extract_i18n_keys(I18n.backend.send(:translations)[locale.to_sym]).sort
end

def untranslated_keys
Translate::Keys.translated_locales.inject({}) do |missing, locale|
missing[locale] = i18n_keys(I18n.default_locale).map do |key|
I18n.backend.send(:lookup, locale, key) ? nil : key
end.compact
missing
end
end

def self.translated_locales
I18n.available_locales.reject { |locale| [:root, I18n.default_locale.to_sym].include?(locale) }
end

# Convert something like:
#
Expand Down
65 changes: 47 additions & 18 deletions spec/keys_spec.rb
Expand Up @@ -29,6 +29,48 @@
@keys.i18n_keys(:en).should == ['articles.new.page_title', 'categories.flash.created', 'home.about']
end

describe "untranslated_keys" do
before(:each) do
I18n.stub!(:default_locale).and_return(:en)
I18n.backend.stub!(:translations).and_return(translations)
end

it "should return a hash with keys with missing translations in each locale" do
Translate::Keys.new.untranslated_keys.should == {
:sv => ['articles.new.page_title', 'categories.flash.created']
}
end
end

describe "translated_locales" do
before(:each) do
I18n.stub!(:default_locale).and_return(:en)
I18n.stub!(:available_locales).and_return([:sv, :no, :en, :root])
end

it "returns all avaiable except :root and the default" do
Translate::Keys.translated_locales.should == [:sv, :no]
end
end

describe "to_deep_hash" do
it "convert shallow hash with dot separated keys to deep hash" do
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

##########################################################################
#
# Helper Methods
#
##########################################################################

def translations
{
:en => {
Expand All @@ -46,28 +88,15 @@ def translations
}
},
:empty => nil
},
:sv => {
:home => {
:about => "Denna site handlar om att tjäna pengar"
}
}
}
end
end

describe "to_deep_hash" do
it "convert shallow hash with dot separated keys to deep hash" do
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

##########################################################################
#
# Helper Methods
#
##########################################################################

def shallow_hash
{
Expand Down
19 changes: 17 additions & 2 deletions tasks/translate.rake
Expand Up @@ -33,6 +33,22 @@ class Hash
end

namespace :translate do
desc "Show untranslated keys for locale LOCALE"
task :untranslated => :environment do
from_locale = I18n.default_locale
untranslated = Translate::Keys.new.untranslated_keys
if untranslated.present?
untranslated.each do |locale, keys|
keys.each do |key|
from_text = I18n.backend.send(:lookup, from_locale, key)
puts "#{locale}.#{key} (#{from_locale}.#{key}='#{from_text}')"
end
end
else
puts "No untranslated keys"
end
end

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
Expand Down Expand Up @@ -64,8 +80,7 @@ namespace :translate do
task :remove_obsolete_keys => :environment do
I18n.backend.send(:init_translations)
master_locale = ENV['LOCALE'] || I18n.default_locale
translated_locales = I18n.available_locales.reject { |locale| [:root, I18n.default_locale.to_sym].include?(locale) }
translated_locales.each do |locale|
Translate::Keys.translated_locales.each do |locale|
texts = {}
Translate::Keys.new.i18n_keys(locale).each do |key|
if I18n.backend.send(:lookup, master_locale, key).to_s.present?
Expand Down

0 comments on commit 42e562b

Please sign in to comment.