martinandert / react-translate-component

A component for React that utilizes the Counterpart module to provide multi-lingual/localized text content.
MIT License
322 stars 31 forks source link

Missing translation could output given string #27

Open micky2be opened 7 years ago

micky2be commented 7 years ago

In my code I have some cases where the string is pass to a function. I use Translate within this function to make it easier but some string are not keys of the translation file. Would be nice to have an option to tell the system to just output the string as is.

martinandert commented 7 years ago

In order to broading my understanding of what you're trying to achieve, could you please provider an example (a stripped-down code snippet or similar) for your use case?

Also, how should the API for the "option to tell the system" look like?

micky2be commented 7 years ago

I have a simple function showError triggered by a form which output a component. Errors can come from the server (already a string). Or by a local validation (translation key).

As a API, the following should be enough.

const Translate = require('react-translate-component');
Translate.allowRawString = true;

And even:


<Translate content={ error } allowRaw />
martinandert commented 7 years ago

You mean that the string passed as content prop should not be used as a key to lookup a translation?

So

<Translate component="h1" content="something" />

just returns

<h1>something</h1>

?

micky2be commented 7 years ago

Yes, indeed

martinandert commented 7 years ago

Yes, indeed

Then why not simply go with <h1>something</h1> in the first place? I'm perhaps missing something obvious here.

micky2be commented 7 years ago

The coming string is a variable. Can be a raw string or a key. If the key cannot be translated I want to display the key as a raw string instead of an error.

On Nov 28, 2016 7:44 PM, "Martin Andert" notifications@github.com wrote:

Yes, indeed

Then why not simply go with

something

in the first place?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/martinandert/react-translate-component/issues/27#issuecomment-263239402, or mute the thread https://github.com/notifications/unsubscribe-auth/AAseRDT3gUFgS4CGv8YsyDsxf4VBAxp4ks5rCrBzgaJpZM4K8Tmj .

martinandert commented 7 years ago

Ah, ok. Now I get it.

What about using the fallback prop:

<Translate content={maybeKey} fallback={maybeKey} />

See also https://github.com/martinandert/counterpart#fallbacks

micky2be commented 7 years ago

Hmm, a bit hugly IMHO but could try it. My cases are limited so should be fine.

adrianhelvik commented 6 years ago
// src/node_modules/translate.js
export default ({ content, ...props }) => (
  <Translate
    content={content}
    fallback={content}
    {...props}
  />
)
erpardeepjain commented 4 years ago

@martinandert fallback seems valid if one would have limited translation strings, may be for project having 1000's of translation text it would be very painful to add fallback overtime for a safer side.

It could be default or global options, again just a suggestion for the same.