-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Getting formatted message without JSX or react just the formatted string for titles, aria-labels etc. #157
Comments
I found a solution using intl-messageformat something like:
any better ways of doing this? |
This has been addressed on the |
See: #162 |
How exactly is this addressed in v2-dev? I see that you can use |
@bartvanandel you're looking in the wrong place then. This lib is React bindings to the |
@ericf I know what this lib is doing. We are using it in a React app with internationalized components. This app however also uses classes which are not visual components (i.e. data managers), which also have the ability to generate messages. These should be internationalized as well, preferably using the same approach of calls to Edit: I see now that there was some discussion on this subject in the comments of #162, but I did not find a (good) solution there. Maybe using Redux could help us solve the issue, using something like mentioned in the "deprecation" message here: https://github.com/overblog/react-redux-intl. Will investigate. |
In order to use this lib, any formatting must happen within the context of an
You'd have to provide more details about what you mean here. |
Fair enough. What I'm trying to do here is making sure that non-visual components don't end up as React Components, which are essentially made to be rendered. Example: we have a set of classes which parse configuration files which are read from a server. Obviously such a configuration parser, and the configuration data itself, are not visual components and should therefore not derive from React Component in my opinion. But when it comes to strings, we are providing some defaults (fallback only, using getters) which should be translated to the language of the user. Hence, internationalization outside of React Components. Currently I've written a workaround which basically relies on the application being initialized (which it always is). Inside our main app I'm including a |
Do what exactly? |
Provide a way to use at least some part of the functionality outside of React Components. Like Our current workaround: CurrentLocale.js, which is rendered once in the App main: import React from 'react';
import {injectIntl} from 'react-intl';
// Does not actually render anything visible.
// We need it to provide access to internationalization for classes
// which are not a React component
class CurrentLocale extends React.Component {
static instance: CurrentLocale = null;
componentWillMount() {
if (!CurrentLocale.instance)
CurrentLocale.instance = this;
}
render() {
return false;
}
}
export default injectIntl(CurrentLocale);
export function intl() {
return CurrentLocale.instance.props.intl;
}
export function formatMessage(...args) {
return intl().formatMessage(...args);
} SomeClass.js, which is not a React component and does not even use React: import {defineMessages} from 'react-intl';
import {formatMessage} from './../intl/CurrentLocale.js';
const messages = defineMessages({
name: {
id: "MyClass.name",
description: "Default name",
defaultMessage: "Name"
}
});
export default class SomeClass {
_name: string;
nameIntl: string;
constructor({name=undefined, nameIntl="MyClass.name", ...args}) {
this.name = name;
this.nameIntl = nameIntl;
}
// ...
get name():string {
return this._name || (this.nameIntl ? formatMessage({id: this.nameIntl}) : null);
}
} Note: we're using Webpack to bundle everything together. |
How is React Intl uses the |
That depends. One use is that we are creating some OpenLayers layers, which by default get a localized name (using the It's not like we cannot refactor everything to be a React component, it's just that it does not make sense so I don't want it. Can we still rely on the |
If the map is being initialized in React via
Yes, this method just echoes its input; it's a hook for |
For anyone looking for a solution of this, with the hook useIntl they made it possible. |
Hey guys,
I have a react-intl/redux project and I am just wondering how can I format the string just without react component?
I need this for titles, aria-lable etc.
I currently have my own getIntlMessage(path) method I am just wondering how to apply the format to it as well.
for example:
Regards,
Andrei
The text was updated successfully, but these errors were encountered: