-
Notifications
You must be signed in to change notification settings - Fork 11
Entity Plus internationalization (i18n) integration
Entity Plus includes a submodule called entity_plus_i18n
that provides integration with Backdrop's contrib module Internationalization (i18n).
This submodule creates the ability for users to manually translate text entered in custom entity properties. For example, if your custom entity has a property called "label", you can enter any text there, and the Internationalization functionality will "pick it up" and allow you to translate it through the translation interface. You must enable Internationalization (i18n) and its submodule i18n_string
for this functionality to work. Then, when you visit your custom entity page in another language (www.example.com/es/my-entity/1
), you'll see the translated label (in this case, in Spanish).
For this functionality to work, your custom entity must contain the following key/value pair in the hook_entity_info()
definition
$entity['my_entity_type']['i18n controller class'] = 'EntityDefaultI18nStringController';
For example:
- Download and enable Basic Entity Plus Example, which provides an example custom entity that uses a lot of Entity Plus and Entity UI
- Enable all the following modules: Localize, Languages, Internationalization and String Translation
- Enable the new Entity Plus Internationalization Integration submodule (
entity_plus_i18n
) - Enable a language at
/admin/config/regional/language
, for example Spanish - Enable URL detection at
/admin/config/regional/language/detection
- Visit the Default Entity configuration page at
admin/structure/basic_entity_plus-types/manage/basic_entity_plus_default
. This is a default entity type provided by the Example module - Once there, you'll see a Translate tab. Clicking it will allow you to translate the two properties of the entity type (the label, in this case "Default entity", and the description property). Those values are also made available by the submodule in the translation interface
/admin/config/regional/translate
- Create a new entity of type "Default entity" at
/basic_entity_plus/add/basic_entity_plus_default
. If you haven't added a field to the entity type, you'll only see one field, "Title". Enter a text in English and save - Go to the translation interface > strings at
admin/config/regional/translate/i18n_string
and refresh the strings for Basic Entity Plus. This will make the title you entered above available for translation - Go back to the translation interface
/admin/config/regional/translate
and search for the title you entered - You will now see that text. Create a translation by clicking Edit
- Now go to the entity page in the other language (for example
/es/basic_entity_plus/1
or the correct ID). The title of the page should now show in Spanish
These additional settings are taken from Drupal 7 Entity API manual.
/**
* Implements hook_entity_property_info_alter().
*/
function basic_entity_plus_entity_property_info_alter(&$info) {
// Mark some properties as translatable, but also denote that translation
// works with i18n_string.
foreach (array('label') as $name) {
$info['basic_entity_plus_type']['properties'][$name]['translatable'] = TRUE;
$info['basic_entity_plus_type']['properties'][$name]['i18n string'] = TRUE;
}
}
/**
* Implements hook_basic_entity_plus_type_insert().
*/
function basic_entity_plus_basic_entity_plus_type_insert($basic_entity_plus_type) {
i18n_string_object_update('basic_entity_plus_type', $basic_entity_plus_type);
}
/**
* Implements hook_basic_entity_plus_type_update().
*/
function basic_entity_plus_basic_entity_plus_type_update($basic_entity_plus_type) {
// Account for name changes.
if ($basic_entity_plus_type->original->type != $basic_entity_plus_type->type) {
i18n_string_update_context("basic_entity_plus:basic_entity_plus_type:{$basic_entity_plus_type->original->type}:*", "basic_entity_plus:basic_entity_plus_type:{$basic_entity_plus_type->type}:*");
}
i18n_string_object_update('basic_entity_plus_type', $basic_entity_plus_type);
}
/**
* Implements hook_basic_entity_plus_type_delete().
*/
function basic_entity_plus_basic_entity_plus_type_delete($basic_entity_plus_type) {
i18n_string_object_remove('basic_entity_plus_type', $basic_entity_plus_type);
}