1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Contains df_tools_translation.module. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
use Drupal\migrate_source_csv\CSVFileObject; |
9
|
|
|
use Drupal\Core\Entity\FieldableEntityInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Performs minimum steps necessary to load a CSV from a file. |
13
|
|
|
* |
14
|
|
|
* @param string $filename The filename of a CSV |
15
|
|
|
* |
16
|
|
|
* @return \Drupal\migrate_source_csv\CSVFileObject The prepared CSV object |
17
|
|
|
*/ |
18
|
|
|
function df_tools_translation_initialize_csv($filename) { |
19
|
|
|
// Grab the processed CSV file using existing CSV methods. |
20
|
|
|
$file = new CSVFileObject($filename); |
21
|
|
|
$file->setHeaderRowCount(1); |
22
|
|
|
|
23
|
|
|
// Map the column names (first row) to values. |
24
|
|
|
$row = $file->current(); |
25
|
|
|
$column_names = []; |
26
|
|
|
foreach ($row as $header) { |
27
|
|
|
$header = trim($header); |
28
|
|
|
$column_names[] = [$header => $header]; |
29
|
|
|
} |
30
|
|
|
$file->setColumnNames($column_names); |
31
|
|
|
|
32
|
|
|
return $file; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Enables translation for the given entity bundles and all their fields. |
37
|
|
|
* |
38
|
|
|
* @param array $entity_info An array mapping entity types to arrays of bundles. |
39
|
|
|
*/ |
40
|
|
|
function df_tools_translation_enable_translation($entity_info) { |
41
|
|
|
// Enable translation for all of our entities/bundles. |
42
|
|
|
$type_manager = \Drupal::entityTypeManager(); |
43
|
|
|
/** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */ |
44
|
|
|
$field_manager = \Drupal::service('entity_field.manager'); |
45
|
|
|
/** @var \Drupal\content_translation\ContentTranslationManagerInterface $translation_manager */ |
46
|
|
|
$translation_manager = \Drupal::service('content_translation.manager'); |
47
|
|
|
$supported_types = $translation_manager->getSupportedEntityTypes(); |
48
|
|
|
foreach ($entity_info as $entity_type_id => $bundles) { |
49
|
|
|
foreach ($bundles as $bundle) { |
50
|
|
|
// Store whether a bundle has translation enabled or not. |
51
|
|
|
if (isset($supported_types[$entity_type_id])) { |
52
|
|
|
$translation_manager->setEnabled($entity_type_id, $bundle, TRUE); |
53
|
|
|
} |
54
|
|
|
// Make every field translatable as well. |
55
|
|
|
$entity_type = $type_manager->getDefinition($entity_type_id); |
56
|
|
|
if ($entity_type && $entity_type->isSubclassOf(FieldableEntityInterface::class)) { |
|
|
|
|
57
|
|
|
$fields = $field_manager->getFieldDefinitions($entity_type_id, $bundle); |
58
|
|
|
foreach ($fields as $field) { |
59
|
|
|
$field_config = $field->getConfig($bundle); |
60
|
|
|
if ($field_config->isTranslatable() && strpos($field->getName(), 'content_translation_') !== 0) { |
61
|
|
|
$field_config->setTranslatable(TRUE)->save(); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
// Ensure entity and menu router information are correctly rebuilt. |
68
|
|
|
$type_manager->clearCachedDefinitions(); |
69
|
|
|
\Drupal::service('router.builder')->setRebuildNeeded(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Updates the current site's translations via a batch process. |
74
|
|
|
*/ |
75
|
|
|
function df_tools_translation_update_config_translation() { |
76
|
|
|
// The Locale module splits its translation functions into separate include |
77
|
|
|
// files, based on utility. |
78
|
|
|
// To ensure that each function we require is available, load its respective |
79
|
|
|
// include file. |
80
|
|
|
\Drupal::moduleHandler()->loadInclude('locale', 'bulk.inc'); |
81
|
|
|
\Drupal::moduleHandler()->loadInclude('locale', 'compare.inc'); |
82
|
|
|
\Drupal::moduleHandler()->loadInclude('locale', 'fetch.inc'); |
83
|
|
|
\Drupal::moduleHandler()->loadInclude('locale', 'translation.inc'); |
84
|
|
|
|
85
|
|
|
// Get a list of all currently installed languages as langcodes. |
86
|
|
|
$languageManager = \Drupal::languageManager(); |
87
|
|
|
$langcodes = array_keys($languageManager->getLanguages()); |
88
|
|
|
|
89
|
|
|
// Set a batch to download and import translations. |
90
|
|
|
locale_translation_flush_projects(); |
91
|
|
|
locale_translation_check_projects(); |
92
|
|
|
$options = _locale_translation_default_update_options(); |
93
|
|
|
$batch = locale_translation_batch_fetch_build([], $langcodes, $options); |
94
|
|
|
batch_set($batch); |
95
|
|
|
// Set a batch to update configuration as well. |
96
|
|
|
if ($batch = locale_config_batch_update_components($options, $langcodes)) { |
97
|
|
|
$batch['file'] = drupal_get_path('module', 'df_tools_translation') . '/df_tools_translation.batch.inc'; |
98
|
|
|
batch_set($batch); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Imports all relevant translations from a modules /translations directory. |
104
|
|
|
* |
105
|
|
|
* @param string $type The project type. |
106
|
|
|
* @param string $name The name of the project. |
107
|
|
|
* |
108
|
|
|
* @return bool FALSE if the project does not exist. |
109
|
|
|
*/ |
110
|
|
|
function df_tools_translation_import_translations($type, $name) { |
111
|
|
|
// Attempt to pull module path. |
112
|
|
|
$path = drupal_get_path($type, $name); |
113
|
|
|
if (!$path) { |
114
|
|
|
return FALSE; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// Get a list of all currently installed languages as langcodes. |
118
|
|
|
$languageManager = \Drupal::languageManager(); |
119
|
|
|
$langcodes = array_keys($languageManager->getLanguages()); |
120
|
|
|
|
121
|
|
|
// Import each file. |
122
|
|
|
foreach ($langcodes as $langcode) { |
123
|
|
|
$filepath = DRUPAL_ROOT . '/' . $path . '/translations/' . $langcode . '.po'; |
124
|
|
|
if (file_exists($filepath)) { |
125
|
|
|
\Drupal::moduleHandler()->loadInclude('locale', 'bulk.inc'); |
126
|
|
|
\Drupal::moduleHandler()->loadInclude('locale', 'translation.inc'); |
127
|
|
|
$options = array_merge(_locale_translation_default_update_options(), [ |
128
|
|
|
'langcode' => $langcode, |
129
|
|
|
'overwrite_options' => [ |
130
|
|
|
'customized' => TRUE, |
131
|
|
|
'not_customized' => TRUE |
132
|
|
|
], |
133
|
|
|
'customized' => TRUE |
134
|
|
|
]); |
135
|
|
|
|
136
|
|
|
$original_file = (object) [ |
137
|
|
|
'filename' => $langcode . '.po', |
138
|
|
|
'uri' => $filepath |
139
|
|
|
]; |
140
|
|
|
$file = locale_translate_file_attach_properties($original_file, $options); |
141
|
|
|
$batch = locale_translate_batch_build([$file->uri => $file], $options); |
142
|
|
|
batch_set($batch); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Implements hook_preprocess_page(). |
149
|
|
|
*/ |
150
|
|
|
function df_tools_translation_preprocess_page(&$variables) { |
151
|
|
|
// Add a new page variable with the current link. |
152
|
|
|
if (!isset($variables['language_current_link']) && isset($variables['language'])) { |
153
|
|
|
$variables['language_current_link'] = [ |
154
|
|
|
'#markup' => t($variables['language']->getName()) |
155
|
|
|
]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// Add the rest of the language links as well, with links to switch to the |
159
|
|
|
// correct language. |
160
|
|
|
if (!isset($variables['language_links'])) { |
161
|
|
|
// Get a list of the current languages. |
162
|
|
|
$languageManager = \Drupal::languageManager(); |
163
|
|
|
$languages = $languageManager->getLanguages(); |
164
|
|
|
|
165
|
|
|
// Remove the current language. |
166
|
|
|
unset($languages[$variables['language']->getId()]); |
167
|
|
|
|
168
|
|
|
// Add each link to the language list. |
169
|
|
|
$links = []; |
170
|
|
|
foreach ($languages as $language) { |
171
|
|
|
$langcode = $language->getId(); |
172
|
|
|
|
173
|
|
|
// Get the path to the current node, translated. |
174
|
|
|
$current_path = \Drupal::service('path.current')->getPath(); |
175
|
|
|
$alias = \Drupal::service('path.alias_manager')->getAliasByPath($current_path, $langcode); |
176
|
|
|
// We don't need to alias English links. |
177
|
|
|
if ($langcode == 'en') { |
178
|
|
|
$langcode = ''; |
179
|
|
|
} |
180
|
|
|
$url = \Drupal\Core\Url::fromUri('base:/' . $langcode . $alias); |
181
|
|
|
|
182
|
|
|
$current_name = [ |
183
|
|
|
'#markup' => t($language->getName()) |
184
|
|
|
]; |
185
|
|
|
|
186
|
|
|
$links[] = \Drupal::l($current_name, $url); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$variables['language_links'] = $links; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Implements hook_site_install_finished(). |
195
|
|
|
*/ |
196
|
|
|
function df_tools_translation_site_install_finished($install_state) { |
197
|
|
|
// After a scenario profile install, import UI translations from the .po files. |
198
|
|
|
if (isset($install_state['profile']) && $info = scenarios_info($install_state['profile'])) { |
|
|
|
|
199
|
|
|
df_tools_translation_import_translations('profile', $install_state['profile']); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.