-
Notifications
You must be signed in to change notification settings - Fork 0
/
GettextMessageSource.php
141 lines (126 loc) · 4.57 KB
/
GettextMessageSource.php
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
<?php
namespace yiithings\i18n;
use Gettext\Translations;
use Gettext\Translator;
use Yii;
use yii\i18n\MissingTranslationEvent;
class GettextMessageSource extends \yii\i18n\GettextMessageSource
{
/**
* @var string|array
*/
public $basePath;
/**
* @var bool Replace path dash character (-) to underline.
*/
public $replaceDashCharacter = false;
/**
* @var bool Enable language directory. If set to true use path $basePath/$language/
* (e.g @app/messages/en-US/en-US.mo), otherwise use $basePath (e.g. @app/messages/en-US.mo).
*/
public $enableLanguageDirectory = false;
/**
* @var bool Enable category file name. If set to true use path $basePath/$language/$category.mo
* (e.g @app/messages/en-US/app.mo), otherwise use $basePath (e.g. @app/messages/en-US.mo).
* Another exception: $enableDirectory is false and $enableCategoryFile is true, use path
* $basePath/$language_$category.mo
*/
public $enableCategoryFile = false;
/**
* @var bool Disabled category.
*/
public $disabledCategory = false;
/**
* @var \Gettext\Translator
*/
protected $translator;
protected function translateMessage($category, $message, $language)
{
if ($this->translator === null) {
$this->translator = $this->loadMessages($category, $language);;
}
if ($this->translator === false) {
return false;
}
if ($this->disabledCategory || $category === null || $category === '') {
$translated = $this->translator->gettext($message);
} else {
$translated = $this->translator->dpgettext(null, $category, $message);
}
if ($translated == $message &&
$this->sourceLanguage != $language &&
$this->hasEventHandlers(self::EVENT_MISSING_TRANSLATION)) {
$event = new MissingTranslationEvent([
'category' => $category,
'message' => $message,
'language' => $language,
]);
$this->trigger(self::EVENT_MISSING_TRANSLATION, $event);
if ($event->translatedMessage !== null) {
return $event->translatedMessage;
}
}
return $translated;
}
protected function loadMessages($category, $language)
{
$messageFiles = $this->getMessageFilePaths($category, $language);
$messages = $this->loadMessagesFromFile($messageFiles, $category);
if ($messages === false) {
$fallbackLanguage = substr($language, 0, 2);
if ($fallbackLanguage !== $language) {
$messageFiles = $this->getMessageFilePaths($category, $language);
$messages = $this->loadMessagesFromFile($messageFiles, $category);
}
}
return $messages;
}
/**
* @param array $messageFiles
* @param string $category
* @return bool|Translator
*/
protected function loadMessagesFromFile($messageFiles, $category)
{
$translator = false;
foreach ($messageFiles as $messageFile) {
if (is_file($messageFile)) {
if ($this->useMoFile) {
$translations = Translations::fromMoFile($messageFile);
} else {
$translations = Translations::fromPoFile($messageFile);
}
if ($translator === false) {
$translator = new Translator();
}
$translator->loadTranslations($translations);
}
}
return $translator;
}
protected function getMessageFilePaths($category, $language)
{
$messageFiles = [];
foreach ((array)$this->basePath as $basePath) {
$messageFile = $language;
if ($this->enableCategoryFile && ! $this->enableLanguageDirectory) {
$messageFile .= '_' . $category;
} elseif ($this->enableCategoryFile) {
$messageFile .= '/' . $category;
} elseif ($this->enableLanguageDirectory) {
$messageFile .= '/' . $language;
}
if ($this->replaceDashCharacter) {
$messageFile = str_replace('-', '_', $messageFile);
}
$messageFile = Yii::getAlias($basePath) . '/' . $messageFile;
if ($this->useMoFile) {
$messageFile .= self::MO_FILE_EXT;
} else {
$messageFile .= self::PO_FILE_EXT;
}
$messageFiles[] = $messageFile;
}
return $messageFiles;
}
}