Skip to content

Commit

Permalink
Additional fix for #162
Browse files Browse the repository at this point in the history
  • Loading branch information
glebm committed Aug 7, 2015
1 parent 497c27f commit dc28725
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
13 changes: 9 additions & 4 deletions lib/i18n/tasks/command/commands/missing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,15 @@ def translate_missing(opt = {})
args: [:locales, :out_format, arg(:value) + [{default: '%{value_or_human_key}'}]]

def add_missing(opt = {})
forest = i18n.missing_keys(opt).set_each_value!(opt[:value])
i18n.data.merge! forest
log_stderr t('i18n_tasks.add_missing.added', count: forest.leaves.count)
print_forest forest, opt
added = i18n.empty_forest
(opt[:locales] || i18n.locales).each { |locale|
forest = i18n.missing_keys({locales: [locale]}.update(opt.slice(:types, :base_locale))).
set_each_value!(opt[:value])
i18n.data.merge! forest
added.merge! forest
}
log_stderr t('i18n_tasks.add_missing.added', count: added.leaves.count)
print_forest added, opt
end
end
end
Expand Down
9 changes: 8 additions & 1 deletion lib/i18n/tasks/command/option_parsers/locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,18 @@ def call(vals, context)
if vals == %w(all) || vals.blank?
context.locales
else
vals.map { |v| v == 'base' ? context.base_locale : v }
move_base_locale_to_front! vals.map { |v| v == 'base' ? context.base_locale : v }, context.base_locale
end.tap do |locales|
locales.each { |locale| validate! locale }
end
end

def move_base_locale_to_front!(locales, base_locale)
if (pos = locales.index(base_locale)) && pos > 0
locales[pos], locales[0] = locales[0], locales[pos]
end
locales
end
end
end
end
Expand Down
23 changes: 23 additions & 0 deletions spec/commands/missing_commands_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'spec_helper'

RSpec.describe 'Data commands' do
delegate :run_cmd, to: :TestCodebase

describe '#add_missing' do
describe 'adds the missing keys to base locale first, then to other locales' do
around do |ex|
TestCodebase.setup(
'config/i18n-tasks.yml' => {base_locale: 'en', locales: %w(es fr)}.to_yaml,
'config/locales/es.yml' => {'es' => {'a' => 'A'}}.to_yaml)
TestCodebase.in_test_app_dir { ex.call }
TestCodebase.teardown
end

it 'with -v argument' do
run_cmd 'add-missing', '-vTRME'
expect(YAML.load_file('config/locales/en.yml')).to eq('en' => {'a' => 'TRME'})
expect(YAML.load_file('config/locales/fr.yml')).to eq('fr' => {'a' => 'TRME'})
end
end
end
end

0 comments on commit dc28725

Please sign in to comment.