Skip to content

Commit

Permalink
minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
glebm committed Nov 23, 2013
1 parent 2eba4bc commit 8299b33
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
8 changes: 4 additions & 4 deletions lib/i18n/tasks/translation_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 15 additions & 12 deletions lib/tasks/i18n-tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace :i18n do
desc 'add <key: placeholder || key.humanize> 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
}
Expand All @@ -34,26 +34,26 @@ namespace :i18n do
desc 'add <key: ""> 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 <key: Google Translated value> 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

desc 'add <key: base value> 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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 8299b33

Please sign in to comment.