From 8299b33d76ae6a08fe070e802d3f9931f4d2ea19 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Sat, 23 Nov 2013 08:08:25 +0100 Subject: [PATCH] minor refactor --- lib/i18n/tasks/translation_data.rb | 8 ++++---- lib/tasks/i18n-tasks.rake | 27 +++++++++++++++------------ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/lib/i18n/tasks/translation_data.rb b/lib/i18n/tasks/translation_data.rb index d27e2ebe..566d2537 100644 --- a/lib/i18n/tasks/translation_data.rb +++ b/lib/i18n/tasks/translation_data.rb @@ -39,16 +39,16 @@ def normalize_store!(locales = self.locales) end end - # fill missing keys with values from passed block - def fill_blanks!(locale: base_locale, &fill_with) + # fill missing / blank keys with values from passed block + def fill_blanks!(locale = base_locale, &fill_with) blank_keys = if locale == base_locale - # for base locale, blank is present in source but not in the base locale + # for base locale "blank" is: present in source but not in the base locale. keys_missing_base_value.map { |e| e[:key] } + traverse_flat_map(data[base_locale]) { |key, value| key if value.to_s.blank? && !ignore_key?(key, :missing) } else - # for other locales, blank is present in base but not in this locale + # for other locales "blank" is: present in base but not in the locale itself. traverse_flat_map(data[base_locale]) { |key| key if !key_value?(key, locale) && !ignore_key?(key, :missing) } end diff --git a/lib/tasks/i18n-tasks.rake b/lib/tasks/i18n-tasks.rake index f227846c..ecfadea9 100644 --- a/lib/tasks/i18n-tasks.rake +++ b/lib/tasks/i18n-tasks.rake @@ -24,7 +24,7 @@ namespace :i18n do desc 'add to the base locale' task :add_missing, [:placeholder] => 'i18n:setup' do |t, args| normalize_store! - i18n_tasks.fill_blanks!(locale: base_locale) { |keys| + i18n_tasks.fill_blanks!(base_locale) { |keys| keys.map { |key| args[:placeholder] || key.split('.').last.to_s.humanize } @@ -34,17 +34,17 @@ namespace :i18n do desc 'add to each locale' task :with_blanks, [:locales] => 'i18n:setup' do |t, args| normalize_store! - [base_locale, *locales_or_all(args)].uniq.each do |locale| - i18n_tasks.fill_blanks!(locale: locale) { |keys| keys.map { '' } } + [base_locale, *non_base_locales].each do |locale| + fill_blanks!(locale) { |blank_keys| blank_keys.map { '' } } end end desc 'add to each non-base locale, uses env GOOGLE_TRANSLATE_API_KEY' task :with_google, [:locales] => 'i18n:setup' do |t, args| normalize_store! - (locales_or_all(args) - [base_locale]).each do |locale| - i18n_tasks.fill_blanks!(locale: locale) { |keys| - i18n_tasks.google_translate keys.map { |k| t(k) }, to: locale, from: base_locale + non_base_locales(args).each do |locale| + fill_blanks!(locale) { |blank_keys| + i18n_tasks.google_translate blank_keys.map { |k| t(k) }, to: locale, from: base_locale } end end @@ -52,8 +52,8 @@ namespace :i18n do desc 'add to each non-base locale' task :with_base, [:locales] => 'i18n:setup' do |t, args| normalize_store! - (locales_or_all(args) - [base_locale]).each do |locale| - i18n_tasks.fill_blanks!(locale: locale) { |keys| keys.map { |k| t(k) } } + non_base_locales(args).each do |locale| + fill_blanks!(locale) { |blank_keys| blank_keys.map { |k| t(k) } } end end end @@ -69,7 +69,7 @@ namespace :i18n do extend ActiveSupport::Concern included do - delegate :t, :locales, :base_locale, :normalize_store!, to: :i18n_tasks + delegate :t, :locales, :base_locale, :normalize_store!, :fill_blanks!, to: :i18n_tasks def i18n_tasks @i18n_tasks ||= I18n::Tasks::BaseTask.new @@ -79,9 +79,12 @@ namespace :i18n do @term_output ||= I18n::Tasks::Output::Terminal.new end - def locales_or_all(args) - args.with_defaults(locales: i18n_tasks.locales * '+') - args[:locales].strip.split(/\s*\+\s*/) + def non_base_locales(args = nil) + locales_or_all(args) - [base_locale] + end + + def locales_or_all(args = nil) + args[:locales] ? args[:locales].strip.split(/\s*\+\s*/) : locales end end end