Skip to main content

Translating

A component is translated as any WordPress plugin is, with one step of its own for the words of its kit.json. The plugin tests take the starter of Building a component through every step on this page and check it in German (tests/specs/component-guide.spec.ts).

The words in PHP​

Every word the code of a component shows goes through the functions of WordPress with the text domain of its plugin, the name of its folder: __(), _x(), esc_html__() and the rest. The starter labels its item in the member menu so:

'label' => __( 'Add a recipe', 'poly-community-recipes' ),

The words of its manifest​

The title, description and summary in kit.json, and the labels of its pages, menu items and tabs, are translated by Poly Community with the text domain the manifest names in textDomain, and the context Kit. Without textDomain, they are looked up in the text domain of Poly Community, where they are not found.

wp i18n make-pot reads PHP and JavaScript but not kit.json. So list those words in a PHP file of their own, which nothing loads, as Poly Community does for its own components:

<?php
// The words of kit.json, for translators: wp i18n make-pot finds them here, with the context Kit that Poly Community
// translates them with. Nothing loads this file.
defined( 'ABSPATH' ) || exit;

return array(
_x( 'Recipes', 'Kit', 'poly-community-recipes' ),
_x( 'A page of recipes, the example of the component guide', 'Kit', 'poly-community-recipes' ),
);

Write each word exactly as it is in kit.json. One word used for the title and the menu label is listed once.

The words of its templates​

The words in a template of the component, templates/{view}.html, are blocks with their text, as in any block template. They are not translated by the files below. With a block theme, the site owner changes them in Appearance → Editor. Keep the words a site needs translated in PHP, in a block of the component, and its templates to blocks.

The files​

  1. Make the template of the translations from the folder of the plugin:

    wp i18n make-pot wp-content/plugins/poly-community-recipes languages/poly-community-recipes.pot
  2. Translate it into a .po file for each language, named by the text domain and the locale, such as poly-community-recipes-de_DE.po, with any editor of .po files:

    msgctxt "Kit"
    msgid "Recipes"
    msgstr "Rezepte"

    msgid "Add a recipe"
    msgstr "Ein Rezept hinzufügen"
  3. Make the file WordPress reads from it:

    wp i18n make-mo poly-community-recipes-de_DE.po wp-content/languages/plugins/

WordPress loads wp-content/languages/plugins/poly-community-recipes-de_DE.mo by itself on a site in German, with no code in the plugin. On the test site, the menu item of the starter reads Rezepte and its item in the member menu Ein Rezept hinzufügen.

A plugin on WordPress.org gets its translations from translate.wordpress.org the same way, once translators have made them there.

See also Building a component and Settings → Words, where a site owner gives any text of Poly Community itself words of the site own.