-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoe_multilingual.module
156 lines (142 loc) · 5.49 KB
/
oe_multilingual.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* @file
* Multilingual module.
*/
declare(strict_types=1);
use Drupal\Component\Gettext\PoStreamReader;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\TranslatableInterface;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
/**
* Implements hook_language_switch_links_alter().
*
* Replaces the language links labels with the native language version.
*/
function oe_multilingual_language_switch_links_alter(array &$links, string $type, Url $url): void {
/** @var \Drupal\Core\Language\LanguageInterface[] $original_languages */
$original_languages = \Drupal::service('language_manager')->getNativeLanguages();
foreach ($links as $code => &$link) {
if (isset($original_languages[$code])) {
$link['title'] = $original_languages[$code]->getName();
}
}
}
/**
* Implements hook_language_fallback_candidates_alter().
*
* - Disable default language fallback logic: the default Drupal behavior
* will return all available languages and then choose the first one
* ordered alphabetically.
* - Return the entity's original language as the default fallback language.
*/
function oe_multilingual_language_fallback_candidates_alter(array &$candidates, array $context): void {
$operation = $context['operation'];
// The default fallback language for any entity is the language it was
// originally written on.
if ($operation === 'entity_upcast' || $operation === 'entity_view') {
$attempted_langcode = $context['langcode'];
if ($context['data'] instanceof TranslatableInterface) {
$candidates = [$attempted_langcode => $attempted_langcode];
$entity = $context['data'];
$default_language = $entity->getUntranslated()->language()->getId();
$candidates[$default_language] = $default_language;
}
}
}
/**
* Implements hook_preprocess_language_selection_page_content().
*
* Replaces the language links labels with the native language version.
*/
function oe_multilingual_preprocess_language_selection_page_content(&$variables): void {
/** @var \Drupal\Core\Language\LanguageInterface[] $original_languages */
$original_languages = \Drupal::service('language_manager')->getNativeLanguages();
foreach ($variables['language_links']['#items'] as $langcode => &$item) {
if (isset($original_languages[$langcode])) {
$item['#title'] = $original_languages[$langcode]->getName();
}
}
}
/**
* Implements hook_ENTITY_TYPE_insert() for the configurable language entities.
*
* Whenever a language entity is created, we need to ensure we create its
* translation so that we have the correct native name for it.
*/
function oe_multilingual_configurable_language_insert(EntityInterface $entity) {
if (\Drupal::isConfigSyncing() || isset($GLOBALS['install_state'])) {
// We only want to trigger this if the language is not created as part of
// a site install or config sync. In those cases, we import the string
// translations which will create the language overrides.
return;
}
$reader = new PoStreamReader();
$reader->setLangcode($entity->getId());
$file = \Drupal::service('extension.list.module')->getPath('oe_multilingual') . "/translations/oe_multilingual-{$entity->getId()}.po";
if (!file_exists($file)) {
return;
}
$reader->setURI($file);
try {
$reader->open();
}
catch (\Exception $exception) {
// If something goes wrong with the the reader, we return and do nothing.
return;
}
while ($item = $reader->readItem()) {
if ($item->getSource() === $entity->getName()) {
/** @var \Drupal\language\ConfigurableLanguageInterface $config */
$config = \Drupal::languageManager()->getLanguageConfigOverride($entity->getId(), 'language.entity.' . $entity->id());
$config->set('label', $item->getTranslation());
$config->save();
break;
}
}
}
/**
* Implements hook_locale_translation_projects_alter().
*/
function oe_multilingual_locale_translation_projects_alter(&$projects) {
$projects['oe_multilingual']['info']['interface translation server pattern'] = \Drupal::service('extension.list.module')->getPath('oe_multilingual') . '/translations/%project-%language.po';
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Add a category selection to language entity form.
*/
function oe_multilingual_form_language_admin_edit_form_alter(&$form, FormStateInterface $form_state): void {
/** @var \Drupal\language\Entity\ConfigurableLanguage $language */
$language = $form_state->getFormObject()->getEntity();
$form['category'] = [
'#type' => 'select',
'#title' => t('Category'),
'#required' => TRUE,
'#options' => [
'eu' => t('EU'),
'non_eu' => t('Non-EU'),
],
'#default_value' => $language->getThirdPartySetting('oe_multilingual', 'category'),
];
$form['#entity_builders'][] = 'oe_multilingual_form_language_edit_form_builder';
}
/**
* Entity form builder for the language edit form.
*
* Saves the language category into the language settings.
*
* @param string $entity_type
* The name of the entity type.
* @param \Drupal\language\Entity\ConfigurableLanguage $language
* The language entity.
* @param array $form
* The form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
function oe_multilingual_form_language_edit_form_builder(string $entity_type, ConfigurableLanguage $language, array &$form, FormStateInterface $form_state): void {
$language->setThirdPartySetting('oe_multilingual', 'category', $form_state->getValue('category'));
}