-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Adds a class for matching Rails model translations. - User.human_attribute_name('name') and User.model_name.human(count: 2)
- Loading branch information
1 parent
1c3bb63
commit 22e1d38
Showing
6 changed files
with
156 additions
and
23 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
lib/i18n/tasks/scanners/ast_matchers/rails_model_matcher.rb
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,63 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'i18n/tasks/scanners/results/occurrence' | ||
|
||
module I18n::Tasks::Scanners::AstMatchers | ||
class RailsModelMatcher < BaseMatcher | ||
def convert_to_key_occurrences(send_node, _method_name, location: send_node.loc) | ||
human_attribute_name_to_key_occurences(send_node: send_node, location: location) || | ||
model_name_human_to_key_occurences(send_node: send_node, location: location) | ||
end | ||
|
||
private | ||
|
||
def human_attribute_name_to_key_occurences(send_node:, location:) | ||
children = Array(send_node&.children) | ||
receiver = children[0] | ||
method_name = children[1] | ||
|
||
return unless method_name == :human_attribute_name && receiver.type == :const | ||
|
||
value = children[2] | ||
|
||
model_name = receiver.to_a.last.downcase | ||
attribute = extract_string(value) | ||
key = "activerecord.attributes.#{model_name}.#{attribute}" | ||
[ | ||
key, | ||
I18n::Tasks::Scanners::Results::Occurrence.from_range( | ||
raw_key: key, | ||
range: location.expression | ||
) | ||
] | ||
end | ||
|
||
# User.model_name.human(count: 2) | ||
# s(:send, | ||
# s(:send, | ||
# s(:const, nil, :User), :model_name), :human, | ||
# s(:hash, | ||
# s(:pair, | ||
# s(:sym, :count), | ||
# s(:int, 2)))) | ||
def model_name_human_to_key_occurences(send_node:, location:) | ||
children = Array(send_node&.children) | ||
return unless children[1] == :human | ||
|
||
base_children = Array(children[0]&.children) | ||
class_node = base_children[0] | ||
|
||
return unless class_node&.type == :const && base_children[1] == :model_name | ||
|
||
model_name = class_node.to_a.last.downcase | ||
key = "activerecord.models.#{model_name}.other" | ||
[ | ||
key, | ||
I18n::Tasks::Scanners::Results::Occurrence.from_range( | ||
raw_key: key, | ||
range: location.expression | ||
) | ||
] | ||
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
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