react-gettext-parser
react-gettext-parser copied to clipboard
Add overrideContext configuration
Adds ability to override context parsed from code with a list of overrides.
This is useful for the common case of languages that have variations based on gender where the usual approach with gettext is to use contexts for managing the gender variations.
Thanks for the submit!
The problem with this is that a static code analyser like react-gettext-parser won't be able to resolve gender or its possible values when doing the extraction. It has to be pure strings (like you did in the test you wrote).
Not sure how to tackle this... 🤔 But I think we need to solve that before looking at the implementation.
That's exactly right, the parser has no way of knowing the contexts, if there is a need to do this kind of thing you'd want it to be passed in via configuration (as in the examples/tests).
The solution I provided addresses the problem I was facing when trying to think about how to have multiple contexts. The assumption is that the caller knows what contexts to expect then they want to override them.
In my example I'm using gender, but it could be something else (not a linguist, so not sure what other use cases there might be). Definitely a need for something like this to handle french, spanish, and probably several other languages.
Otherwise you end up with something like this:
{ person.gender === "male" ? (
<T
message="Dear {{ name }}, there is one potato left"
messagePlural="Dear {{ name }}, there are {{ count }} potatoes left"
count={numPotatoes}
name={person.name}
context="male"
/>
)
:
(
<T
message="Dear {{ name }}, there is one potato left"
messagePlural="Dear {{ name }}, there are {{ count }} potatoes left"
count={numPotatoes}
name={person.name}
context="female"
/>
)
}
which is the only way to get to have the proper contexts extracted from the source. Obviously this is far from ideal as there's lots of needless duplication.
The static code analysis needs not care about the context in the components (which is why I called it an override, as it discards whatever value is in the context and overrides with with values from the provided configuration list).