-
-
Notifications
You must be signed in to change notification settings - Fork 125
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 react-i18n <Trans> components #44
Conversation
This adds basic support for use of Trans components. There are probably still a few missing pieces, but it seems to work for me.
2 similar comments
There are a few limitations in this code:
I don't know what the exact numbering rules for Trans are, so I am not sure if it makes sense to try and fix things here with trial and error. Fixing up the |
numbering rules are based on the index of the node: https://github.com/i18next/react-i18next/blob/master/src/trans.js#L15 |
Hm, so to fully support it most likely I will need to add a HTML parser that can also deal with |
I've pushed a reimplementation which switches to using parse5 to parse the fragment to build an AST, and then use that to generate output. The result should be completely accurate generating of text. A possible next step might be to convert parseAttrFromString to work from the same AST as well so it doesn't have to try and parse HTML using regular expressions. That should simplify the code quite a bit as well. Let me know if you want me to add that. |
// Parses translation keys from `Trans` components in JSX | ||
// <Trans i18nKey="some.key">Default text</Trans> | ||
parseTransFromString(content, opts = {}, customHandler = null) { | ||
const pattern = '<Trans[^]*?i18nKey="([^"]+)"[^]*?>([^]*?)</\\s*Trans\\s*>'; |
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.
@wichert Is it possible to make Trans
and i18nKey
configurable?
I strongly agree with your suggestion to convert Thank you for your contribution to make it happen. |
@cheton Done! I could make Trans and i18nKey configurable, but I'm not entirely sure what the use case for that is so I'm a bit hesitant to complicate the code. Are you thinking of other i18next wrappers that use different elements? I'm not sure what the integrations with AngularJS, Vue or Aurelia look like from memory. I'ld like to know that so I can make a better decision about the API; in particular if the values should be strings or regular expressions. |
This adds basic support for use of Trans components. There are probably still a few missing pieces, but it seems to work for me.