From befab7aa54c5c1415ef89aadfa9e920d372f0980 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Fri, 7 Aug 2015 19:16:25 +0100 Subject: [PATCH] Additional fix for #162 --- lib/i18n/tasks/command/commands/missing.rb | 18 ++++++++++++--- .../tasks/command/option_parsers/locale.rb | 9 +++++++- spec/commands/missing_commands_spec.rb | 23 +++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 spec/commands/missing_commands_spec.rb diff --git a/lib/i18n/tasks/command/commands/missing.rb b/lib/i18n/tasks/command/commands/missing.rb index 87a1423e..1a083e1e 100644 --- a/lib/i18n/tasks/command/commands/missing.rb +++ b/lib/i18n/tasks/command/commands/missing.rb @@ -47,10 +47,22 @@ 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]) + added = i18n.empty_forest + locales = (opt[:locales] || i18n.locales) + if locales[0] == i18n.base_locale + # Merge base locale first, as this may affect the value for the other locales + forest = i18n.missing_keys({locales: [locales[0]]}.update(opt.slice(:types, :base_locale))). + set_each_value!(opt[:value]) + i18n.data.merge! forest + added.merge! forest + locales = locales[1..-1] + end + forest = i18n.missing_keys({locales: locales}.update(opt.slice(:types, :base_locale))). + 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.merge! forest + log_stderr t('i18n_tasks.add_missing.added', count: added.leaves.count) + print_forest added, opt end end end diff --git a/lib/i18n/tasks/command/option_parsers/locale.rb b/lib/i18n/tasks/command/option_parsers/locale.rb index ad132746..dbf155a9 100644 --- a/lib/i18n/tasks/command/option_parsers/locale.rb +++ b/lib/i18n/tasks/command/option_parsers/locale.rb @@ -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 diff --git a/spec/commands/missing_commands_spec.rb b/spec/commands/missing_commands_spec.rb new file mode 100644 index 00000000..28d11eb1 --- /dev/null +++ b/spec/commands/missing_commands_spec.rb @@ -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