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.
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.
/es/node/add
and you will notice that the Basic
page content type description will now match your translation.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.
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.
We will explore ways to install other languages, check translation statuses, and many more in the following sections.
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.
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.
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.
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!
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.
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.
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:
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.
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 codeMore 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.
locale.api.php
documentation at https://api.drupal.org/api/drupal/core%21modules%21locale%21locale.api.php/8