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

Add overrideContext configuration #76

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

marksteele
Copy link

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.

@alexanderwallin
Copy link
Collaborator

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.

@marksteele
Copy link
Author

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants