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

Expose Polyglot.t() to onMissingKey to allow substitutions #67

Open
gomesalexandre opened this issue May 20, 2022 · 2 comments
Open

Expose Polyglot.t() to onMissingKey to allow substitutions #67

gomesalexandre opened this issue May 20, 2022 · 2 comments

Comments

@gomesalexandre
Copy link

Currently, react-polyglot allows to use onMissingKey from node-polyglot, see:
https://github.com/airbnb/polyglot.js/blob/6c10c6f3eacec8f23deb4635dfafca862167161e/index.js#L406

Unfortunately, since there is no exposed translation function that allows substitutions, the passed onMissingKey function will have to implement its own substitution logic, effectively using node-polyglot exports. Here is an example of such implementation:

  const onMissingKey = (key: string, substitutions?: InterpolationOptions) => {
    const translation = get(translations['en'], key)
    return transformPhrase(translation, substitutions)
  }

It would be great to make the translation function available so that implementors of onMissingKey can benefit from the already available translation context.

Proposed solutions:

  1. Augment onMissingKey with an additional t parameter (probably not the best for arity reasons, as locale will have to be passed)
  2. Export the transformPhrase or Polyglot.t translation function - this way, there would be no back and forth switching between the two libraries needed to implement this.

Note that if we go with this, InterpolationOptions will need to be exported, as onMissingKey is typed like so, and implementations using TS need to type it if they enabled noImplicitAny in their tsconfig:

(property) PolyglotOptions.onMissingKey?: ((key: string, options?: InterpolationOptions | undefined, locale?: string | undefined) => string) | undefined

Happy to implement it if that's something of interest! I could also document it/add examples as documentation is missing for it on both node-polyglot and on this library.

@nayaabkhan
Copy link
Owner

Hey @gomesalexandre, thanks for putting all the work into the details.

On reading, I don't see any problems with the idea itself. I'm just wondering about the use-cases where this would be useful. It's hard for me to imagine any as I've not needed it myself. Can you provide one or a few?

@0xdef1cafe
Copy link

Hey @gomesalexandre, thanks for putting all the work into the details.

On reading, I don't see any problems with the idea itself. I'm just wondering about the use-cases where this would be useful. It's hard for me to imagine any as I've not needed it myself. Can you provide one or a few?

hey @nayaabkhan - please see this PR as an example shapeshift/web#1858

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

No branches or pull requests

3 participants