mirror of
https://gitlab.unicamp.br/infimecc_drupal11_modules/structural_pages.git
synced 2026-03-08 01:27:42 -03:00
Add content translations for site_sections taxonomy terms
Taxonomy term names are content, not interface strings, so .po files don't translate them. This adds programmatic content translations via the ContentLanguageSettings API and addTranslation/setName on each term. - Add _structural_pages_term_translations() with pt-br name map - Add _structural_pages_add_term_translations() to create/update translations - Add _structural_pages_ensure_content_translation() using ContentLanguageSettings API - Add config/optional for new installs with content_translation enabled - Remove term name entries from .po file (ineffective for content) - Add update hook 10004 to fix existing installations Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
langcode: en
|
||||
status: true
|
||||
dependencies:
|
||||
config:
|
||||
- taxonomy.vocabulary.site_sections
|
||||
module:
|
||||
- content_translation
|
||||
- taxonomy
|
||||
id: taxonomy_term.site_sections
|
||||
target_entity_type_id: taxonomy_term
|
||||
target_bundle: site_sections
|
||||
default_langcode: site_default
|
||||
language_alterable: true
|
||||
content_translation_enabled: true
|
||||
third_party_settings:
|
||||
content_translation:
|
||||
enabled: true
|
||||
@@ -73,6 +73,8 @@ function _structural_pages_create_default_terms(): void {
|
||||
'IT Services' => [],
|
||||
];
|
||||
|
||||
$created_terms = [];
|
||||
|
||||
$weight = 0;
|
||||
foreach ($terms_structure as $parent_name => $children) {
|
||||
// Create parent term.
|
||||
@@ -82,6 +84,7 @@ function _structural_pages_create_default_terms(): void {
|
||||
'weight' => $weight++,
|
||||
]);
|
||||
$parent_term->save();
|
||||
$created_terms[] = $parent_term;
|
||||
|
||||
// Create child terms.
|
||||
$child_weight = 0;
|
||||
@@ -93,8 +96,96 @@ function _structural_pages_create_default_terms(): void {
|
||||
'weight' => $child_weight++,
|
||||
]);
|
||||
$child_term->save();
|
||||
$created_terms[] = $child_term;
|
||||
}
|
||||
}
|
||||
|
||||
// Add content translations if available.
|
||||
_structural_pages_add_term_translations($created_terms);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds content translations to taxonomy terms.
|
||||
*
|
||||
* @param \Drupal\taxonomy\Entity\Term[] $terms
|
||||
* The terms to translate.
|
||||
*/
|
||||
function _structural_pages_add_term_translations(array $terms): void {
|
||||
if (!\Drupal::moduleHandler()->moduleExists('content_translation')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$translations = _structural_pages_term_translations();
|
||||
$language_manager = \Drupal::languageManager();
|
||||
|
||||
foreach ($translations as $langcode => $name_map) {
|
||||
if (!$language_manager->getLanguage($langcode)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($terms as $term) {
|
||||
$name = $term->getName();
|
||||
if (!isset($name_map[$name])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$translated_name = $name_map[$name];
|
||||
|
||||
if ($term->hasTranslation($langcode)) {
|
||||
// Update existing translation if name doesn't match.
|
||||
$translation = $term->getTranslation($langcode);
|
||||
if ($translation->getName() !== $translated_name) {
|
||||
$translation->setName($translated_name);
|
||||
$translation->save();
|
||||
}
|
||||
}
|
||||
else {
|
||||
$term->addTranslation($langcode, ['name' => $translated_name]);
|
||||
$term->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns term name translations keyed by langcode.
|
||||
*
|
||||
* @return array
|
||||
* Nested array: langcode => [english_name => translated_name].
|
||||
*/
|
||||
function _structural_pages_term_translations(): array {
|
||||
return [
|
||||
'pt-br' => [
|
||||
'News' => 'Notícias',
|
||||
'Events' => 'Eventos',
|
||||
'People' => 'Pessoas',
|
||||
'Institutional' => 'Institucional',
|
||||
'About' => 'Sobre',
|
||||
'Communication' => 'Comunicação',
|
||||
'Information and Services' => 'Informações e Serviços',
|
||||
'Team' => 'Equipe',
|
||||
'Management' => 'Gestão',
|
||||
'Inclusion and Belonging' => 'Inclusão e Pertencimento',
|
||||
'Undergraduate' => 'Graduação',
|
||||
'Statistics' => 'Estatística',
|
||||
'Mathematics' => 'Matemática',
|
||||
'Applied Mathematics' => 'Matemática Aplicada',
|
||||
'Mathematics Teaching' => 'Licenciatura em Matemática',
|
||||
'Graduate' => 'Pós-Graduação',
|
||||
'Statistics Program' => 'Programa de Estatística',
|
||||
'Mathematics Program' => 'Programa de Matemática',
|
||||
'Applied Mathematics Program' => 'Programa de Matemática Aplicada',
|
||||
'Research' => 'Pesquisa',
|
||||
'Extension' => 'Extensão',
|
||||
'Administration' => 'Administração',
|
||||
'Departments' => 'Departamentos',
|
||||
'Statistics Department' => 'Departamento de Estatística',
|
||||
'Mathematics Department' => 'Departamento de Matemática',
|
||||
'Applied Mathematics Department' => 'Departamento de Matemática Aplicada',
|
||||
'Library' => 'Biblioteca',
|
||||
'IT Services' => 'Informática',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -186,6 +277,49 @@ function structural_pages_update_10001(): void {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add content translations to site_sections taxonomy terms (no-op, see 10003).
|
||||
*/
|
||||
function structural_pages_update_10002(): void {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add content translations to site_sections taxonomy terms (no-op, see 10004).
|
||||
*/
|
||||
function structural_pages_update_10003(): void {
|
||||
}
|
||||
|
||||
/**
|
||||
* Fix content translation config and update site_sections term translations.
|
||||
*/
|
||||
function structural_pages_update_10004(): void {
|
||||
// Ensure content translation is properly configured for the vocabulary.
|
||||
_structural_pages_ensure_content_translation();
|
||||
|
||||
$terms = \Drupal::entityTypeManager()
|
||||
->getStorage('taxonomy_term')
|
||||
->loadByProperties(['vid' => 'site_sections']);
|
||||
|
||||
if ($terms) {
|
||||
_structural_pages_add_term_translations($terms);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures content translation settings exist for site_sections vocabulary.
|
||||
*/
|
||||
function _structural_pages_ensure_content_translation(): void {
|
||||
if (!\Drupal::moduleHandler()->moduleExists('content_translation')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$config = \Drupal\language\Entity\ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', 'site_sections');
|
||||
$config->setDefaultLangcode('site_default');
|
||||
$config->setLanguageAlterable(TRUE);
|
||||
$config->setThirdPartySetting('content_translation', 'enabled', TRUE);
|
||||
$config->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_requirements().
|
||||
*/
|
||||
|
||||
@@ -225,91 +225,6 @@ msgstr "Configuração do campo atualizada com sucesso."
|
||||
msgid "Configure allowed parent entity types for content pages."
|
||||
msgstr "Configure os tipos de entidade pai permitidos para páginas de conteúdo."
|
||||
|
||||
#: Default taxonomy terms (structural_pages.install)
|
||||
msgid "News"
|
||||
msgstr "Notícias"
|
||||
|
||||
msgid "Events"
|
||||
msgstr "Eventos"
|
||||
|
||||
msgid "People"
|
||||
msgstr "Pessoas"
|
||||
|
||||
msgid "Institutional"
|
||||
msgstr "Institucional"
|
||||
|
||||
msgid "About"
|
||||
msgstr "Sobre"
|
||||
|
||||
msgid "Communication"
|
||||
msgstr "Comunicação"
|
||||
|
||||
msgid "Information and Services"
|
||||
msgstr "Informações e Serviços"
|
||||
|
||||
msgid "Team"
|
||||
msgstr "Equipe"
|
||||
|
||||
msgid "Management"
|
||||
msgstr "Gestão"
|
||||
|
||||
msgid "Inclusion and Belonging"
|
||||
msgstr "Inclusão e Pertencimento"
|
||||
|
||||
msgid "Undergraduate"
|
||||
msgstr "Graduação"
|
||||
|
||||
msgid "Statistics"
|
||||
msgstr "Estatística"
|
||||
|
||||
msgid "Mathematics"
|
||||
msgstr "Matemática"
|
||||
|
||||
msgid "Applied Mathematics"
|
||||
msgstr "Matemática Aplicada"
|
||||
|
||||
msgid "Mathematics Teaching"
|
||||
msgstr "Licenciatura em Matemática"
|
||||
|
||||
msgid "Graduate"
|
||||
msgstr "Pós-Graduação"
|
||||
|
||||
msgid "Statistics Program"
|
||||
msgstr "Programa de Estatística"
|
||||
|
||||
msgid "Mathematics Program"
|
||||
msgstr "Programa de Matemática"
|
||||
|
||||
msgid "Applied Mathematics Program"
|
||||
msgstr "Programa de Matemática Aplicada"
|
||||
|
||||
msgid "Research"
|
||||
msgstr "Pesquisa"
|
||||
|
||||
msgid "Extension"
|
||||
msgstr "Extensão"
|
||||
|
||||
msgid "Administration"
|
||||
msgstr "Administração"
|
||||
|
||||
msgid "Departments"
|
||||
msgstr "Departamentos"
|
||||
|
||||
msgid "Statistics Department"
|
||||
msgstr "Departamento de Estatística"
|
||||
|
||||
msgid "Mathematics Department"
|
||||
msgstr "Departamento de Matemática"
|
||||
|
||||
msgid "Applied Mathematics Department"
|
||||
msgstr "Departamento de Matemática Aplicada"
|
||||
|
||||
msgid "Library"
|
||||
msgstr "Biblioteca"
|
||||
|
||||
msgid "IT Services"
|
||||
msgstr "Informática"
|
||||
|
||||
#: src/Plugin/Block/StructuralPagesMenuBlock.php
|
||||
msgid "Structural Pages Menu"
|
||||
msgstr "Menu das Páginas Estruturais"
|
||||
|
||||
Reference in New Issue
Block a user