-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Verify that non-base translations are using variable names defined in the base translation. Fix #303
- Loading branch information
Showing
12 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
module I18n::Tasks | ||
module Command | ||
module Commands | ||
module Inconsistent | ||
include Command::Collection | ||
|
||
cmd :check_consistent_interpolations, | ||
pos: '[locale ...]', | ||
desc: t('i18n_tasks.cmd.desc.check_consistent_interpolations'), | ||
args: %i[locales out_format] | ||
|
||
def check_consistent_interpolations(opt = {}) | ||
forest = i18n.inconsistent_interpolation(opt.slice(:locales, :base_locale)) | ||
print_forest forest, opt, :inconsistent_interpolation | ||
:exit_1 unless forest.empty? | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
module I18n::Tasks | ||
module InconsistentInterpolation | ||
VARIABLE_REGEX = /%{[^}]+}/ | ||
|
||
def inconsistent_interpolation(locales: nil, base_locale: nil) | ||
locales ||= self.locales | ||
base = base_locale || self.base_locale | ||
tree = empty_forest | ||
|
||
data[base].key_values.each do |key, value| | ||
next if ignore_key?(key, :inconsistent) || !value.is_a?(String) | ||
|
||
base_variables = Set.new(value.scan(VARIABLE_REGEX)) | ||
|
||
(locales - [base]).each do |current_locale| | ||
node = data[current_locale].first.children[key] | ||
|
||
next if !node&.value&.is_a?(String) || base_variables == Set.new(node.value.scan(VARIABLE_REGEX)) | ||
|
||
tree.merge! inconsistent_interpolation_tree(current_locale, key) | ||
end | ||
end | ||
|
||
tree | ||
end | ||
|
||
def inconsistent_interpolation_tree(locale, key) | ||
data[locale].select_keys(root: false) { |x| x == key } | ||
.set_root_key!(locale, type: :inconsistent_interpolation) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe 'Inconsistent commands' do | ||
delegate :run_cmd, :in_test_app_dir, to: :TestCodebase | ||
|
||
let(:base_keys) { { 'a' => 'hello %{world}', 'b' => 'foo', 'c' => { 'd' => 'hello %{name}' }, 'e' => 'ok' } } | ||
let(:test_keys) { { 'a' => 'hello', 'b' => 'foo %{bar}', 'c' => { 'd' => 'hola %{amigo}' }, 'e' => 'ok' } } | ||
|
||
let(:wrong_subtree) { { 'es' => test_keys.slice('a', 'b', 'c') } } | ||
|
||
around do |ex| | ||
TestCodebase.setup( | ||
'config/i18n-tasks.yml' => { base_locale: 'en', locales: %w[es] }.to_yaml, | ||
'config/locales/en.yml' => { 'en' => base_keys }.to_yaml, | ||
'config/locales/es.yml' => { 'es' => test_keys }.to_yaml | ||
) | ||
|
||
TestCodebase.in_test_app_dir { ex.call } | ||
TestCodebase.teardown | ||
end | ||
|
||
describe '#check_consistent_interpolations' do | ||
it 'returns inconsistent keys' do | ||
expect(YAML.load(run_cmd('check-consistent-interpolations', '-fyaml'))).to eq(wrong_subtree) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe 'InconsistentInterpolation' do | ||
let!(:task) { I18n::Tasks::BaseTask.new } | ||
|
||
let(:base_keys) { { 'a' => 'hello %{world}', 'b' => 'foo', 'c' => { 'd' => 'hello %{name}' }, 'e' => 'ok' } } | ||
let(:test_keys) { { 'a' => 'hello', 'b' => 'foo %{bar}', 'c' => { 'd' => 'hola %{amigo}' }, 'e' => 'ok' } } | ||
|
||
around do |ex| | ||
TestCodebase.setup( | ||
'config/i18n-tasks.yml' => { base_locale: 'en', locales: %w[es] }.to_yaml, | ||
'config/locales/en.yml' => { 'en' => base_keys }.to_yaml, | ||
'config/locales/es.yml' => { 'es' => test_keys }.to_yaml | ||
) | ||
|
||
TestCodebase.in_test_app_dir { ex.call } | ||
TestCodebase.teardown | ||
end | ||
|
||
it '#inconsistent_interpolation' do | ||
wrong = task.inconsistent_interpolation | ||
leaves = wrong.leaves.to_a | ||
|
||
expect(leaves.size).to eq 3 | ||
expect(leaves[0].full_key).to eq 'es.a' | ||
expect(leaves[1].full_key).to eq 'es.b' | ||
expect(leaves[2].full_key).to eq 'es.c.d' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters