-
Notifications
You must be signed in to change notification settings - Fork 0
/
locale_translation_context.module
120 lines (105 loc) · 3.53 KB
/
locale_translation_context.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
<?php
/**
* @file
* Contains hooks for the Locale Translation Context module.
*/
use Drupal\Component\Gettext\PoStreamWriter;
use Drupal\Core\Database\Database;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\locale_translation_context\PoDatabaseReader;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
/**
* Implements hook_form_FORM_ID_alter().
*/
function locale_translation_context_form_locale_translate_export_form_alter(&$form, FormStateInterface $form_state) {
$form['context'] = [
'#title' => t("Context"),
'#type' => 'select',
'#options' => locale_translation_context_get_context_options(),
'#empty_option' => t('All contexts'),
];
// Change the form's submit function.
$original_submit_function_key = array_search('::submitForm', $form['#submit']);
$form['#submit'][$original_submit_function_key] = 'locale_translation_context_export_form_submit';
}
/**
* Get the available context list.
*
* @return array
* Array of the available contexts.
*/
function locale_translation_context_get_context_options() {
$context_options = [];
$connection = Database::getConnection();
$data = $connection->select('locales_source', 'ls');
$data->fields('ls', ['context']);
$data->isNotNull('context');
$data->groupBy('context');
$data->orderBy('context');
$results = $data->execute();
foreach ($results as $result) {
if (!empty($result->context)) {
$context_options[$result->context] = $result->context;
}
}
return $context_options;
}
/**
* Alters the Drupal\locale\Form\ExportForm submitForm method.
*
* @param array $form
* Contains the form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state object.
*/
function locale_translation_context_export_form_submit(array &$form, FormStateInterface $form_state) {
// If template is required, language code is not given.
if ($form_state->getValue('langcode') != LanguageInterface::LANGCODE_SYSTEM) {
$language = \Drupal::languageManager()->getLanguage($form_state->getValue('langcode'));
}
else {
$language = NULL;
}
$content_options = $form_state->getValue('content_options', []);
$context = $form_state->getValue('context');
$reader = new PoDatabaseReader();
$language_name = '';
if ($language != NULL) {
$reader->setLangcode($language->getId());
$reader->setOptions($content_options);
$languages = \Drupal::languageManager()->getLanguages();
$language_name = isset($languages[$language->getId()]) ? $languages[$language->getId()]->getName() : '';
$filename = $language->getId() . '.po';
}
else {
// Template required.
$filename = 'drupal.pot';
}
// Set context option.
if (!empty($context)) {
$options = $reader->getOptions();
$options['context'] = $context;
$reader->setOptions($options);
}
$item = $reader->readItem();
if (!empty($item)) {
$uri = \Drupal::service('file_system')->tempnam('temporary://', 'po_');
$header = $reader->getHeader();
$header->setProjectName(\Drupal::config('system.site')->get('name'));
$header->setLanguageName($language_name);
$writer = new PoStreamWriter();
$writer->setURI($uri);
$writer->setHeader($header);
$writer->open();
$writer->writeItem($item);
$writer->writeItems($reader);
$writer->close();
$response = new BinaryFileResponse($uri);
$response->setContentDisposition('attachment', $filename);
$form_state->setResponse($response);
}
else {
\Drupal::messenger()->addStatus(t('Nothing to export.'));
}
}