Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support partially dynamic segments #509

Merged
merged 4 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ A special syntax similar to file glob patterns is used throughout i18n-tasks to
|:------------:|:----------------------------------------------------------|
| `*` | matches everything |
| `:` | matches a single key |
| `*:` | matches part of a single key |
| `{a, b.c}` | match any in set, can use `:` and `*`, match is captured |

Example of usage:
Expand Down
2 changes: 2 additions & 0 deletions lib/i18n/tasks/key_pattern_matching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def compile_patterns_re(key_patterns)
# In patterns:
# * is like .* in regexs
# : matches a single key
# *: matches part of a single key, equivalent to `[^.]+?` regex
# { a, b.c } match any in set, can use : and *, match is captured
def compile_key_pattern(key_pattern)
return key_pattern if key_pattern.is_a?(Regexp)
Expand All @@ -31,6 +32,7 @@ def compile_key_pattern(key_pattern)
def key_pattern_re_body(key_pattern)
key_pattern
.gsub(/\./, '\.')
.gsub(/\*:/, '[^.]+?')
.gsub(/\*/, '.*')
.gsub(/:/, '(?<=^|\.)[^.]+?(?=\.|$)')
.gsub(/\{(.*?)}/) { "(#{Regexp.last_match(1).strip.gsub(/\s*,\s*/, '|')})" }
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/tasks/used_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def strict?(strict)

# keys in the source that end with a ., e.g. t("category.#{ cat.i18n_key }") or t("category." + category.key)
# @param [String] replacement for interpolated values.
def expr_key_re(replacement: ':')
def expr_key_re(replacement: '*:')
@expr_key_re ||= begin
# disallow patterns with no keys
ignore_pattern_re = /\A[.#{replacement}]*\z/
Expand Down
17 changes: 17 additions & 0 deletions spec/key_pattern_matching_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@
end
end

describe '*:' do
it 'as suffix' do
expect('a.b.pre-*:-post').to match_key 'a.b.pre-c-post'
expect('a.b.pre-*:-post').not_to match_key 'a.b.pre-c.-post'
end

it 'as prefix' do
expect('pre-*:-post.b.c').to match_key 'pre-a-post.b.c'
expect('pre-*:-post.b.c').not_to match_key 'pre-.a-post.b.c'
end

it 'as infix' do
expect('a.pre-*:-post.c').to match_key 'a.pre-b-post.c'
expect('a.pre-*:-post.c').not_to match_key 'a.pre-b.-post.c'
end
end

describe '{sets}' do
it 'matches' do
p = 'a.{x,y}.b'
Expand Down