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] Best way to translate without the component? #983
Comments
@kud you can skip |
I have the same question and I realy don't get your answer. How can I skip |
Thank you. It was the next thing I just attempted. For me this is not obvious from the docs, maybe it should be elaborated a little bit more. So again, thank you very much @okovpashko . |
I access the child Every time this comes up, someone warns you "to not use context" because it's semi-internal, semi-stable, experimental and/or not-recommended in React. But Anyway, some code: // Intl.js
import { intlShape } from 'react-intl';
/** @type {intlShape} */
var intl;
/** React component to capture the context. */
export function IntlCapture(props, context) {
// Other examples show this access being done in `componentWillMount`.
// This works just fine.
intl = context.intl;
// Done. Just return the children to be rendered.
return props.children;
}
IntlCapture.contextTypes = {
intl: intlShape.isRequired
};
/** This is the wrapper for `intl` where I expose its functionality. */
class _Intl {
t(message, values) { return intl.formatMessage(message, values); }
// ...
async load(locale) {
// ... some code to fetch a locale translation file from `/locales/${locale}.json`.
}
}
const Intl = new _Intl();
export default Intl; // index.js
import { IntlProvider } from 'react-intl';
import Intl, {IntlCapture} from './lib/Intl';
function render() {
const rootContainer = document.getElementById('root');
const rootElement = (
<IntlProvider
locale={Intl.locale}
defaultLocale={Intl.defaultLocale}
messages={Intl.messages}>
<IntlCapture>
<ReduxProvider store={store}>
<App />
</ReduxProvider>
</IntlCapture>
</IntlProvider>
);
ReactDOM.render(rootElement, rootContainer);
}
Intl.load('en').then(render).catch(render); NOTE: In the implementation of my See also: #749 - String without FormattedMessage |
One other note - if you make an Intl wrapper, be aware that you cannot wrap the |
One mistake that I made - don't name your module I wanted a short name, so I renamed it to |
In your wrapper component you could always assign the function to the document. NOT a fan of this hack but it works. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Issue was closed because of inactivity. If you think this is still a valid issue, please file a new issue with additional information. |
Hey,
I'd like to know what's the best way to translate without using the component.
For the moment, I do this:
But it seems to be for me a bit too verbose or non-direct. It's a quite annoying to have to use definesMessages first.
Isn't possible to do a way like:
placeholder={intl.formatMessage(search.searchbox.placeholder)}
orplaceholder={intl.formatMessage(defineMessage(search.searchbox.placeholder))}
?The text was updated successfully, but these errors were encountered: