Skip to content

Internationalization#

Internationalization (i18n) is the process of creating user interfaces which are suitable for different languages and cultural contexts. Volto uses the library react-intl to provide translations for any potential language. Anything you can read in the official documentation of react-intl also applies for Volto.

However this section teaches you about the most common use cases relating to i18n you probably will have when developing your own Add-on or contributing to the Volto core itself.

Broad overview#

The workflow for creating new translatable texts is as follows:

  1. Create translatable i18n strings in your code
  2. Extract all i18n strings from your code with a script and create artifacts like .po and .pot files
  3. Use your favorite editor to translate all i18n strings (i.e. edit the .po files)
  4. Re-run the script, which then moves the translations from the .po files into for Volto usable .json files

This way of organizing translations relies on gettext, a proven and established system with great tool support.

All translation files are located under the directory locales. This might look like this:

$ tree locales/
locales/
├── de
│   └── LC_MESSAGES
│       └── volto.po
├── de.json
├── en
│   └── LC_MESSAGES
│       └── volto.po
├── en.json
├── it
│   └── LC_MESSAGES
│       └── volto.po
├── it.json
├── nl
│   └── LC_MESSAGES
│       └── volto.po
├── nl.json
└── volto.pot

8 directories, 9 files

The file volto.pot holds all extracted i18n strings and acts as master template for all the *.po files. The translation for each language is stored within a dedicated sub-directory (like en for English, it for Italian, etc.) and are stored as *.po file and separately stored directly under locales as *.json file.

Creating i18n Strings#

Translating Text Within HTML Elements#

react-intl can identify translatable texts with the FormattedMessage components. As the name of this component suggests, it is also possible to format your messages as your liking.

This is an example of how you can write a text with contents Hello World, which can be identified via hello_world:

import { FormattedMessage } from 'react-intl';

function HelloWorld(props) {
  return (
    <div>
      <FormattedMessage
        id="hello_world"
        defaultMessage="Hello World"
      />
    </div>
  );
}

The identifier hello_world is then commonly used between all the translations. There are also more features available such as using placeholders. See the docs for all features in the FormattedMessage component.

Translating Attributes#

As FormatMessage is only suitable for creating text within HTML elements, it cannot be used for translating individual attributes. But with the method formatMessage there exists another way to translate primitive strings.

This approach can be best explained with an example: Assume you have a component called TeaserImage which contains an image that has for accessibility reasons the alt attribute.

To translate the alt attribute, you have to do the following steps:

  1. Import the following necessary methods:
    import { defineMessages, injectIntl, intlShape } from 'react-intl';
    
  2. Define a message (or more) via defineMessages:
    const messages = defineMessages({
      teaserAltText: {
        id: 'teaser_alt_text',
        defaultMessage: 'Teaser Alt Text',
      },
    });
    
  3. As the method formatMessage in our component class/function is needed, there is a special propery intl, that needs to be injected with either the following ways:
    // When using a pure function:
    export default injectIntl(TeaserImage);
    
    // OR when using a component:
    @injectIntl
    class TeaserImage extends Component {
      ...
    }
    
    Since you now have another prop available, it has to be to properly defined in the propTypes:
    TeaserImage.propTypes = {
      intl: intlShape.isRequired,
      ...
    };
    
  4. As last step, the method can be used like this:
    <img src="..." alt={intl.formatMessage(messages.teaserAltText)}>
    

Extracting i18n Strings#

Volto provides an i18n extraction script to get all translatable strings from your application.

This script can be invoked by this command:

$ 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 output suggests it will first extract all messages from the source files into .json files. Then it will synchronize the extracted messages with the .pot master template and with all the .po files found in the project. This script will combine the messages located in Volto itself and the current project, and combine them into the .json files.

Contributing translations for a unsupported language#

The Volto project welcomes all speakers from the world to include any language, which is not supported yet.