Skip to content

Commit

Permalink
Detect plural default values
Browse files Browse the repository at this point in the history
  • Loading branch information
cantin authored and glebm committed Sep 18, 2022
1 parent 89d87e6 commit 497454f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
32 changes: 23 additions & 9 deletions lib/i18n/tasks/data/tree/traversal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,29 @@ def set_each_value!(val_pattern, key_pattern = nil, &value_proc)
human_key = ActiveSupport::Inflector.humanize(node.key.to_s)
full_key = node.full_key
default = (node.data[:occurrences] || []).detect { |o| o.default_arg.presence }.try(:default_arg)
StringInterpolation.interpolate_soft(
val_pattern,
value: node_value,
human_key: human_key,
key: full_key,
default: default,
value_or_human_key: node_value.presence || human_key,
value_or_default_or_human_key: node_value.presence || default || human_key
)
if default.is_a?(Hash)
default.each_with_object({}) do |(k, v), h|
h[k] = StringInterpolation.interpolate_soft(
val_pattern,
value: node_value,
human_key: human_key,
key: full_key,
default: v,
value_or_human_key: node_value.presence || human_key,
value_or_default_or_human_key: node_value.presence || v || human_key
)
end
else
StringInterpolation.interpolate_soft(
val_pattern,
value: node_value,
human_key: human_key,
key: full_key,
default: default,
value_or_human_key: node_value.presence || human_key,
value_or_default_or_human_key: node_value.presence || default || human_key
)
end
end
pattern_re = I18n::Tasks::KeyPatternMatching.compile_key_pattern(key_pattern) if key_pattern.present?
keys.each do |key, node|
Expand Down
16 changes: 16 additions & 0 deletions lib/i18n/tasks/scanners/ast_matchers/base_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ def extract_string(node, array_join_with: nil, array_flatten: false, array_rejec
end
end

# Extract the whole hash from a node of type `:hash`
#
# @param node [AST::Node] a node of type `:hash`.
# @return [Hash] the whole hash from the node
def extract_hash(node)
return {} if node.nil?

if node.type == :hash
node.children.each_with_object({}) do |pair, h|
key = pair.children[0].children[0].to_s
value = pair.children[1].children[0]
h[key] = value
end
end
end

# Extract a hash pair with a given literal key.
#
# @param node [AST::Node] a node of type `:hash`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,13 @@ def process_options(node:, key:)

key = [scope, key].join('.') unless scope == ''
end
default_arg_node = extract_hash_pair(node, 'default')
default_arg = extract_string(default_arg_node.children[1]) if default_arg_node
if default_arg_node = extract_hash_pair(node, 'default')
default_arg = if default_arg_node.children[1]&.type == :hash
extract_hash(default_arg_node.children[1])
else
extract_string(default_arg_node.children[1])
end
end

[key, default_arg]
end
Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def show()
# default arg
I18n.t('default_arg', default: 'Default Text')

I18n.t('default_plural_arg', default: { one: 'One Text', other: 'Other Text' })

# only `t()` calls can use relative keys and not `I18n.t()` calls.
I18n.t('.not_relative')

Expand Down
2 changes: 2 additions & 0 deletions spec/i18n_tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
index.my_custom_scanner.title
magic_comment
default_arg
default_plural_arg
.not_relative
scope.subscope.a.b
scope.key_in_erb
Expand Down Expand Up @@ -263,6 +264,7 @@
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'
expect(YAML.load_file('config/locales/en.yml')['en']['default_plural_arg']).to eq({ 'one' => 'One Text', 'other' => 'Other Text' })
end
end

Expand Down

0 comments on commit 497454f

Please sign in to comment.