TranslateWpmlString.php
5.43 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
namespace WPML\ST;
use WPML\ST\Gettext\Settings as GettextSettings;
use WPML\ST\MO\Hooks\LanguageSwitch;
use WPML\ST\MO\File\Manager;
use WPML\ST\StringsFilter\Provider;
use WPML_Locale;
class TranslateWpmlString {
/** @var array $loadedDomains */
private static $loadedDomains = [];
/** @var Provider $filterProvider */
private $filterProvider;
/** @var LanguageSwitch $languageSwitch */
private $languageSwitch;
/** @var WPML_Locale $locale */
private $locale;
/** @var GettextSettings $gettextSettings */
private $gettextSettings;
/** @var Manager $fileManager */
private $fileManager;
/** @var bool $isAutoRegisterDisabled */
private $isAutoRegisterDisabled;
/** @var bool $lock */
private $lock = false;
public function __construct(
Provider $filterProvider,
LanguageSwitch $languageSwitch,
WPML_Locale $locale,
GettextSettings $gettextSettings,
Manager $fileManager
) {
$this->filterProvider = $filterProvider;
$this->languageSwitch = $languageSwitch;
$this->locale = $locale;
$this->gettextSettings = $gettextSettings;
$this->fileManager = $fileManager;
}
public function init() {
$this->languageSwitch->initCurrentLocale();
$this->isAutoRegisterDisabled = ! $this->gettextSettings->isAutoRegistrationEnabled();
}
/**
* @param string|array $wpmlContext
* @param string $name
* @param bool|string $value
* @param bool $allowEmptyValue
* @param null|bool $hasTranslation
* @param null|string $targetLang
*
* @return bool|string
*/
public function translate( $wpmlContext, $name, $value = false, $allowEmptyValue = false, &$hasTranslation = null, $targetLang = null ) {
if ( $this->lock ) {
return $value;
}
$this->lock = true;
if ( wpml_st_is_requested_blog() ) {
if ( $this->isAutoRegisterDisabled && self::canTranslateWithMO( $value, $name ) ) {
$value = $this->translateByMOFile( $wpmlContext, $name, $value, $hasTranslation, $targetLang );
} else {
$value = $this->translateByDBQuery( $wpmlContext, $name, $value, $hasTranslation, $targetLang );
}
}
$this->lock = false;
return $value;
}
/**
* @param string|array $wpmlContext
* @param string $name
* @param bool|string $value
* @param null|bool $hasTranslation
* @param null|string $targetLang
*
* @return string
*/
private function translateByMOFile( $wpmlContext, $name, $value, &$hasTranslation, $targetLang ) {
list ( $domain, $gettextContext ) = wpml_st_extract_context_parameters( $wpmlContext );
$translateByName = function ( $locale ) use ( $name, $domain, $gettextContext ) {
$this->loadTextDomain( $domain, $locale );
if ( $gettextContext ) {
return _x( $name, $gettextContext, $domain );
} else {
return __( $name, $domain );
}
};
$new_value = $this->withMOLocale( $targetLang, $translateByName );
$hasTranslation = $new_value !== $name;
if ( $hasTranslation ) {
$value = $new_value;
}
return $value;
}
/**
* @param string|array $wpmlContext
* @param string $name
* @param bool|string $value
* @param null|bool $hasTranslation
* @param null|string $targetLang
*
* @return string
*/
private function translateByDBQuery( $wpmlContext, $name, $value, &$hasTranslation, $targetLang ) {
$filter = $this->filterProvider->getFilter( $targetLang, $name );
if ( $filter ) {
$value = $filter->translate_by_name_and_context( $value, $name, $wpmlContext, $hasTranslation );
}
return $value;
}
/**
* @param string $domain
* @param string $locale
*/
private function loadTextDomain( $domain, $locale ) {
if (
! isset( $GLOBALS['l10n'][ $domain ] )
&& ! isset( $GLOBALS['l10n_unloaded'][ $domain ] )
&& ! isset( self::$loadedDomains[ $locale ][ $domain ] )
) {
load_textdomain(
$domain,
$this->fileManager->getFilepath( $domain, $locale )
);
self::$loadedDomains[ $locale ][ $domain ] = true;
}
}
/**
* @param string $targetLang
* @param callable $function
*
* @return string
*/
private function withMOLocale( $targetLang, $function ) {
$initialLocale = $this->languageSwitch->getCurrentLocale();
if ( $targetLang ) {
/** @var string $targetLocale */
$targetLocale = $this->locale->get_locale( $targetLang );
$this->languageSwitch->switchToLocale( $targetLocale );
$result = $function( $targetLocale );
$this->languageSwitch->switchToLocale( $initialLocale );
} else {
$result = $function( $initialLocale );
}
return $result;
}
/**
* We will allow MO translation only when
* the original is not empty.
*
* We also need to make sure we deal with a
* WPML registered string (not gettext).
*
* If those conditions are not fulfilled,
* we will translate from the database.
*
* @param string|bool $original
* @param string $name
*
* @return bool
*/
public static function canTranslateWithMO( $original, $name ) {
return $original && self::isWpmlRegisteredString( $original, $name );
}
/**
* This allows to differentiate WPML registered strings
* from gettext strings that have the default hash for
* the name.
*
* But it's still possible that WPML registered strings
* have a hash for the name.
*
* @param string|bool $original
* @param string $name
*
* @return bool
*/
private static function isWpmlRegisteredString( $original, $name ) {
return $name && md5( (string) $original ) !== $name;
}
public static function resetCache() {
self::$loadedDomains = [];
}
}