-
-
Notifications
You must be signed in to change notification settings - Fork 411
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
Add I18n.interpolation_keys #682
Conversation
I'd be keen to know what this project is :) It sounds a little in the vein of LocaleApp, etc. I think the feature is lightweight enough and it could be useful for others. Please do add some tests here. |
end | ||
|
||
test "interpolation_keys returns an empty array when missing translation " do | ||
assert_equal [], I18n.interpolation_keys("does-not-exist") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could raise an exception or return nil
, but in the interest of simplicity and consistency, I feel it's probably fine to return []
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think []
is better here as it makes the return type consistently Array
rather than Array | nil
|
||
test "interpolation_keys returns an empty array when nested translation" do | ||
store_translations(:en, "example_nested" => { "one" => "One %{foo}", "two" => "Two %{bar}" }) | ||
assert_equal [], I18n.interpolation_keys("example_nested") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this scenario, I18n.t("example_nested")
does not require/use any interpolation keys (even though the child translations do). Therefore I feel it's correct for the method to return []
.
when ::String | ||
translation.scan(Regexp.union(I18n.config.interpolation_patterns)) | ||
when ::Array | ||
translation.map { |element| interpolation_keys_from_translation(element) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this does need to be recursive!
5362318
to
7559fef
Compare
@radar Sorry for the slow progress, I was on holiday 😄 I've gone through some more edge cases, added more documentation and tests. Please take a look when you have time! |
I did have applications like that in mind when proposing to push this feature into the main In an application, translations may or may not utilise interpolation keys, in each locale. Sometimes, resolving those keys is an "expensive" operation. So as an optimisation, it makes sense to skip trying to resolve any keys that aren't actually being used. |
Thanks! |
Adds a new method,
I18n.interpolation_keys
, which returns a list of keys needed to fully resolve a translation.Some examples:
In a recent project, I wanted to be able to figure out "What keys are needed for this translation"?
To my surprise, there wasn't a built-in method for this in
I18n
; the only option was to hand-roll some regex to look for%{...}
patterns in the translation.So now I've tried to implement it here "properly", making use of
I18n.config.interpolation_patterns
to be more robust with how interpolations are identified by the library -- e.g. it will also match%<foo>.d
by default, and respect custom pattern configurations like{{foo}}
.