diff --git a/lib/i18n/tasks/scanners/ruby_ast_scanner.rb b/lib/i18n/tasks/scanners/ruby_ast_scanner.rb index a1ee0ff6..4c8f21c2 100644 --- a/lib/i18n/tasks/scanners/ruby_ast_scanner.rb +++ b/lib/i18n/tasks/scanners/ruby_ast_scanner.rb @@ -88,8 +88,13 @@ def send_node_to_key_occurrence(send_node, method_name, location: send_node.loc) extract_string(default_arg_node.children[1]) end end - [absolute_key(key, location.expression.source_buffer.name, method_name), - range_to_occurrence(location.expression, default_arg: default_arg)] + full_key = if send_node.children[0].nil? + # Relative keys only work if called via `t()` but not `I18n.t()`: + absolute_key(key, location.expression.source_buffer.name, method_name) + else + key + end + [full_key, range_to_occurrence(location.expression, default_arg: default_arg)] end end diff --git a/spec/fixtures/app/controllers/events_controller.rb b/spec/fixtures/app/controllers/events_controller.rb index 12c6ff7a..c6e28fcb 100644 --- a/spec/fixtures/app/controllers/events_controller.rb +++ b/spec/fixtures/app/controllers/events_controller.rb @@ -37,6 +37,9 @@ def show() # default arg I18n.t('default_arg', default: 'Default Text') + + # only `t()` calls can use relative keys and not `I18n.t()` calls. + I18n.t('.not_relative') end def update diff --git a/spec/i18n_tasks_spec.rb b/spec/i18n_tasks_spec.rb index 75fae50b..f1612712 100644 --- a/spec/i18n_tasks_spec.rb +++ b/spec/i18n_tasks_spec.rb @@ -64,6 +64,7 @@ index.my_custom_scanner.title magic_comment default_arg + .not_relative ) } let (:expected_missing_keys_diff) {