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

[Question] How to check if translation string/id exists? #747

Closed
tswardenski opened this issue Nov 22, 2016 · 3 comments
Closed

[Question] How to check if translation string/id exists? #747

tswardenski opened this issue Nov 22, 2016 · 3 comments
Labels

Comments

@tswardenski
Copy link

I have a bit of an odd scenario where I'm displaying a list of mixed values; some system default string values that need to be translated, and some user defined strings that shouldn't be translated.

I've gone through the docs a handful of times to see if there's a "pretty" way to check if a translation string ID exists and I ended up just implementing:

if (formatMessage({ id: `some.key.${value}` }) !== `some.key.${value}`) { 
  // do stuff
}
else {
 // do other stuff
}

I don't like this method as it ends up giving me a TON of console errors. I'm wondering if there's a better way to do this. Any direction is appreciated!

Thanks!

@ericf
Copy link
Collaborator

ericf commented Nov 22, 2016

@tswardenski yeah this has come up before. My recommendation is to create a component to handle this. I'm not sure on your exact API of how to you want to receive value, but here's a place to start: #488 (comment)

@ericf ericf added the support label Nov 22, 2016
@tswardenski
Copy link
Author

@ericf thanks for the reference. It looks like maybe I could get away with doing something like this?

const stringExists = !!this.props.intl.messages[`some.key.${value}`];

Thoughts?

@ericf
Copy link
Collaborator

ericf commented Dec 1, 2016

Yeah, exactly!

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

No branches or pull requests

2 participants