|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @file |
|
5
|
|
|
* Contains df_tools_translation.module. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
use Drupal\Core\Url; |
|
9
|
|
|
use Drupal\Core\Link; |
|
10
|
|
|
use Drupal\Core\Entity\FieldableEntityInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Enables translation for the given entity bundles and all their fields. |
|
14
|
|
|
* |
|
15
|
|
|
* @param array $entity_info An array mapping entity types to arrays of bundles. |
|
16
|
|
|
*/ |
|
17
|
|
|
function df_tools_translation_enable_translation($entity_info) { |
|
18
|
|
|
// Enable translation for all of our entities/bundles. |
|
19
|
|
|
$type_manager = \Drupal::entityTypeManager(); |
|
20
|
|
|
/** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */ |
|
21
|
|
|
$field_manager = \Drupal::service('entity_field.manager'); |
|
22
|
|
|
/** @var \Drupal\content_translation\ContentTranslationManagerInterface $translation_manager */ |
|
23
|
|
|
$translation_manager = \Drupal::service('content_translation.manager'); |
|
24
|
|
|
$supported_types = $translation_manager->getSupportedEntityTypes(); |
|
25
|
|
|
foreach ($entity_info as $entity_type_id => $bundles) { |
|
26
|
|
|
foreach ($bundles as $bundle) { |
|
27
|
|
|
// Store whether a bundle has translation enabled or not. |
|
28
|
|
|
if (isset($supported_types[$entity_type_id])) { |
|
29
|
|
|
$translation_manager->setEnabled($entity_type_id, $bundle, TRUE); |
|
30
|
|
|
} |
|
31
|
|
|
// Make every field translatable as well. |
|
32
|
|
|
$entity_type = $type_manager->getDefinition($entity_type_id); |
|
33
|
|
|
if ($entity_type && $entity_type->entityClassImplements(FieldableEntityInterface::class)) { |
|
34
|
|
|
$fields = $field_manager->getFieldDefinitions($entity_type_id, $bundle); |
|
35
|
|
|
foreach ($fields as $field) { |
|
36
|
|
|
$field_config = $field->getConfig($bundle); |
|
37
|
|
|
if ($field_config->isTranslatable() && strpos($field->getName(), 'content_translation_') !== 0) { |
|
38
|
|
|
$field_config->setTranslatable(TRUE)->save(); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
// Ensure entity and menu router information are correctly rebuilt. |
|
45
|
|
|
$type_manager->clearCachedDefinitions(); |
|
46
|
|
|
\Drupal::service('router.builder')->setRebuildNeeded(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Imports all relevant translations from a modules /translations directory. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $type The project type. |
|
53
|
|
|
* @param string $name The name of the project. |
|
54
|
|
|
* |
|
55
|
|
|
* @return bool FALSE if the project does not exist. |
|
56
|
|
|
*/ |
|
57
|
|
|
function df_tools_translation_import_translations($type, $name) { |
|
|
|
|
|
|
58
|
|
|
// Attempt to pull module path. |
|
59
|
|
|
$path = \Drupal::service('extension.list.module')->getPath($name); |
|
60
|
|
|
if (!$path) { |
|
61
|
|
|
return FALSE; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// Get a list of all currently installed languages as langcodes. |
|
65
|
|
|
$languageManager = \Drupal::languageManager(); |
|
66
|
|
|
$langcodes = array_keys($languageManager->getLanguages()); |
|
67
|
|
|
|
|
68
|
|
|
// Import each file. |
|
69
|
|
|
foreach ($langcodes as $langcode) { |
|
70
|
|
|
$filepath = DRUPAL_ROOT . '/' . $path . '/translations/' . $langcode . '.po'; |
|
71
|
|
|
if (file_exists($filepath)) { |
|
72
|
|
|
\Drupal::moduleHandler()->loadInclude('locale', 'bulk.inc'); |
|
73
|
|
|
\Drupal::moduleHandler()->loadInclude('locale', 'translation.inc'); |
|
74
|
|
|
$options = array_merge(_locale_translation_default_update_options(), [ |
|
75
|
|
|
'langcode' => $langcode, |
|
76
|
|
|
'overwrite_options' => [ |
|
77
|
|
|
'customized' => TRUE, |
|
78
|
|
|
'not_customized' => TRUE |
|
79
|
|
|
], |
|
80
|
|
|
'customized' => TRUE |
|
81
|
|
|
]); |
|
82
|
|
|
|
|
83
|
|
|
$original_file = (object) [ |
|
84
|
|
|
'filename' => $langcode . '.po', |
|
85
|
|
|
'uri' => $filepath |
|
86
|
|
|
]; |
|
87
|
|
|
$file = locale_translate_file_attach_properties($original_file, $options); |
|
88
|
|
|
$batch = locale_translate_batch_build([$file->uri => $file], $options); |
|
89
|
|
|
batch_set($batch); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Implements hook_preprocess_page(). |
|
96
|
|
|
*/ |
|
97
|
|
|
function df_tools_translation_preprocess_page(&$variables) { |
|
98
|
|
|
// Add a new page variable with the current link. |
|
99
|
|
|
if (!isset($variables['language_current_link']) && isset($variables['language'])) { |
|
100
|
|
|
$path = \Drupal::service('path.current')->getPath(); |
|
101
|
|
|
$language = $variables['language']->getId(); |
|
102
|
|
|
$alias = \Drupal::service('path_alias.manager')->getAliasByPath($path, $language); |
|
103
|
|
|
$url = Url::fromUri('internal:/' . $alias); |
|
104
|
|
|
$name = t($variables['language']->getName()); |
|
105
|
|
|
$link = Link::fromTextAndUrl($name, $url)->toString(); |
|
106
|
|
|
$variables['language_current_link'] = $link; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// Add the rest of the language links as well, with links to switch to the |
|
110
|
|
|
// correct language. |
|
111
|
|
|
if (!isset($variables['language_links'])) { |
|
112
|
|
|
// Get a list of the current languages. |
|
113
|
|
|
$languageManager = \Drupal::languageManager(); |
|
114
|
|
|
$languages = $languageManager->getLanguages(); |
|
115
|
|
|
|
|
116
|
|
|
// Remove the current language. |
|
117
|
|
|
unset($languages[$variables['language']->getId()]); |
|
118
|
|
|
|
|
119
|
|
|
// Add each link to the language list. |
|
120
|
|
|
$links = []; |
|
121
|
|
|
foreach ($languages as $language) { |
|
122
|
|
|
$langcode = $language->getId(); |
|
123
|
|
|
|
|
124
|
|
|
// Get the path to the current node, translated. |
|
125
|
|
|
$current_path = \Drupal::service('path.current')->getPath(); |
|
126
|
|
|
$alias = \Drupal::service('path_alias.manager')->getAliasByPath($current_path, $langcode); |
|
127
|
|
|
// We don't need to alias English links. |
|
128
|
|
|
if ($langcode == 'en') { |
|
129
|
|
|
$langcode = ''; |
|
130
|
|
|
} |
|
131
|
|
|
$url = Url::fromUri('base:/' . $langcode . $alias); |
|
132
|
|
|
$current_name = t($language->getName()); |
|
133
|
|
|
$links[] = Link::fromTextAndUrl($current_name, $url)->toString(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$variables['language_links'] = $links; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.