Skip to content

Commit

Permalink
Rake task which detects missing translations for a given locale
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Ullmann authored and steveyken committed Jul 2, 2013
1 parent 43e23dd commit d36af30
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
50 changes: 50 additions & 0 deletions lib/missing_translation_detector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Compares two locale files and detects missing translations.
class MissingTranslationDetector
attr_reader :missing_translations

# @params [String] base_file_name File name of the base locale is i.e. en-US
# @params [String] target_file_name File name of a locale with missing translations is i.e. de
def initialize(base_file_name, target_file_name)
@base = yml_load base_file_name
@target = yml_load target_file_name
@missing_translations = []
end

# Detects missing translations within the target locale file
# and stores it in "missing_translations".
def detect(h=@base, keys=[])
h.each_key do |key|
key_path = keys.clone.push key

if h[key].is_a?(Hash)
detect h[key], key_path
elsif blank?(key_path)
missing_translations << OpenStruct.new(:key_path => key_path,
:value => h[key])
end
end
end

# @returns [Boolean] true if missing translations are detected, otherwise false is returned.
def missing_translations?
!@missing_translations.empty?
end

private

def blank?(keys)
h = @target

keys.each do |key|
return true if !h.is_a?(Hash) || !h.has_key?(key)
h = h[key]
end

h.nil?
end

def yml_load(file_name)
h = YAML.load_file "#{Rails.root}/config/locales/#{file_name}.yml"
h[h.keys.first]
end
end
33 changes: 33 additions & 0 deletions lib/tasks/ffcrm/missing_translations.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright (c) 2008-2013 Michael Dvorkin and contributors.
#
# Fat Free CRM is freely distributable under the terms of MIT license.
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------

require './lib/missing_translation_detector.rb'

namespace :ffcrm do
namespace :missing_translations do
desc 'Detects missing translations for a locale - Takes source language and compares with "en-US".'
task :detect, [:locale] => [:environment] do |t, args|
base_locale = 'en-US'

[[base_locale, args[:locale]],
["#{base_locale}_fat_free_crm", "#{args[:locale]}_fat_free_crm"]].each do |locale_file_names|
detector = MissingTranslationDetector.new locale_file_names.first,
locale_file_names.last
detector.detect

if detector.missing_translations?
puts
puts "Detected missing translations within \"config/locales/#{locale_file_names.last}.yml\":"
puts

detector.missing_translations.each do |missing|
puts "#{missing.key_path.join(' => ')}: #{missing.value}"
end
end
end
end
end
end

0 comments on commit d36af30

Please sign in to comment.