Skip to content

Commit

Permalink
default: argument support for add-missing -v
Browse files Browse the repository at this point in the history
Finally, #55
  • Loading branch information
glebm committed Aug 8, 2015
1 parent d6ce295 commit a482c68
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ en:
pattern_router: 'Use pattern router: keys moved per config data.write'
strict: Avoid inferring dynamic key usages such as t("cats.#{cat}.name"). Takes precedence over the
config setting if set.
value: 'Value. Interpolates: %{value}, %{human_key}, %{value_or_human_key}, %{key}'
value: '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
config: display i18n-tasks configuration
Expand Down
2 changes: 1 addition & 1 deletion config/locales/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ru:
out_format: "Формат вывода: %{valid_text}. %{default_text}."
pattern_router: "Использовать pattern_router: ключи распределятся по файлам согласно data.write"
strict: Не угадывать динамические использования ключей, например `t("category.#{category.key}")`
value: "Значение, интерполируется с %{value}, %{human_key}, %{value_or_human_key}, %{key}"
value: "Значение, интерполируется с %{value}, %{human_key}, %{key}, %{default}, %{value_or_human_key}, %{value_or_default_or_human_key}"
desc:
add_missing: "добавить недостающие ключи к переводам"
config: "показать конфигурацию"
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/tasks/command/commands/missing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def translate_missing(opt = {})
cmd :add_missing,
pos: '[locale ...]',
desc: t('i18n_tasks.cmd.desc.add_missing'),
args: [:locales, :out_format, arg(:value) + [{default: '%{value_or_human_key}'}]]
args: [:locales, :out_format, arg(:value) + [{default: '%{value_or_default_or_human_key}'}]]

def add_missing(opt = {})
added = i18n.empty_forest
Expand Down
14 changes: 9 additions & 5 deletions lib/i18n/tasks/data/tree/traversal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,17 @@ def set_each_value!(val_pattern, key_pattern = nil, &value_proc)
value_proc ||= proc { |node|
node_value = node.value
human_key = ActiveSupport::Inflector.humanize(node.key.to_s)
full_key = node.full_key
full_key = node.full_key
StringInterpolation.interpolate_soft(
val_pattern,
value: node_value,
human_key: human_key,
key: full_key,
value_or_human_key: node_value.presence || human_key
value: node_value,
human_key: human_key,
key: full_key,
value_or_human_key: node_value.presence || human_key,
value_or_default_or_human_key: node_value.presence ||
(node.data[:occurrences] || []).detect { |o|
o.default_arg.presence }.try(:default_arg) ||
human_key
)
}
if key_pattern.present?
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def show()
t(".success")

# i18n-tasks-use t('magic_comment')
magic

# default arg
I18n.t('default_arg', default: 'Default Text')
end

def update
Expand Down
5 changes: 4 additions & 1 deletion spec/i18n_tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
events.show.success
index.my_custom_scanner.title
magic_comment
default_arg
)
}
let (:expected_missing_keys_diff) {
Expand Down Expand Up @@ -172,14 +173,16 @@
end

describe 'add_missing' do
it 'default placeholder: key.humanize for base_locale' do
it 'default placeholder: default_or_value_or_human_key' do
in_test_app_dir {
expect(YAML.load_file('config/locales/en.yml')['en']['used_but_missing']).to be_nil
expect(YAML.load_file('config/locales/en.yml')['en']['default_arg']).to be_nil
}
run_cmd 'add-missing', 'base'
in_test_app_dir {
expect(YAML.load_file('config/locales/en.yml')['en']['used_but_missing']['key']).to eq 'Key'
expect(YAML.load_file('config/locales/en.yml')['en']['present_in_es_but_not_en']['a']).to eq 'ES_TEXT'
expect(YAML.load_file('config/locales/en.yml')['en']['default_arg']).to eq 'Default Text'
}
end

Expand Down

0 comments on commit a482c68

Please sign in to comment.