-
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
8 changed files
with
118 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
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,48 @@ | ||
# frozen_string_literal: true | ||
|
||
module I18n::Tasks | ||
module WrongInterpolations | ||
VARIABLE_REGEX = /%{[^}]+}/ | ||
|
||
def wrong_interpolations(locales: nil, base_locale: nil) | ||
locales ||= self.locales | ||
base = base_locale || self.base_locale | ||
tree = empty_forest | ||
|
||
data[base].key_values.each do |key, node| | ||
next unless node.is_a?(String) | ||
|
||
base_variables = node.scan(VARIABLE_REGEX) | ||
|
||
(locales - [base]).each do |current_locale| | ||
check_in_other_locale(tree, key, base_variables, current_locale) | ||
end | ||
end | ||
|
||
tree | ||
end | ||
|
||
private | ||
|
||
def check_in_other_locale(tree, key, base_variables, locale) | ||
node = data[locale].first.children[key] | ||
|
||
return if node.nil? || !node.value.is_a?(String) | ||
|
||
compare_variables = node.value.scan(VARIABLE_REGEX) | ||
|
||
return if same_array?(base_variables, compare_variables) | ||
|
||
tree.merge!(select_key(key, locale)) | ||
end | ||
|
||
def select_key(key, locale) | ||
data[locale].select_keys(root: false) { |search_key| search_key == key } | ||
.set_root_key!(locale, type: :wrong_interpolations) | ||
end | ||
|
||
def same_array?(a, b) | ||
a.size == b.size && (a & b == a) | ||
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 'WrongInterpolations' 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 '#wrong_interpolations' do | ||
wrong = task.wrong_interpolations | ||
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 |