Translating administrative interfaces

The interface translation module provides a method for translating strings found in the Drupal user interface. Harnessing the Language module, interface translations are automatically downloaded from the Drupal translation server. By default, the interface language is loaded through the language code as a path prefix. With the default Language configuration, paths will be prefixed with the default language.

Interface translations are based on strings provided in the code that are passed through the internal translation functions.

In this recipe, we will enable Spanish, import the language files, and review the translated interface strings to provide missing or custom translations.

Getting ready

Drupal 8 provides an automated installation process of translation files. For this to work, your web server must be able to communicate with https://localize.drupal.org/. If your web server cannot automatically download the files from the translation server, you can refer to the manual installation instructions, which will be covered in the There's more section.

How to do it…

  1. Go to Extend and install the Interface Translation module. It will prompt you to enable the Language, File, and Field modules to be installed as well if they are not.
  2. After the module is installed, click on Configuration. Go to the Languages page under the Regional and Language section.
  3. Click on Add language in the languages overview table:
    How to do it…
  4. The Add language page provides a select list of all available languages that the interface can be translated to. Select Spanish, and then click on Add language.
  5. A batch process will run, install the translation language files, and import them.
  6. The INTERFACE TRANSLATION column specifies the percentage of active translatable interface strings that have a matching translation. Clicking on the link allows you to view the User interface translation form:
    How to do it…
  7. The Filter Translatable Strings form allows you to search for translated strings or untranslated strings. Select Only untranslated strings from the Search in select list and click on Filter.
  8. Using the text box on the right-hand side of the screen, a custom translation can be added to Only untranslated strings. Type in a translation for the item.
    How to do it…
  9. Click on Save translations to save the modification.
  10. Go to /es/node/add and you will notice that the Basic page content type description will now match your translation.

How it works…

The interface translation module provides DrupallocaleLocaleTranslation, which implements DrupalCoreStringTranslationTranslatorTranslatorInterface. This class is registered under the string_translation service as an available lookup method.

When the t function or the DrupalCoreStringTranslationStringTranslationTrait::t method is invoked, the string_translation service is called to provide a translated string. The string_translation service will iterate through the available translators and return a translated string, if possible.

Tip

Developers need to note that this is a key reason to ensure that module strings are passed through translation functions. It allows you to identify strings that need to be translated.

The translator provided in the interface translation will then attempt to resolve the provided string against known translations for the current language. If a translation has been saved, it will be returned.

There's more…

We will explore ways to install other languages, check translation statuses, and many more in the following sections.

Manually installing language files

Translation files can be manually installed by downloading them from the Drupal.org translation server and uploading them through the language interface. You can also use the import interface to upload custom Gettext Portable Object (.po) files.

Drupal core and most contributed projects have .po files available at the Drupal translations site, https://localize.drupal.org. On the site, click on Download and you will be able to download a .po file for Drupal core in all available languages. Additionally, clicking on a language will provide more translations for a specific language across projects.

Manually installing language files

You can import a .po file by going to the User interface translation form and selecting the Import tab. You need to select the .po file and then the appropriate language. You have the ability to treat the uploaded files as custom created translations. This is recommended if you are providing a custom translation file that was not provided by Drupal.org. If you are updating Drupal.org translations manually, make sure that you check the box that overwrites existing noncustom translations. The final option allows you to replace customized translations if the .po file provides them. This can be useful if you have translated missing strings that might now be provided by the official translation file.

Checking translation status

As you add new modules, the available translations will grow. The Interface translation module provides a translation status report that is accessible from the Reports page. This will check the default translation server for the project and check whether there is a .po available or if it has changed. In the event of a custom module, you can provide a custom translation server, which is covered in Providing translations for a custom module.

If an update is available, you will be alerted. You can then import the translation file updates automatically or download and manually import them.

Exporting translations

In the User interface translation form, there is an Export tab. This form will provide a Gettext Portable Object (.po) file. You have the ability to export all the available source text that is discovered in your current Drupal site without translations. This will provide a base .po for translators to work on.

Additionally, you can download a specific language. Specific language downloads can include noncustomized translations, customized translations, and missing translations. Downloading customized translations can be used to help make contributions to the multilingual and internationalization efforts of the Drupal community!

Interface translation permissions

The interface translation module provides a single permission called Translate interface text. This permission grants users the permission to interact with all of the module's capabilities. It is flagged with a security warning as it allows users with this permission to customize all the output text presented to users.

However, it does allow you to provide a role for translators and limits their access to just translation interfaces.

Using interface translation to customize default English strings

The interface translation module is useful beyond its typical multilingual purposes. You can use it to customize strings in the interface that are not available to be modified through typical hook methods, or if you are not a developer!

Firstly, you will need to edit the English language from the Languages screen. Check the checkbox for Enable interface translation for English and click on Save language. You will now have the ability to customize existing interface strings.

Note

This is only recommended for areas of the interface that cannot already be customized through the normal user interface or provided API mechanisms.

Interface text language detection

The Language module provides detection and selection rules. By default, the module will detect the current language based on the URL, with the language code acting as a prefix to the current path. For example, /es/node will display the node listing page in Spanish:

Interface text language detection

You can have multiple detection options enabled at once and use ordering to decide which takes precedence. This can allow you to use the language code in the URL first, but, if missing, a fallback to the language is specified by the user's browser.

Some detection methods have settings. For instance, the URL detection method can be based on the default path prefix or subdomains.

Providing translations for a custom module

Modules can provide custom translations in their directories or point to a remote file. These definitions are added to the module's info.yml file. First, you need to specify the interface translation project key if it differs from the project's machine name.

You need to then specify a server pattern through the interface translation server pattern key. This can be a relative path to Drupal's root, such as modules/custom/mymodule/translation.po, or a remote file URL at http://example.com/files/translations/mymodule/translation.po.

Distributions (or other modules) can implement hook_locale_translation_projects_alter to provide this information on behalf of modules or alter defaults.

The server pattern accepts the following different tokens:

  • %core for the version of a course (for example, 8.x)
  • %project for the project's name
  • %version for the current version string
  • %language for the language code

More information on the interface translation keys and variables can be found in the local.api.php document file located in the interface translation module's base folder.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset