Skip to content

Commit

Permalink
Fix unescaped interpolated reserved key in help text and handle escap…
Browse files Browse the repository at this point in the history
…ed interpolations in inconsistent interpolations check (#553)

* Fix unescaped interpolated reserved key in help text

* Ignore escaped interpolations in inconsistent interpolations check

* Add test for escaped interpolations in inconsisten interpolation check
  • Loading branch information
Bilka2 committed May 7, 2024
1 parent d8b98fa commit dffd911
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
6 changes: 2 additions & 4 deletions config/locales/en.yml
Expand Up @@ -16,12 +16,10 @@ en:
data_format: 'Data format: %{valid_text}.'
keep_order: Keep the order of the keys
key_pattern: Filter by key pattern (e.g. 'common.*')
key_pattern_to_rename: Full key (pattern) to rename. Required
locale: :i18n_tasks.common.locale
locale_to_translate_from: Locale to translate from
locales_filter: 'Locale(s) to process. Special: base'
missing_types: 'Filter by types: %{valid}'
new_key_name: New name, interpolates original name as %{key}. Required
nostdin: Do not read from stdin
out_format: 'Output format: %{valid_text}'
pattern_router: 'Use pattern router: keys moved per config data.write'
Expand All @@ -30,8 +28,8 @@ en:
the config setting if set.
translation_backend: Translation backend (google or deepl)
value: >-
Value. Interpolates: %{value}, %{human_key}, %{key}, %{default}, %{value_or_human_key},
%{value_or_default_or_human_key}
Value. Interpolates: %%{value}, %%{human_key}, %%{key}, %%{default}, %%{value_or_human_key},
%%{value_or_default_or_human_key}
desc:
add_missing: add missing keys to locale data, optionally match a pattern
check_consistent_interpolations: verify that all translations use correct interpolation variables
Expand Down
6 changes: 2 additions & 4 deletions config/locales/ru.yml
Expand Up @@ -13,22 +13,20 @@ ru:
data_format: 'Формат данных: %{valid_text}.'
keep_order: Keep the order of the keys
key_pattern: Маска ключа (например, common.*)
key_pattern_to_rename: Полный ключ (шаблон) для переименования. Необходимый параметр.
locale: 'Язык. По умолчанию: base'
locale_to_translate_from: 'Язык, с которого переводить (по умолчанию: base)'
locales_filter: >-
Список языков для обработки, разделенный запятыми (,). По умолчанию: все. Специальное
значение: base.
missing_types: 'Типы недостающих переводов: %{valid}. По умолчанию: все'
new_key_name: Новое имя, интерполирует оригинальное название как %{key}. Необходимый параметр.
nostdin: Не читать дерево из стандартного ввода
out_format: 'Формат вывода: %{valid_text}.'
pattern_router: 'Использовать pattern_router: ключи распределятся по файлам согласно data.write'
strict: Не угадывать динамические использования ключей, например `t("category.#{category.key}")`
translation_backend: Движок перевода (google или deepl)
value: >-
Значение, интерполируется с %{value}, %{human_key}, %{key}, %{default}, %{value_or_human_key},
%{value_or_default_or_human_key}
Значение, интерполируется с %%{value}, %%{human_key}, %%{key}, %%{default}, %%{value_or_human_key},
%%{value_or_default_or_human_key}
desc:
add_missing: добавить недостающие ключи к переводам
check_consistent_interpolations: убедитесь, что во всех переводах используются правильные
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/tasks/command/options/common.rb
Expand Up @@ -26,7 +26,7 @@ module Common
arg :value,
'-v',
'--value VALUE',
t('i18n_tasks.cmd.args.desc.value')
t('i18n_tasks.cmd.args.desc.value', dummy: 'value') # Dummy value is workaround for https://github.com/ruby-i18n/i18n/issues/689

arg :config,
'-c',
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/tasks/interpolations.rb
Expand Up @@ -5,7 +5,7 @@ module Interpolations
class << self
attr_accessor :variable_regex
end
@variable_regex = /%{[^}]+}/.freeze
@variable_regex = /(?<!%)%{[^}]+}/.freeze

def inconsistent_interpolations(locales: nil, base_locale: nil) # rubocop:disable Metrics/AbcSize
locales ||= self.locales
Expand Down
10 changes: 8 additions & 2 deletions spec/interpolations_spec.rb
Expand Up @@ -5,8 +5,14 @@
RSpec.describe 'Interpolations' 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' } }
let(:base_keys) do
{ 'a' => 'hello %{world}', 'b' => 'foo', 'c' => { 'd' => 'hello %{name}' }, 'e' => 'ok', 'f' => '%%{escaped}',
'g' => 'okay' }
end
let(:test_keys) do
{ 'a' => 'hello', 'b' => 'foo %{bar}', 'c' => { 'd' => 'hola %{amigo}' }, 'e' => 'ok', 'f' => 'okay',
'g' => '%%{ignored}' }
end

around do |ex|
TestCodebase.setup(
Expand Down

0 comments on commit dffd911

Please sign in to comment.