forked from hechoendrupal/drupal-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranslatorHelper.php
More file actions
121 lines (100 loc) · 3.49 KB
/
Copy pathTranslatorHelper.php
File metadata and controls
121 lines (100 loc) · 3.49 KB
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
<?php
/**
* @file
* Contains \Drupal\AppConsole\Command\Helper\TranslatorHelper.
*/
namespace Drupal\AppConsole\Command\Helper;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\Loader\YamlFileLoader;
use Symfony\Component\Translation\Writer\TranslationWriter;
use Symfony\Component\Translation\MessageCatalogue;
use Drupal\AppConsole\YamlFileDumper;
class TranslatorHelper extends Helper
{
private $language;
/**
* @var $translator Translator
*/
private $translator;
private function addResource($resource)
{
$this->translator->addResource(
'yaml',
$resource,
$this->language
);
}
public function loadResource($language, $directoryRoot)
{
$resource_fallback = $directoryRoot . 'config/translations/console.en.yml';
$resource_language = $directoryRoot . 'config/translations/console.' . $language . '.yml';
if (!file_exists($resource_language)) {
$language = 'en';
$resource_language = $resource_fallback;
}
$this->language = $language;
$this->translator = new Translator($this->language);
$this->translator->addLoader('yaml', new YamlFileLoader());
//Fallback to English (en)
$this->addResource($resource_fallback);
//Load user language
if ($resource_language != $resource_fallback) {
$this->addResource($resource_language);
}
}
public function addResourceTranslationsByModule($module)
{
$resource = DRUPAL_ROOT . '/' . drupal_get_path('module', $module) .
'/config/translations/console.' . $this->language . '.yml';
if (file_exists($resource)) {
$this->addResource($resource);
}
}
public function writeTranslationsByModule($module, $messages)
{
$currentMessages = $this->getMessagesByModule($module);
$language = 'en';
$resource = DRUPAL_ROOT . '/' . drupal_get_path('module', $module) .
'/config/translations/';
$messageCatalogue = new MessageCatalogue($language);
if ($currentMessages && $currentMessages['messages']) {
$messageCatalogue->add($currentMessages['messages'], 'console');
}
$messageCatalogue->add($messages, 'console');
$translatorWriter = new TranslationWriter();
$translatorWriter->addDumper('yaml', new YamlFileDumper());
$translatorWriter->writeTranslations(
$messageCatalogue,
'yaml',
['path' => $resource, 'nest-level' => 10, 'indent' => 2]
);
}
protected function getMessagesByModule($module)
{
$resource = DRUPAL_ROOT . '/' . drupal_get_path('module', $module) .
'/config/translations/console.' . $this->language . '.yml';
if (file_exists($resource)) {
$moduleTranslator = new Translator($this->language);
$moduleTranslator->addLoader('yaml', new YamlFileLoader());
$moduleTranslator->addResource(
'yaml',
$resource,
$this->language
);
return $moduleTranslator->getMessages($this->language);
}
return [];
}
public function trans($key)
{
return $this->translator->trans($key);
}
/**
* @see \Symfony\Component\Console\Helper\HelperInterface::getName()
*/
public function getName()
{
return 'translator';
}
}