Internationalization#
Making Text Translatable#
Volto uses react-intl
to translate strings.
We start by importing the component:
import { FormattedMessage } from 'react-intl';
Then we can use the FormattedMessage
component to translate a string:
<FormattedMessage
id="Some string"
defaultMessage="Some string"
/>
Extracting i18n Strings#
Volto provides an i18n extraction script to get all translatable strings from your application. Run the following:
yarn i18n
This will generate the following output:
Extracting messages from source files...
Synchronizing messages to pot file...
Synchronizing messages to po files...
Generating the json files...
done!
As the description suggests it will first extract all messages from the source files into a .json
file.
Then it will synchronize the extracted messages with the .pot
master file and with all the .po
files found in the project.
After that it will generate the final .json
files which are used in the application.
If you want to translate the strings edit the .po
files and run the script again.
This script will combine the messages located in Volto itself and the current project, and combine them into the .json
files.
Exercise#
Translate the Tags:
label which was added in the previous chapter to Dutch and German.
Translate Attributes#
When you want to translate attributes you can't use the FormattedMessage
component since it will generate markup.
In order to translate a normal string or attribute you can use the formatMessage
method of react-intl
.
First you need to import all the required methods:
import { defineMessages, injectIntl, intlShape } from 'react-intl';
Then you can use the defineMessages
method to define your messages:
const messages = defineMessages({
site: {
id: 'Site',
defaultMessage: 'Site',
},
});
In order to use the formatMessage
method you have to inject the intl
object into your class or function.
For pure functions you can use:
export default injectIntl(Logo);
And for classes you can use:
@injectIntl
class Logo extends Component {
}
You can use the provided PropType
as follows:
Tags.propTypes = {
tags: PropTypes.arrayOf(PropTypes.string),
intl: intlShape.isRequired,
};
Now we can use the method like this:
<Link to="/" title={intl.formatMessage(messages.site)}>
Exercise#
Add a title to the tag links with the message Search for tag {tag}
.