There are two ways to import translations to Drupal:
- Configuration translations, which are imported to Drupal via configuration translation files
- User interface translations, which are imported to Drupal via .po files
The user interface translations could be categorized in four different levels
- Contributed translations from localize.drupal.org
- Helfi specific translations which are imported from Helfi modules and HDBT themes
- Translations which override contrib module translations.
- Translations missing from localize.drupal.org (but we need them immediately)
Learn more about contributing to Drupal translations.
Configuration can be translated either in Drupal admin UI or manually in to the configuration translation files.
Export the configuration translations from DB to files with drush cex
and copy the exported files
from/conf/cmi/language/{langcode}/
to /module/config/optional/language/{langcode}/
.
Create and update hook for your module to import the translations.
See Development for more information.
We might want to customize some strings from contrib modules without overriding the string globally. Contrib modules can specify translation context for translatable strings in configuration schema. However, not all contrib modules use translation contexts. To customize the context, use hook_config_schema_info_alter()
:
/**
* Implements hook_config_schema_info_alter().
*/
function my_module_config_schema_info_alter(array &$definitions) : void {
if (isset($definitions['social_media.item.email'])) {
$definitions['social_media.item.email']['mapping']['text']['translation context'] = 'Social media: email';
}
}
Make sure the translations are imported during locale import (drush locale:update
) by checking
the module/theme has the following information in module_name.info.yml
. For an example, check helfi_content.info.yml.
'interface translation project': module_name
'interface translation server pattern': modules/custom/module_name/translations/%language.po
Place your UI translations module_name/translations/{langcode}.po
and follow the PO file syntax.
$this->t('Example', [], ['context' => 'My custom context'])
{{ 'Example'|t({}, {'context': 'My custom context'}) }}
const variable = Drupal.t('Example', {}, {context: 'My custom context'});
And the way to add the actual translation in to f.e. fi.po
is done like so:
msgctxt "My custom context"
msgid "Example"
msgstr "Esimerkki"
To see these translation changes in an instance, run in container shell:
drush locale:check && drush locale:update
And then flush all caches from top left drupal admin menu under "Druplicon".
The translation overrides are imported to Drupal instance with PO importer and translation overrides lives in helfi_platform_config/translations/override/{langcode}.po
.
Add the original module name of overridden translation as a comment, so that it's easier to locate the original UI text.
The contrib module translations are not complete, thus we need to have a way to add translations for the contrib modules in Hel.fi context. It is highly recommended to contribute back to the Drupal community and add the translations to localize.drupal.org.
We do usually have the urgency to add translations right away and cannot wait for the
processes. In these situations we can add translations in to helfi_platform_config/translations/new/{langcode}.po
.
Add the module name as a comment, so that it's easier to contribute back to drupal.org.