author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 18:28:13 +0200 | |
changeset 9 | 177826044cd9 |
parent 7 | cf61fcea0001 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
* Core Translation API |
0 | 4 |
* |
5 |
* @package WordPress |
|
6 |
* @subpackage i18n |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7 |
* @since 1.2.0 |
0 | 8 |
*/ |
9 |
||
10 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
11 |
* Retrieves the current locale. |
0 | 12 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
13 |
* If the locale is set, then it will filter the locale in the {@see 'locale'} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
14 |
* filter hook and return the value. |
0 | 15 |
* |
16 |
* If the locale is not set already, then the WPLANG constant is used if it is |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
17 |
* defined. Then it is filtered through the {@see 'locale'} filter hook and |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
18 |
* the value for the locale global set and the locale is returned. |
0 | 19 |
* |
20 |
* The process to get the locale should only be done once, but the locale will |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
21 |
* always be filtered using the {@see 'locale'} hook. |
0 | 22 |
* |
23 |
* @since 1.5.0 |
|
24 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
25 |
* @global string $locale |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
26 |
* @global string $wp_local_package |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
27 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
28 |
* @return string The locale of the blog or from the {@see 'locale'} hook. |
0 | 29 |
*/ |
30 |
function get_locale() { |
|
5 | 31 |
global $locale, $wp_local_package; |
0 | 32 |
|
5 | 33 |
if ( isset( $locale ) ) { |
0 | 34 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
35 |
* Filters the locale ID of the WordPress installation. |
0 | 36 |
* |
5 | 37 |
* @since 1.5.0 |
0 | 38 |
* |
39 |
* @param string $locale The locale ID. |
|
40 |
*/ |
|
41 |
return apply_filters( 'locale', $locale ); |
|
5 | 42 |
} |
0 | 43 |
|
5 | 44 |
if ( isset( $wp_local_package ) ) { |
45 |
$locale = $wp_local_package; |
|
46 |
} |
|
47 |
||
48 |
// WPLANG was defined in wp-config. |
|
49 |
if ( defined( 'WPLANG' ) ) { |
|
0 | 50 |
$locale = WPLANG; |
5 | 51 |
} |
0 | 52 |
|
53 |
// If multisite, check options. |
|
54 |
if ( is_multisite() ) { |
|
55 |
// Don't check blog option when installing. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
56 |
if ( wp_installing() || ( false === $ms_locale = get_option( 'WPLANG' ) ) ) { |
5 | 57 |
$ms_locale = get_site_option( 'WPLANG' ); |
58 |
} |
|
0 | 59 |
|
5 | 60 |
if ( $ms_locale !== false ) { |
0 | 61 |
$locale = $ms_locale; |
5 | 62 |
} |
63 |
} else { |
|
64 |
$db_locale = get_option( 'WPLANG' ); |
|
65 |
if ( $db_locale !== false ) { |
|
66 |
$locale = $db_locale; |
|
67 |
} |
|
0 | 68 |
} |
69 |
||
5 | 70 |
if ( empty( $locale ) ) { |
0 | 71 |
$locale = 'en_US'; |
5 | 72 |
} |
0 | 73 |
|
5 | 74 |
/** This filter is documented in wp-includes/l10n.php */ |
0 | 75 |
return apply_filters( 'locale', $locale ); |
76 |
} |
|
77 |
||
78 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
79 |
* Retrieves the locale of a user. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
80 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
81 |
* If the user has a locale set to a non-empty string then it will be |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
82 |
* returned. Otherwise it returns the locale of get_locale(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
83 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
84 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
85 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
86 |
* @param int|WP_User $user_id User's ID or a WP_User object. Defaults to current user. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
87 |
* @return string The locale of the user. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
88 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
89 |
function get_user_locale( $user_id = 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
90 |
$user = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
91 |
if ( 0 === $user_id && function_exists( 'wp_get_current_user' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
92 |
$user = wp_get_current_user(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
93 |
} elseif ( $user_id instanceof WP_User ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
94 |
$user = $user_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
95 |
} elseif ( $user_id && is_numeric( $user_id ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
96 |
$user = get_user_by( 'id', $user_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
97 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
98 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
99 |
if ( ! $user ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
100 |
return get_locale(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
101 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
102 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
103 |
$locale = $user->locale; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
104 |
return $locale ? $locale : get_locale(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
105 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
106 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
107 |
/** |
9 | 108 |
* Determine the current locale desired for the request. |
109 |
* |
|
110 |
* @since 5.0.0 |
|
111 |
* |
|
112 |
* @global string $pagenow |
|
113 |
* |
|
114 |
* @return string The determined locale. |
|
115 |
*/ |
|
116 |
function determine_locale() { |
|
117 |
/** |
|
118 |
* Filters the locale for the current request prior to the default determination process. |
|
119 |
* |
|
120 |
* Using this filter allows to override the default logic, effectively short-circuiting the function. |
|
121 |
* |
|
122 |
* @since 5.0.0 |
|
123 |
* |
|
124 |
* @param string|null The locale to return and short-circuit, or null as default. |
|
125 |
*/ |
|
126 |
$determined_locale = apply_filters( 'pre_determine_locale', null ); |
|
127 |
if ( ! empty( $determined_locale ) && is_string( $determined_locale ) ) { |
|
128 |
return $determined_locale; |
|
129 |
} |
|
130 |
||
131 |
$determined_locale = get_locale(); |
|
132 |
||
133 |
if ( is_admin() ) { |
|
134 |
$determined_locale = get_user_locale(); |
|
135 |
} |
|
136 |
||
137 |
if ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() ) { |
|
138 |
$determined_locale = get_user_locale(); |
|
139 |
} |
|
140 |
||
141 |
if ( ! empty( $_GET['wp_lang'] ) && ! empty( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) { |
|
142 |
$determined_locale = sanitize_text_field( $_GET['wp_lang'] ); |
|
143 |
} |
|
144 |
||
145 |
/** |
|
146 |
* Filters the locale for the current request. |
|
147 |
* |
|
148 |
* @since 5.0.0 |
|
149 |
* |
|
150 |
* @param string $locale The locale. |
|
151 |
*/ |
|
152 |
return apply_filters( 'determine_locale', $determined_locale ); |
|
153 |
} |
|
154 |
||
155 |
/** |
|
0 | 156 |
* Retrieve the translation of $text. |
157 |
* |
|
158 |
* If there is no translation, or the text domain isn't loaded, the original text is returned. |
|
159 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
160 |
* *Note:* Don't use translate() directly, use __() or related functions. |
0 | 161 |
* |
162 |
* @since 2.2.0 |
|
163 |
* |
|
164 |
* @param string $text Text to translate. |
|
165 |
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
166 |
* Default 'default'. |
0 | 167 |
* @return string Translated text |
168 |
*/ |
|
169 |
function translate( $text, $domain = 'default' ) { |
|
170 |
$translations = get_translations_for_domain( $domain ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
171 |
$translation = $translations->translate( $text ); |
5 | 172 |
|
0 | 173 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
174 |
* Filters text with its translation. |
0 | 175 |
* |
176 |
* @since 2.0.11 |
|
177 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
178 |
* @param string $translation Translated text. |
0 | 179 |
* @param string $text Text to translate. |
180 |
* @param string $domain Text domain. Unique identifier for retrieving translated strings. |
|
181 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
182 |
return apply_filters( 'gettext', $translation, $text, $domain ); |
0 | 183 |
} |
184 |
||
185 |
/** |
|
186 |
* Remove last item on a pipe-delimited string. |
|
187 |
* |
|
188 |
* Meant for removing the last item in a string, such as 'Role name|User role'. The original |
|
189 |
* string will be returned if no pipe '|' characters are found in the string. |
|
190 |
* |
|
191 |
* @since 2.8.0 |
|
192 |
* |
|
193 |
* @param string $string A pipe-delimited string. |
|
194 |
* @return string Either $string or everything before the last pipe. |
|
195 |
*/ |
|
196 |
function before_last_bar( $string ) { |
|
197 |
$last_bar = strrpos( $string, '|' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
198 |
if ( false === $last_bar ) { |
0 | 199 |
return $string; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
200 |
} else { |
0 | 201 |
return substr( $string, 0, $last_bar ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
202 |
} |
0 | 203 |
} |
204 |
||
205 |
/** |
|
206 |
* Retrieve the translation of $text in the context defined in $context. |
|
207 |
* |
|
208 |
* If there is no translation, or the text domain isn't loaded the original |
|
209 |
* text is returned. |
|
210 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
211 |
* *Note:* Don't use translate_with_gettext_context() directly, use _x() or related functions. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
212 |
* |
0 | 213 |
* @since 2.8.0 |
214 |
* |
|
215 |
* @param string $text Text to translate. |
|
216 |
* @param string $context Context information for the translators. |
|
217 |
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
218 |
* Default 'default'. |
0 | 219 |
* @return string Translated text on success, original text on failure. |
220 |
*/ |
|
221 |
function translate_with_gettext_context( $text, $context, $domain = 'default' ) { |
|
222 |
$translations = get_translations_for_domain( $domain ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
223 |
$translation = $translations->translate( $text, $context ); |
0 | 224 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
225 |
* Filters text with its translation based on context information. |
0 | 226 |
* |
227 |
* @since 2.8.0 |
|
228 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
229 |
* @param string $translation Translated text. |
0 | 230 |
* @param string $text Text to translate. |
231 |
* @param string $context Context information for the translators. |
|
232 |
* @param string $domain Text domain. Unique identifier for retrieving translated strings. |
|
233 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
234 |
return apply_filters( 'gettext_with_context', $translation, $text, $context, $domain ); |
0 | 235 |
} |
236 |
||
237 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
238 |
* Retrieve the translation of $text. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
239 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
240 |
* If there is no translation, or the text domain isn't loaded, the original text is returned. |
0 | 241 |
* |
242 |
* @since 2.1.0 |
|
243 |
* |
|
244 |
* @param string $text Text to translate. |
|
245 |
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
246 |
* Default 'default'. |
0 | 247 |
* @return string Translated text. |
248 |
*/ |
|
249 |
function __( $text, $domain = 'default' ) { |
|
250 |
return translate( $text, $domain ); |
|
251 |
} |
|
252 |
||
253 |
/** |
|
254 |
* Retrieve the translation of $text and escapes it for safe use in an attribute. |
|
255 |
* |
|
256 |
* If there is no translation, or the text domain isn't loaded, the original text is returned. |
|
257 |
* |
|
258 |
* @since 2.8.0 |
|
259 |
* |
|
260 |
* @param string $text Text to translate. |
|
261 |
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
262 |
* Default 'default'. |
0 | 263 |
* @return string Translated text on success, original text on failure. |
264 |
*/ |
|
265 |
function esc_attr__( $text, $domain = 'default' ) { |
|
266 |
return esc_attr( translate( $text, $domain ) ); |
|
267 |
} |
|
268 |
||
269 |
/** |
|
270 |
* Retrieve the translation of $text and escapes it for safe use in HTML output. |
|
271 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
272 |
* If there is no translation, or the text domain isn't loaded, the original text |
9 | 273 |
* is escaped and returned. |
0 | 274 |
* |
275 |
* @since 2.8.0 |
|
276 |
* |
|
277 |
* @param string $text Text to translate. |
|
278 |
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
279 |
* Default 'default'. |
0 | 280 |
* @return string Translated text |
281 |
*/ |
|
282 |
function esc_html__( $text, $domain = 'default' ) { |
|
283 |
return esc_html( translate( $text, $domain ) ); |
|
284 |
} |
|
285 |
||
286 |
/** |
|
287 |
* Display translated text. |
|
288 |
* |
|
289 |
* @since 1.2.0 |
|
290 |
* |
|
291 |
* @param string $text Text to translate. |
|
292 |
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
293 |
* Default 'default'. |
0 | 294 |
*/ |
295 |
function _e( $text, $domain = 'default' ) { |
|
296 |
echo translate( $text, $domain ); |
|
297 |
} |
|
298 |
||
299 |
/** |
|
300 |
* Display translated text that has been escaped for safe use in an attribute. |
|
301 |
* |
|
302 |
* @since 2.8.0 |
|
303 |
* |
|
304 |
* @param string $text Text to translate. |
|
305 |
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
306 |
* Default 'default'. |
0 | 307 |
*/ |
308 |
function esc_attr_e( $text, $domain = 'default' ) { |
|
309 |
echo esc_attr( translate( $text, $domain ) ); |
|
310 |
} |
|
311 |
||
312 |
/** |
|
313 |
* Display translated text that has been escaped for safe use in HTML output. |
|
314 |
* |
|
315 |
* @since 2.8.0 |
|
316 |
* |
|
317 |
* @param string $text Text to translate. |
|
318 |
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
319 |
* Default 'default'. |
0 | 320 |
*/ |
321 |
function esc_html_e( $text, $domain = 'default' ) { |
|
322 |
echo esc_html( translate( $text, $domain ) ); |
|
323 |
} |
|
324 |
||
325 |
/** |
|
326 |
* Retrieve translated string with gettext context. |
|
327 |
* |
|
328 |
* Quite a few times, there will be collisions with similar translatable text |
|
329 |
* found in more than two places, but with different translated context. |
|
330 |
* |
|
331 |
* By including the context in the pot file, translators can translate the two |
|
332 |
* strings differently. |
|
333 |
* |
|
334 |
* @since 2.8.0 |
|
335 |
* |
|
336 |
* @param string $text Text to translate. |
|
337 |
* @param string $context Context information for the translators. |
|
338 |
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
339 |
* Default 'default'. |
0 | 340 |
* @return string Translated context string without pipe. |
341 |
*/ |
|
342 |
function _x( $text, $context, $domain = 'default' ) { |
|
343 |
return translate_with_gettext_context( $text, $context, $domain ); |
|
344 |
} |
|
345 |
||
346 |
/** |
|
347 |
* Display translated string with gettext context. |
|
348 |
* |
|
349 |
* @since 3.0.0 |
|
350 |
* |
|
351 |
* @param string $text Text to translate. |
|
352 |
* @param string $context Context information for the translators. |
|
353 |
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
354 |
* Default 'default'. |
0 | 355 |
* @return string Translated context string without pipe. |
356 |
*/ |
|
357 |
function _ex( $text, $context, $domain = 'default' ) { |
|
358 |
echo _x( $text, $context, $domain ); |
|
359 |
} |
|
360 |
||
361 |
/** |
|
362 |
* Translate string with gettext context, and escapes it for safe use in an attribute. |
|
363 |
* |
|
364 |
* @since 2.8.0 |
|
365 |
* |
|
366 |
* @param string $text Text to translate. |
|
367 |
* @param string $context Context information for the translators. |
|
368 |
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
369 |
* Default 'default'. |
0 | 370 |
* @return string Translated text |
371 |
*/ |
|
372 |
function esc_attr_x( $text, $context, $domain = 'default' ) { |
|
373 |
return esc_attr( translate_with_gettext_context( $text, $context, $domain ) ); |
|
374 |
} |
|
375 |
||
376 |
/** |
|
377 |
* Translate string with gettext context, and escapes it for safe use in HTML output. |
|
378 |
* |
|
379 |
* @since 2.9.0 |
|
380 |
* |
|
381 |
* @param string $text Text to translate. |
|
382 |
* @param string $context Context information for the translators. |
|
383 |
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
384 |
* Default 'default'. |
0 | 385 |
* @return string Translated text. |
386 |
*/ |
|
387 |
function esc_html_x( $text, $context, $domain = 'default' ) { |
|
388 |
return esc_html( translate_with_gettext_context( $text, $context, $domain ) ); |
|
389 |
} |
|
390 |
||
391 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
392 |
* Translates and retrieves the singular or plural form based on the supplied number. |
0 | 393 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
394 |
* Used when you want to use the appropriate form of a string based on whether a |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
395 |
* number is singular or plural. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
396 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
397 |
* Example: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
398 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
399 |
* printf( _n( '%s person', '%s people', $count, 'text-domain' ), number_format_i18n( $count ) ); |
0 | 400 |
* |
401 |
* @since 2.8.0 |
|
402 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
403 |
* @param string $single The text to be used if the number is singular. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
404 |
* @param string $plural The text to be used if the number is plural. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
405 |
* @param int $number The number to compare against to use either the singular or plural form. |
0 | 406 |
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
407 |
* Default 'default'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
408 |
* @return string The translated singular or plural form. |
0 | 409 |
*/ |
410 |
function _n( $single, $plural, $number, $domain = 'default' ) { |
|
411 |
$translations = get_translations_for_domain( $domain ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
412 |
$translation = $translations->translate_plural( $single, $plural, $number ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
413 |
|
0 | 414 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
415 |
* Filters the singular or plural form of a string. |
0 | 416 |
* |
417 |
* @since 2.2.0 |
|
418 |
* |
|
419 |
* @param string $translation Translated text. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
420 |
* @param string $single The text to be used if the number is singular. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
421 |
* @param string $plural The text to be used if the number is plural. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
422 |
* @param string $number The number to compare against to use either the singular or plural form. |
0 | 423 |
* @param string $domain Text domain. Unique identifier for retrieving translated strings. |
424 |
*/ |
|
425 |
return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain ); |
|
426 |
} |
|
427 |
||
428 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
429 |
* Translates and retrieves the singular or plural form based on the supplied number, with gettext context. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
430 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
431 |
* This is a hybrid of _n() and _x(). It supports context and plurals. |
0 | 432 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
433 |
* Used when you want to use the appropriate form of a string with context based on whether a |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
434 |
* number is singular or plural. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
435 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
436 |
* Example of a generic phrase which is disambiguated via the context parameter: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
437 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
438 |
* printf( _nx( '%s group', '%s groups', $people, 'group of people', 'text-domain' ), number_format_i18n( $people ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
439 |
* printf( _nx( '%s group', '%s groups', $animals, 'group of animals', 'text-domain' ), number_format_i18n( $animals ) ); |
0 | 440 |
* |
441 |
* @since 2.8.0 |
|
442 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
443 |
* @param string $single The text to be used if the number is singular. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
444 |
* @param string $plural The text to be used if the number is plural. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
445 |
* @param int $number The number to compare against to use either the singular or plural form. |
0 | 446 |
* @param string $context Context information for the translators. |
447 |
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
448 |
* Default 'default'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
449 |
* @return string The translated singular or plural form. |
0 | 450 |
*/ |
9 | 451 |
function _nx( $single, $plural, $number, $context, $domain = 'default' ) { |
0 | 452 |
$translations = get_translations_for_domain( $domain ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
453 |
$translation = $translations->translate_plural( $single, $plural, $number, $context ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
454 |
|
0 | 455 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
456 |
* Filters the singular or plural form of a string with gettext context. |
0 | 457 |
* |
458 |
* @since 2.8.0 |
|
459 |
* |
|
460 |
* @param string $translation Translated text. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
461 |
* @param string $single The text to be used if the number is singular. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
462 |
* @param string $plural The text to be used if the number is plural. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
463 |
* @param string $number The number to compare against to use either the singular or plural form. |
0 | 464 |
* @param string $context Context information for the translators. |
465 |
* @param string $domain Text domain. Unique identifier for retrieving translated strings. |
|
466 |
*/ |
|
467 |
return apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain ); |
|
468 |
} |
|
469 |
||
470 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
471 |
* Registers plural strings in POT file, but does not translate them. |
0 | 472 |
* |
473 |
* Used when you want to keep structures with translatable plural |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
474 |
* strings and use them later when the number is known. |
0 | 475 |
* |
476 |
* Example: |
|
5 | 477 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
478 |
* $message = _n_noop( '%s post', '%s posts', 'text-domain' ); |
5 | 479 |
* ... |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
480 |
* printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) ); |
0 | 481 |
* |
482 |
* @since 2.5.0 |
|
483 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
484 |
* @param string $singular Singular form to be localized. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
485 |
* @param string $plural Plural form to be localized. |
0 | 486 |
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
487 |
* Default null. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
488 |
* @return array { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
489 |
* Array of translation information for the strings. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
490 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
491 |
* @type string $0 Singular form to be localized. No longer used. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
492 |
* @type string $1 Plural form to be localized. No longer used. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
493 |
* @type string $singular Singular form to be localized. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
494 |
* @type string $plural Plural form to be localized. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
495 |
* @type null $context Context information for the translators. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
496 |
* @type string $domain Text domain. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
497 |
* } |
0 | 498 |
*/ |
499 |
function _n_noop( $singular, $plural, $domain = null ) { |
|
9 | 500 |
return array( |
501 |
0 => $singular, |
|
502 |
1 => $plural, |
|
503 |
'singular' => $singular, |
|
504 |
'plural' => $plural, |
|
505 |
'context' => null, |
|
506 |
'domain' => $domain, |
|
507 |
); |
|
0 | 508 |
} |
509 |
||
510 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
511 |
* Registers plural strings with gettext context in POT file, but does not translate them. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
512 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
513 |
* Used when you want to keep structures with translatable plural |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
514 |
* strings and use them later when the number is known. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
515 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
516 |
* Example of a generic phrase which is disambiguated via the context parameter: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
517 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
518 |
* $messages = array( |
9 | 519 |
* 'people' => _nx_noop( '%s group', '%s groups', 'people', 'text-domain' ), |
520 |
* 'animals' => _nx_noop( '%s group', '%s groups', 'animals', 'text-domain' ), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
521 |
* ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
522 |
* ... |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
523 |
* $message = $messages[ $type ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
524 |
* printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) ); |
0 | 525 |
* |
526 |
* @since 2.8.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
527 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
528 |
* @param string $singular Singular form to be localized. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
529 |
* @param string $plural Plural form to be localized. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
530 |
* @param string $context Context information for the translators. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
531 |
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
532 |
* Default null. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
533 |
* @return array { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
534 |
* Array of translation information for the strings. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
535 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
536 |
* @type string $0 Singular form to be localized. No longer used. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
537 |
* @type string $1 Plural form to be localized. No longer used. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
538 |
* @type string $2 Context information for the translators. No longer used. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
539 |
* @type string $singular Singular form to be localized. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
540 |
* @type string $plural Plural form to be localized. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
541 |
* @type string $context Context information for the translators. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
542 |
* @type string $domain Text domain. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
543 |
* } |
0 | 544 |
*/ |
545 |
function _nx_noop( $singular, $plural, $context, $domain = null ) { |
|
9 | 546 |
return array( |
547 |
0 => $singular, |
|
548 |
1 => $plural, |
|
549 |
2 => $context, |
|
550 |
'singular' => $singular, |
|
551 |
'plural' => $plural, |
|
552 |
'context' => $context, |
|
553 |
'domain' => $domain, |
|
554 |
); |
|
0 | 555 |
} |
556 |
||
557 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
558 |
* Translates and retrieves the singular or plural form of a string that's been registered |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
559 |
* with _n_noop() or _nx_noop(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
560 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
561 |
* Used when you want to use a translatable plural string once the number is known. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
562 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
563 |
* Example: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
564 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
565 |
* $message = _n_noop( '%s post', '%s posts', 'text-domain' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
566 |
* ... |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
567 |
* printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) ); |
0 | 568 |
* |
569 |
* @since 3.1.0 |
|
570 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
571 |
* @param array $nooped_plural Array with singular, plural, and context keys, usually the result of _n_noop() or _nx_noop(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
572 |
* @param int $count Number of objects. |
0 | 573 |
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. If $nooped_plural contains |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
574 |
* a text domain passed to _n_noop() or _nx_noop(), it will override this value. Default 'default'. |
0 | 575 |
* @return string Either $single or $plural translated text. |
576 |
*/ |
|
577 |
function translate_nooped_plural( $nooped_plural, $count, $domain = 'default' ) { |
|
9 | 578 |
if ( $nooped_plural['domain'] ) { |
0 | 579 |
$domain = $nooped_plural['domain']; |
9 | 580 |
} |
0 | 581 |
|
9 | 582 |
if ( $nooped_plural['context'] ) { |
0 | 583 |
return _nx( $nooped_plural['singular'], $nooped_plural['plural'], $count, $nooped_plural['context'], $domain ); |
9 | 584 |
} else { |
0 | 585 |
return _n( $nooped_plural['singular'], $nooped_plural['plural'], $count, $domain ); |
9 | 586 |
} |
0 | 587 |
} |
588 |
||
589 |
/** |
|
590 |
* Load a .mo file into the text domain $domain. |
|
591 |
* |
|
592 |
* If the text domain already exists, the translations will be merged. If both |
|
593 |
* sets have the same string, the translation from the original value will be taken. |
|
594 |
* |
|
595 |
* On success, the .mo file will be placed in the $l10n global by $domain |
|
596 |
* and will be a MO object. |
|
597 |
* |
|
598 |
* @since 1.5.0 |
|
599 |
* |
|
9 | 600 |
* @global MO[] $l10n An array of all currently loaded text domains. |
601 |
* @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
602 |
* |
0 | 603 |
* @param string $domain Text domain. Unique identifier for retrieving translated strings. |
604 |
* @param string $mofile Path to the .mo file. |
|
605 |
* @return bool True on success, false on failure. |
|
606 |
*/ |
|
607 |
function load_textdomain( $domain, $mofile ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
608 |
global $l10n, $l10n_unloaded; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
609 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
610 |
$l10n_unloaded = (array) $l10n_unloaded; |
0 | 611 |
|
612 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
613 |
* Filters whether to override the .mo file loading. |
0 | 614 |
* |
615 |
* @since 2.9.0 |
|
616 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
617 |
* @param bool $override Whether to override the .mo file loading. Default false. |
5 | 618 |
* @param string $domain Text domain. Unique identifier for retrieving translated strings. |
619 |
* @param string $mofile Path to the MO file. |
|
0 | 620 |
*/ |
621 |
$plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile ); |
|
622 |
||
623 |
if ( true == $plugin_override ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
624 |
unset( $l10n_unloaded[ $domain ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
625 |
|
0 | 626 |
return true; |
627 |
} |
|
628 |
||
629 |
/** |
|
630 |
* Fires before the MO translation file is loaded. |
|
631 |
* |
|
632 |
* @since 2.9.0 |
|
633 |
* |
|
634 |
* @param string $domain Text domain. Unique identifier for retrieving translated strings. |
|
635 |
* @param string $mofile Path to the .mo file. |
|
636 |
*/ |
|
637 |
do_action( 'load_textdomain', $domain, $mofile ); |
|
638 |
||
639 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
640 |
* Filters MO file path for loading translations for a specific text domain. |
0 | 641 |
* |
642 |
* @since 2.9.0 |
|
643 |
* |
|
644 |
* @param string $mofile Path to the MO file. |
|
645 |
* @param string $domain Text domain. Unique identifier for retrieving translated strings. |
|
646 |
*/ |
|
647 |
$mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain ); |
|
648 |
||
9 | 649 |
if ( ! is_readable( $mofile ) ) { |
650 |
return false; |
|
651 |
} |
|
0 | 652 |
|
653 |
$mo = new MO(); |
|
9 | 654 |
if ( ! $mo->import_from_file( $mofile ) ) { |
655 |
return false; |
|
656 |
} |
|
0 | 657 |
|
9 | 658 |
if ( isset( $l10n[ $domain ] ) ) { |
659 |
$mo->merge_with( $l10n[ $domain ] ); |
|
660 |
} |
|
0 | 661 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
662 |
unset( $l10n_unloaded[ $domain ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
663 |
|
9 | 664 |
$l10n[ $domain ] = &$mo; |
0 | 665 |
|
666 |
return true; |
|
667 |
} |
|
668 |
||
669 |
/** |
|
670 |
* Unload translations for a text domain. |
|
671 |
* |
|
672 |
* @since 3.0.0 |
|
673 |
* |
|
9 | 674 |
* @global MO[] $l10n An array of all currently loaded text domains. |
675 |
* @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
676 |
* |
0 | 677 |
* @param string $domain Text domain. Unique identifier for retrieving translated strings. |
678 |
* @return bool Whether textdomain was unloaded. |
|
679 |
*/ |
|
680 |
function unload_textdomain( $domain ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
681 |
global $l10n, $l10n_unloaded; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
682 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
683 |
$l10n_unloaded = (array) $l10n_unloaded; |
0 | 684 |
|
685 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
686 |
* Filters whether to override the text domain unloading. |
0 | 687 |
* |
688 |
* @since 3.0.0 |
|
689 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
690 |
* @param bool $override Whether to override the text domain unloading. Default false. |
5 | 691 |
* @param string $domain Text domain. Unique identifier for retrieving translated strings. |
0 | 692 |
*/ |
693 |
$plugin_override = apply_filters( 'override_unload_textdomain', false, $domain ); |
|
694 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
695 |
if ( $plugin_override ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
696 |
$l10n_unloaded[ $domain ] = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
697 |
|
0 | 698 |
return true; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
699 |
} |
0 | 700 |
|
701 |
/** |
|
702 |
* Fires before the text domain is unloaded. |
|
703 |
* |
|
704 |
* @since 3.0.0 |
|
705 |
* |
|
706 |
* @param string $domain Text domain. Unique identifier for retrieving translated strings. |
|
707 |
*/ |
|
708 |
do_action( 'unload_textdomain', $domain ); |
|
709 |
||
9 | 710 |
if ( isset( $l10n[ $domain ] ) ) { |
711 |
unset( $l10n[ $domain ] ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
712 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
713 |
$l10n_unloaded[ $domain ] = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
714 |
|
0 | 715 |
return true; |
716 |
} |
|
717 |
||
718 |
return false; |
|
719 |
} |
|
720 |
||
721 |
/** |
|
722 |
* Load default translated strings based on locale. |
|
723 |
* |
|
724 |
* Loads the .mo file in WP_LANG_DIR constant path from WordPress root. |
|
725 |
* The translated (.mo) file is named based on the locale. |
|
726 |
* |
|
727 |
* @see load_textdomain() |
|
728 |
* |
|
729 |
* @since 1.5.0 |
|
5 | 730 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
731 |
* @param string $locale Optional. Locale to load. Default is the value of get_locale(). |
5 | 732 |
* @return bool Whether the textdomain was loaded. |
0 | 733 |
*/ |
5 | 734 |
function load_default_textdomain( $locale = null ) { |
735 |
if ( null === $locale ) { |
|
9 | 736 |
$locale = determine_locale(); |
5 | 737 |
} |
0 | 738 |
|
5 | 739 |
// Unload previously loaded strings so we can switch translations. |
740 |
unload_textdomain( 'default' ); |
|
741 |
||
742 |
$return = load_textdomain( 'default', WP_LANG_DIR . "/$locale.mo" ); |
|
0 | 743 |
|
9 | 744 |
if ( ( is_multisite() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) && ! file_exists( WP_LANG_DIR . "/admin-$locale.mo" ) ) { |
0 | 745 |
load_textdomain( 'default', WP_LANG_DIR . "/ms-$locale.mo" ); |
5 | 746 |
return $return; |
0 | 747 |
} |
748 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
749 |
if ( is_admin() || wp_installing() || ( defined( 'WP_REPAIRING' ) && WP_REPAIRING ) ) { |
0 | 750 |
load_textdomain( 'default', WP_LANG_DIR . "/admin-$locale.mo" ); |
5 | 751 |
} |
0 | 752 |
|
9 | 753 |
if ( is_network_admin() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) { |
0 | 754 |
load_textdomain( 'default', WP_LANG_DIR . "/admin-network-$locale.mo" ); |
9 | 755 |
} |
0 | 756 |
|
5 | 757 |
return $return; |
0 | 758 |
} |
759 |
||
760 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
761 |
* Loads a plugin's translated strings. |
0 | 762 |
* |
763 |
* If the path is not given then it will be the root of the plugin directory. |
|
764 |
* |
|
765 |
* The .mo file should be named based on the text domain with a dash, and then the locale exactly. |
|
766 |
* |
|
767 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
768 |
* @since 4.6.0 The function now tries to load the .mo file from the languages directory first. |
0 | 769 |
* |
9 | 770 |
* @param string $domain Unique identifier for retrieving translated strings |
771 |
* @param string|false $deprecated Optional. Deprecated. Use the $plugin_rel_path parameter instead. |
|
772 |
* Default false. |
|
773 |
* @param string|false $plugin_rel_path Optional. Relative path to WP_PLUGIN_DIR where the .mo file resides. |
|
774 |
* Default false. |
|
5 | 775 |
* @return bool True when textdomain is successfully loaded, false otherwise. |
0 | 776 |
*/ |
777 |
function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path = false ) { |
|
778 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
779 |
* Filters a plugin's locale. |
0 | 780 |
* |
781 |
* @since 3.0.0 |
|
782 |
* |
|
783 |
* @param string $locale The plugin's current locale. |
|
784 |
* @param string $domain Text domain. Unique identifier for retrieving translated strings. |
|
785 |
*/ |
|
9 | 786 |
$locale = apply_filters( 'plugin_locale', determine_locale(), $domain ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
787 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
788 |
$mofile = $domain . '-' . $locale . '.mo'; |
0 | 789 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
790 |
// Try to load from the languages directory first. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
791 |
if ( load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
792 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
793 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
794 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
795 |
if ( false !== $plugin_rel_path ) { |
0 | 796 |
$path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' ); |
5 | 797 |
} elseif ( false !== $deprecated ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
798 |
_deprecated_argument( __FUNCTION__, '2.7.0' ); |
0 | 799 |
$path = ABSPATH . trim( $deprecated, '/' ); |
800 |
} else { |
|
801 |
$path = WP_PLUGIN_DIR; |
|
802 |
} |
|
803 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
804 |
return load_textdomain( $domain, $path . '/' . $mofile ); |
0 | 805 |
} |
806 |
||
807 |
/** |
|
808 |
* Load the translated strings for a plugin residing in the mu-plugins directory. |
|
809 |
* |
|
810 |
* @since 3.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
811 |
* @since 4.6.0 The function now tries to load the .mo file from the languages directory first. |
0 | 812 |
* |
813 |
* @param string $domain Text domain. Unique identifier for retrieving translated strings. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
814 |
* @param string $mu_plugin_rel_path Optional. Relative to `WPMU_PLUGIN_DIR` directory in which the .mo |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
815 |
* file resides. Default empty string. |
0 | 816 |
* @return bool True when textdomain is successfully loaded, false otherwise. |
817 |
*/ |
|
818 |
function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) { |
|
5 | 819 |
/** This filter is documented in wp-includes/l10n.php */ |
9 | 820 |
$locale = apply_filters( 'plugin_locale', determine_locale(), $domain ); |
0 | 821 |
|
822 |
$mofile = $domain . '-' . $locale . '.mo'; |
|
823 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
824 |
// Try to load from the languages directory first. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
825 |
if ( load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
826 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
827 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
828 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
829 |
$path = WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
830 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
831 |
return load_textdomain( $domain, $path . '/' . $mofile ); |
0 | 832 |
} |
833 |
||
834 |
/** |
|
835 |
* Load the theme's translated strings. |
|
836 |
* |
|
837 |
* If the current locale exists as a .mo file in the theme's root directory, it |
|
838 |
* will be included in the translated strings by the $domain. |
|
839 |
* |
|
840 |
* The .mo files must be named based on the locale exactly. |
|
841 |
* |
|
842 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
843 |
* @since 4.6.0 The function now tries to load the .mo file from the languages directory first. |
0 | 844 |
* |
845 |
* @param string $domain Text domain. Unique identifier for retrieving translated strings. |
|
846 |
* @param string $path Optional. Path to the directory containing the .mo file. |
|
847 |
* Default false. |
|
848 |
* @return bool True when textdomain is successfully loaded, false otherwise. |
|
849 |
*/ |
|
850 |
function load_theme_textdomain( $domain, $path = false ) { |
|
851 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
852 |
* Filters a theme's locale. |
0 | 853 |
* |
854 |
* @since 3.0.0 |
|
855 |
* |
|
856 |
* @param string $locale The theme's current locale. |
|
857 |
* @param string $domain Text domain. Unique identifier for retrieving translated strings. |
|
858 |
*/ |
|
9 | 859 |
$locale = apply_filters( 'theme_locale', determine_locale(), $domain ); |
0 | 860 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
861 |
$mofile = $domain . '-' . $locale . '.mo'; |
0 | 862 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
863 |
// Try to load from the languages directory first. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
864 |
if ( load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
865 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
866 |
} |
0 | 867 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
868 |
if ( ! $path ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
869 |
$path = get_template_directory(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
870 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
871 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
872 |
return load_textdomain( $domain, $path . '/' . $locale . '.mo' ); |
0 | 873 |
} |
874 |
||
875 |
/** |
|
876 |
* Load the child themes translated strings. |
|
877 |
* |
|
878 |
* If the current locale exists as a .mo file in the child themes |
|
879 |
* root directory, it will be included in the translated strings by the $domain. |
|
880 |
* |
|
881 |
* The .mo files must be named based on the locale exactly. |
|
882 |
* |
|
883 |
* @since 2.9.0 |
|
884 |
* |
|
885 |
* @param string $domain Text domain. Unique identifier for retrieving translated strings. |
|
5 | 886 |
* @param string $path Optional. Path to the directory containing the .mo file. |
887 |
* Default false. |
|
0 | 888 |
* @return bool True when the theme textdomain is successfully loaded, false otherwise. |
889 |
*/ |
|
890 |
function load_child_theme_textdomain( $domain, $path = false ) { |
|
9 | 891 |
if ( ! $path ) { |
0 | 892 |
$path = get_stylesheet_directory(); |
9 | 893 |
} |
0 | 894 |
return load_theme_textdomain( $domain, $path ); |
895 |
} |
|
896 |
||
897 |
/** |
|
9 | 898 |
* Loads the script translated strings. |
899 |
* |
|
900 |
* @since 5.0.0 |
|
901 |
* @since 5.0.2 Uses load_script_translations() to load translation data. |
|
902 |
* @since 5.1.0 The `$domain` parameter was made optional. |
|
903 |
* |
|
904 |
* @see WP_Scripts::set_translations() |
|
905 |
* |
|
906 |
* @param string $handle Name of the script to register a translation domain to. |
|
907 |
* @param string $domain Optional. Text domain. Default 'default'. |
|
908 |
* @param string $path Optional. The full file path to the directory containing translation files. |
|
909 |
* |
|
910 |
* @return false|string False if the script textdomain could not be loaded, the translated strings |
|
911 |
* in JSON encoding otherwise. |
|
912 |
*/ |
|
913 |
function load_script_textdomain( $handle, $domain = 'default', $path = null ) { |
|
914 |
$wp_scripts = wp_scripts(); |
|
915 |
||
916 |
if ( ! isset( $wp_scripts->registered[ $handle ] ) ) { |
|
917 |
return false; |
|
918 |
} |
|
919 |
||
920 |
$path = untrailingslashit( $path ); |
|
921 |
$locale = determine_locale(); |
|
922 |
||
923 |
// If a path was given and the handle file exists simply return it. |
|
924 |
$file_base = $domain === 'default' ? $locale : $domain . '-' . $locale; |
|
925 |
$handle_filename = $file_base . '-' . $handle . '.json'; |
|
926 |
||
927 |
if ( $path ) { |
|
928 |
$translations = load_script_translations( $path . '/' . $handle_filename, $handle, $domain ); |
|
929 |
||
930 |
if ( $translations ) { |
|
931 |
return $translations; |
|
932 |
} |
|
933 |
} |
|
934 |
||
935 |
$src = $wp_scripts->registered[ $handle ]->src; |
|
936 |
||
937 |
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && 0 === strpos( $src, $wp_scripts->content_url ) ) ) { |
|
938 |
$src = $wp_scripts->base_url . $src; |
|
939 |
} |
|
940 |
||
941 |
$relative = false; |
|
942 |
$languages_path = WP_LANG_DIR; |
|
943 |
||
944 |
$src_url = wp_parse_url( $src ); |
|
945 |
$content_url = wp_parse_url( content_url() ); |
|
946 |
$site_url = wp_parse_url( site_url() ); |
|
947 |
||
948 |
// If the host is the same or it's a relative URL. |
|
949 |
if ( |
|
950 |
strpos( $src_url['path'], $content_url['path'] ) === 0 && |
|
951 |
( ! isset( $src_url['host'] ) || $src_url['host'] === $content_url['host'] ) |
|
952 |
) { |
|
953 |
// Make the src relative the specific plugin or theme. |
|
954 |
$relative = trim( substr( $src_url['path'], strlen( $content_url['path'] ) ), '/' ); |
|
955 |
$relative = explode( '/', $relative ); |
|
956 |
||
957 |
$languages_path = WP_LANG_DIR . '/' . $relative[0]; |
|
958 |
||
959 |
$relative = array_slice( $relative, 2 ); |
|
960 |
$relative = implode( '/', $relative ); |
|
961 |
} elseif ( ! isset( $src_url['host'] ) || $src_url['host'] === $site_url['host'] ) { |
|
962 |
if ( ! isset( $site_url['path'] ) ) { |
|
963 |
$relative = trim( $src_url['path'], '/' ); |
|
964 |
} elseif ( ( strpos( $src_url['path'], trailingslashit( $site_url['path'] ) ) === 0 ) ) { |
|
965 |
// Make the src relative to the WP root. |
|
966 |
$relative = substr( $src_url['path'], strlen( $site_url['path'] ) ); |
|
967 |
$relative = trim( $relative, '/' ); |
|
968 |
} |
|
969 |
} |
|
970 |
||
971 |
/** |
|
972 |
* Filters the relative path of scripts used for finding translation files. |
|
973 |
* |
|
974 |
* @since 5.0.2 |
|
975 |
* |
|
976 |
* @param string $relative The relative path of the script. False if it could not be determined. |
|
977 |
* @param string $src The full source url of the script. |
|
978 |
*/ |
|
979 |
$relative = apply_filters( 'load_script_textdomain_relative_path', $relative, $src ); |
|
980 |
||
981 |
// If the source is not from WP. |
|
982 |
if ( false === $relative ) { |
|
983 |
return load_script_translations( false, $handle, $domain ); |
|
984 |
} |
|
985 |
||
986 |
// Translations are always based on the unminified filename. |
|
987 |
if ( substr( $relative, -7 ) === '.min.js' ) { |
|
988 |
$relative = substr( $relative, 0, -7 ) . '.js'; |
|
989 |
} |
|
990 |
||
991 |
$md5_filename = $file_base . '-' . md5( $relative ) . '.json'; |
|
992 |
||
993 |
if ( $path ) { |
|
994 |
$translations = load_script_translations( $path . '/' . $md5_filename, $handle, $domain ); |
|
995 |
||
996 |
if ( $translations ) { |
|
997 |
return $translations; |
|
998 |
} |
|
999 |
} |
|
1000 |
||
1001 |
$translations = load_script_translations( $languages_path . '/' . $md5_filename, $handle, $domain ); |
|
1002 |
||
1003 |
if ( $translations ) { |
|
1004 |
return $translations; |
|
1005 |
} |
|
1006 |
||
1007 |
return load_script_translations( false, $handle, $domain ); |
|
1008 |
} |
|
1009 |
||
1010 |
/** |
|
1011 |
* Loads the translation data for the given script handle and text domain. |
|
1012 |
* |
|
1013 |
* @since 5.0.2 |
|
1014 |
* |
|
1015 |
* @param string|false $file Path to the translation file to load. False if there isn't one. |
|
1016 |
* @param string $handle Name of the script to register a translation domain to. |
|
1017 |
* @param string $domain The text domain. |
|
1018 |
* @return string|false The JSON-encoded translated strings for the given script handle and text domain. False if there are none. |
|
1019 |
*/ |
|
1020 |
function load_script_translations( $file, $handle, $domain ) { |
|
1021 |
/** |
|
1022 |
* Pre-filters script translations for the given file, script handle and text domain. |
|
1023 |
* |
|
1024 |
* Returning a non-null value allows to override the default logic, effectively short-circuiting the function. |
|
1025 |
* |
|
1026 |
* @since 5.0.2 |
|
1027 |
* |
|
1028 |
* @param string|false $translations JSON-encoded translation data. Default null. |
|
1029 |
* @param string|false $file Path to the translation file to load. False if there isn't one. |
|
1030 |
* @param string $handle Name of the script to register a translation domain to. |
|
1031 |
* @param string $domain The text domain. |
|
1032 |
*/ |
|
1033 |
$translations = apply_filters( 'pre_load_script_translations', null, $file, $handle, $domain ); |
|
1034 |
||
1035 |
if ( null !== $translations ) { |
|
1036 |
return $translations; |
|
1037 |
} |
|
1038 |
||
1039 |
/** |
|
1040 |
* Filters the file path for loading script translations for the given script handle and text domain. |
|
1041 |
* |
|
1042 |
* @since 5.0.2 |
|
1043 |
* |
|
1044 |
* @param string|false $file Path to the translation file to load. False if there isn't one. |
|
1045 |
* @param string $handle Name of the script to register a translation domain to. |
|
1046 |
* @param string $domain The text domain. |
|
1047 |
*/ |
|
1048 |
$file = apply_filters( 'load_script_translation_file', $file, $handle, $domain ); |
|
1049 |
||
1050 |
if ( ! $file || ! is_readable( $file ) ) { |
|
1051 |
return false; |
|
1052 |
} |
|
1053 |
||
1054 |
$translations = file_get_contents( $file ); |
|
1055 |
||
1056 |
/** |
|
1057 |
* Filters script translations for the given file, script handle and text domain. |
|
1058 |
* |
|
1059 |
* @since 5.0.2 |
|
1060 |
* |
|
1061 |
* @param string $translations JSON-encoded translation data. |
|
1062 |
* @param string $file Path to the translation file that was loaded. |
|
1063 |
* @param string $handle Name of the script to register a translation domain to. |
|
1064 |
* @param string $domain The text domain. |
|
1065 |
*/ |
|
1066 |
return apply_filters( 'load_script_translations', $translations, $file, $handle, $domain ); |
|
1067 |
} |
|
1068 |
||
1069 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1070 |
* Loads plugin and theme textdomains just-in-time. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1071 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1072 |
* When a textdomain is encountered for the first time, we try to load |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1073 |
* the translation file from `wp-content/languages`, removing the need |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1074 |
* to call load_plugin_texdomain() or load_theme_texdomain(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1075 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1076 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1077 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1078 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1079 |
* @see get_translations_for_domain() |
9 | 1080 |
* @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1081 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1082 |
* @param string $domain Text domain. Unique identifier for retrieving translated strings. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1083 |
* @return bool True when the textdomain is successfully loaded, false otherwise. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1084 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1085 |
function _load_textdomain_just_in_time( $domain ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1086 |
global $l10n_unloaded; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1087 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1088 |
$l10n_unloaded = (array) $l10n_unloaded; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1089 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1090 |
// Short-circuit if domain is 'default' which is reserved for core. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1091 |
if ( 'default' === $domain || isset( $l10n_unloaded[ $domain ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1092 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1093 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1094 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1095 |
$translation_path = _get_path_to_translation( $domain ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1096 |
if ( false === $translation_path ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1097 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1098 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1099 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1100 |
return load_textdomain( $domain, $translation_path ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1101 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1102 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1103 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1104 |
* Gets the path to a translation file for loading a textdomain just in time. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1105 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1106 |
* Caches the retrieved results internally. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1107 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1108 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1109 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1110 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1111 |
* @see _load_textdomain_just_in_time() |
9 | 1112 |
* @staticvar array $available_translations |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1113 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1114 |
* @param string $domain Text domain. Unique identifier for retrieving translated strings. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1115 |
* @param bool $reset Whether to reset the internal cache. Used by the switch to locale functionality. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1116 |
* @return string|false The path to the translation file or false if no translation file was found. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1117 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1118 |
function _get_path_to_translation( $domain, $reset = false ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1119 |
static $available_translations = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1120 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1121 |
if ( true === $reset ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1122 |
$available_translations = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1123 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1124 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1125 |
if ( ! isset( $available_translations[ $domain ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1126 |
$available_translations[ $domain ] = _get_path_to_translation_from_lang_dir( $domain ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1127 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1128 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1129 |
return $available_translations[ $domain ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1130 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1131 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1132 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1133 |
* Gets the path to a translation file in the languages directory for the current locale. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1134 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1135 |
* Holds a cached list of available .mo files to improve performance. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1136 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1137 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1138 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1139 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1140 |
* @see _get_path_to_translation() |
9 | 1141 |
* @staticvar array $cached_mofiles |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1142 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1143 |
* @param string $domain Text domain. Unique identifier for retrieving translated strings. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1144 |
* @return string|false The path to the translation file or false if no translation file was found. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1145 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1146 |
function _get_path_to_translation_from_lang_dir( $domain ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1147 |
static $cached_mofiles = null; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1148 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1149 |
if ( null === $cached_mofiles ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1150 |
$cached_mofiles = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1151 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1152 |
$locations = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1153 |
WP_LANG_DIR . '/plugins', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1154 |
WP_LANG_DIR . '/themes', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1155 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1156 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1157 |
foreach ( $locations as $location ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1158 |
$mofiles = glob( $location . '/*.mo' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1159 |
if ( $mofiles ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1160 |
$cached_mofiles = array_merge( $cached_mofiles, $mofiles ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1161 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1162 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1163 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1164 |
|
9 | 1165 |
$locale = determine_locale(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1166 |
$mofile = "{$domain}-{$locale}.mo"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1167 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1168 |
$path = WP_LANG_DIR . '/plugins/' . $mofile; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1169 |
if ( in_array( $path, $cached_mofiles ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1170 |
return $path; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1171 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1172 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1173 |
$path = WP_LANG_DIR . '/themes/' . $mofile; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1174 |
if ( in_array( $path, $cached_mofiles ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1175 |
return $path; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1176 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1177 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1178 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1179 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1180 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1181 |
/** |
0 | 1182 |
* Return the Translations instance for a text domain. |
1183 |
* |
|
1184 |
* If there isn't one, returns empty Translations instance. |
|
1185 |
* |
|
5 | 1186 |
* @since 2.8.0 |
1187 |
* |
|
9 | 1188 |
* @global MO[] $l10n |
1189 |
* @staticvar NOOP_Translations $noop_translations |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1190 |
* |
0 | 1191 |
* @param string $domain Text domain. Unique identifier for retrieving translated strings. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1192 |
* @return Translations|NOOP_Translations A Translations instance. |
0 | 1193 |
*/ |
1194 |
function get_translations_for_domain( $domain ) { |
|
1195 |
global $l10n; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1196 |
if ( isset( $l10n[ $domain ] ) || ( _load_textdomain_just_in_time( $domain ) && isset( $l10n[ $domain ] ) ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1197 |
return $l10n[ $domain ]; |
0 | 1198 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1199 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1200 |
static $noop_translations = null; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1201 |
if ( null === $noop_translations ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1202 |
$noop_translations = new NOOP_Translations; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1203 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1204 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1205 |
return $noop_translations; |
0 | 1206 |
} |
1207 |
||
1208 |
/** |
|
1209 |
* Whether there are translations for the text domain. |
|
1210 |
* |
|
1211 |
* @since 3.0.0 |
|
5 | 1212 |
* |
9 | 1213 |
* @global MO[] $l10n |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1214 |
* |
0 | 1215 |
* @param string $domain Text domain. Unique identifier for retrieving translated strings. |
1216 |
* @return bool Whether there are translations. |
|
1217 |
*/ |
|
1218 |
function is_textdomain_loaded( $domain ) { |
|
1219 |
global $l10n; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1220 |
return isset( $l10n[ $domain ] ); |
0 | 1221 |
} |
1222 |
||
1223 |
/** |
|
1224 |
* Translates role name. |
|
1225 |
* |
|
1226 |
* Since the role names are in the database and not in the source there |
|
1227 |
* are dummy gettext calls to get them into the POT file and this function |
|
1228 |
* properly translates them back. |
|
1229 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1230 |
* The before_last_bar() call is needed, because older installations keep the roles |
0 | 1231 |
* using the old context format: 'Role name|User role' and just skipping the |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1232 |
* content after the last bar is easier than fixing them in the DB. New installations |
0 | 1233 |
* won't suffer from that problem. |
1234 |
* |
|
1235 |
* @since 2.8.0 |
|
9 | 1236 |
* @since 5.2.0 Added the `$domain` parameter. |
0 | 1237 |
* |
9 | 1238 |
* @param string $name The role name. |
1239 |
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. |
|
1240 |
* Default 'default'. |
|
0 | 1241 |
* @return string Translated role name on success, original name on failure. |
1242 |
*/ |
|
9 | 1243 |
function translate_user_role( $name, $domain = 'default' ) { |
1244 |
return translate_with_gettext_context( before_last_bar( $name ), 'User role', $domain ); |
|
0 | 1245 |
} |
1246 |
||
1247 |
/** |
|
1248 |
* Get all available languages based on the presence of *.mo files in a given directory. |
|
1249 |
* |
|
1250 |
* The default directory is WP_LANG_DIR. |
|
1251 |
* |
|
1252 |
* @since 3.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1253 |
* @since 4.7.0 The results are now filterable with the {@see 'get_available_languages'} filter. |
0 | 1254 |
* |
1255 |
* @param string $dir A directory to search for language files. |
|
1256 |
* Default WP_LANG_DIR. |
|
1257 |
* @return array An array of language codes or an empty array if no languages are present. Language codes are formed by stripping the .mo extension from the language file names. |
|
1258 |
*/ |
|
1259 |
function get_available_languages( $dir = null ) { |
|
1260 |
$languages = array(); |
|
1261 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1262 |
$lang_files = glob( ( is_null( $dir ) ? WP_LANG_DIR : $dir ) . '/*.mo' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1263 |
if ( $lang_files ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1264 |
foreach ( $lang_files as $lang_file ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1265 |
$lang_file = basename( $lang_file, '.mo' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1266 |
if ( 0 !== strpos( $lang_file, 'continents-cities' ) && 0 !== strpos( $lang_file, 'ms-' ) && |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1267 |
0 !== strpos( $lang_file, 'admin-' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1268 |
$languages[] = $lang_file; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1269 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1270 |
} |
0 | 1271 |
} |
1272 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1273 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1274 |
* Filters the list of available language codes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1275 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1276 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1277 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1278 |
* @param array $languages An array of available language codes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1279 |
* @param string $dir The directory where the language files were found. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1280 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1281 |
return apply_filters( 'get_available_languages', $languages, $dir ); |
0 | 1282 |
} |
1283 |
||
1284 |
/** |
|
1285 |
* Get installed translations. |
|
1286 |
* |
|
1287 |
* Looks in the wp-content/languages directory for translations of |
|
1288 |
* plugins or themes. |
|
1289 |
* |
|
1290 |
* @since 3.7.0 |
|
1291 |
* |
|
1292 |
* @param string $type What to search for. Accepts 'plugins', 'themes', 'core'. |
|
1293 |
* @return array Array of language data. |
|
1294 |
*/ |
|
1295 |
function wp_get_installed_translations( $type ) { |
|
9 | 1296 |
if ( $type !== 'themes' && $type !== 'plugins' && $type !== 'core' ) { |
0 | 1297 |
return array(); |
9 | 1298 |
} |
0 | 1299 |
|
1300 |
$dir = 'core' === $type ? '' : "/$type"; |
|
1301 |
||
9 | 1302 |
if ( ! is_dir( WP_LANG_DIR ) ) { |
0 | 1303 |
return array(); |
9 | 1304 |
} |
0 | 1305 |
|
9 | 1306 |
if ( $dir && ! is_dir( WP_LANG_DIR . $dir ) ) { |
0 | 1307 |
return array(); |
9 | 1308 |
} |
0 | 1309 |
|
1310 |
$files = scandir( WP_LANG_DIR . $dir ); |
|
9 | 1311 |
if ( ! $files ) { |
0 | 1312 |
return array(); |
9 | 1313 |
} |
0 | 1314 |
|
1315 |
$language_data = array(); |
|
1316 |
||
1317 |
foreach ( $files as $file ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1318 |
if ( '.' === $file[0] || is_dir( WP_LANG_DIR . "$dir/$file" ) ) { |
5 | 1319 |
continue; |
1320 |
} |
|
1321 |
if ( substr( $file, -3 ) !== '.po' ) { |
|
0 | 1322 |
continue; |
5 | 1323 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1324 |
if ( ! preg_match( '/(?:(.+)-)?([a-z]{2,3}(?:_[A-Z]{2})?(?:_[a-z0-9]+)?).po/', $file, $match ) ) { |
0 | 1325 |
continue; |
5 | 1326 |
} |
9 | 1327 |
if ( ! in_array( substr( $file, 0, -3 ) . '.mo', $files ) ) { |
0 | 1328 |
continue; |
5 | 1329 |
} |
0 | 1330 |
|
1331 |
list( , $textdomain, $language ) = $match; |
|
5 | 1332 |
if ( '' === $textdomain ) { |
0 | 1333 |
$textdomain = 'default'; |
5 | 1334 |
} |
0 | 1335 |
$language_data[ $textdomain ][ $language ] = wp_get_pomo_file_data( WP_LANG_DIR . "$dir/$file" ); |
1336 |
} |
|
1337 |
return $language_data; |
|
1338 |
} |
|
1339 |
||
1340 |
/** |
|
1341 |
* Extract headers from a PO file. |
|
1342 |
* |
|
1343 |
* @since 3.7.0 |
|
1344 |
* |
|
1345 |
* @param string $po_file Path to PO file. |
|
1346 |
* @return array PO file headers. |
|
1347 |
*/ |
|
1348 |
function wp_get_pomo_file_data( $po_file ) { |
|
9 | 1349 |
$headers = get_file_data( |
1350 |
$po_file, |
|
1351 |
array( |
|
1352 |
'POT-Creation-Date' => '"POT-Creation-Date', |
|
1353 |
'PO-Revision-Date' => '"PO-Revision-Date', |
|
1354 |
'Project-Id-Version' => '"Project-Id-Version', |
|
1355 |
'X-Generator' => '"X-Generator', |
|
1356 |
) |
|
1357 |
); |
|
0 | 1358 |
foreach ( $headers as $header => $value ) { |
1359 |
// Remove possible contextual '\n' and closing double quote. |
|
1360 |
$headers[ $header ] = preg_replace( '~(\\\n)?"$~', '', $value ); |
|
1361 |
} |
|
1362 |
return $headers; |
|
1363 |
} |
|
5 | 1364 |
|
1365 |
/** |
|
1366 |
* Language selector. |
|
1367 |
* |
|
1368 |
* @since 4.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1369 |
* @since 4.3.0 Introduced the `echo` argument. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1370 |
* @since 4.7.0 Introduced the `show_option_site_default` argument. |
9 | 1371 |
* @since 5.1.0 Introduced the `show_option_en_us` argument. |
5 | 1372 |
* |
1373 |
* @see get_available_languages() |
|
1374 |
* @see wp_get_available_translations() |
|
1375 |
* |
|
1376 |
* @param string|array $args { |
|
1377 |
* Optional. Array or string of arguments for outputting the language selector. |
|
1378 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1379 |
* @type string $id ID attribute of the select element. Default 'locale'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1380 |
* @type string $name Name attribute of the select element. Default 'locale'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1381 |
* @type array $languages List of installed languages, contain only the locales. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1382 |
* Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1383 |
* @type array $translations List of available translations. Default result of |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1384 |
* wp_get_available_translations(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1385 |
* @type string $selected Language which should be selected. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1386 |
* @type bool|int $echo Whether to echo the generated markup. Accepts 0, 1, or their |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1387 |
* boolean equivalents. Default 1. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1388 |
* @type bool $show_available_translations Whether to show available translations. Default true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1389 |
* @type bool $show_option_site_default Whether to show an option to fall back to the site's locale. Default false. |
9 | 1390 |
* @type bool $show_option_en_us Whether to show an option for English (United States). Default true. |
5 | 1391 |
* } |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1392 |
* @return string HTML content |
5 | 1393 |
*/ |
1394 |
function wp_dropdown_languages( $args = array() ) { |
|
1395 |
||
9 | 1396 |
$parsed_args = wp_parse_args( |
1397 |
$args, |
|
1398 |
array( |
|
1399 |
'id' => 'locale', |
|
1400 |
'name' => 'locale', |
|
1401 |
'languages' => array(), |
|
1402 |
'translations' => array(), |
|
1403 |
'selected' => '', |
|
1404 |
'echo' => 1, |
|
1405 |
'show_available_translations' => true, |
|
1406 |
'show_option_site_default' => false, |
|
1407 |
'show_option_en_us' => true, |
|
1408 |
) |
|
1409 |
); |
|
5 | 1410 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1411 |
// Bail if no ID or no name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1412 |
if ( ! $parsed_args['id'] || ! $parsed_args['name'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1413 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1414 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1415 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1416 |
// English (United States) uses an empty string for the value attribute. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1417 |
if ( 'en_US' === $parsed_args['selected'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1418 |
$parsed_args['selected'] = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1419 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1420 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1421 |
$translations = $parsed_args['translations']; |
5 | 1422 |
if ( empty( $translations ) ) { |
1423 |
require_once( ABSPATH . 'wp-admin/includes/translation-install.php' ); |
|
1424 |
$translations = wp_get_available_translations(); |
|
1425 |
} |
|
1426 |
||
1427 |
/* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1428 |
* $parsed_args['languages'] should only contain the locales. Find the locale in |
5 | 1429 |
* $translations to get the native name. Fall back to locale. |
1430 |
*/ |
|
1431 |
$languages = array(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1432 |
foreach ( $parsed_args['languages'] as $locale ) { |
5 | 1433 |
if ( isset( $translations[ $locale ] ) ) { |
1434 |
$translation = $translations[ $locale ]; |
|
1435 |
$languages[] = array( |
|
1436 |
'language' => $translation['language'], |
|
1437 |
'native_name' => $translation['native_name'], |
|
1438 |
'lang' => current( $translation['iso'] ), |
|
1439 |
); |
|
1440 |
||
1441 |
// Remove installed language from available translations. |
|
1442 |
unset( $translations[ $locale ] ); |
|
1443 |
} else { |
|
1444 |
$languages[] = array( |
|
1445 |
'language' => $locale, |
|
1446 |
'native_name' => $locale, |
|
1447 |
'lang' => '', |
|
1448 |
); |
|
1449 |
} |
|
1450 |
} |
|
1451 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1452 |
$translations_available = ( ! empty( $translations ) && $parsed_args['show_available_translations'] ); |
5 | 1453 |
|
1454 |
// Holds the HTML markup. |
|
1455 |
$structure = array(); |
|
1456 |
||
1457 |
// List installed languages. |
|
1458 |
if ( $translations_available ) { |
|
1459 |
$structure[] = '<optgroup label="' . esc_attr_x( 'Installed', 'translations' ) . '">'; |
|
1460 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1461 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1462 |
// Site default. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1463 |
if ( $parsed_args['show_option_site_default'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1464 |
$structure[] = sprintf( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1465 |
'<option value="site-default" data-installed="1"%s>%s</option>', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1466 |
selected( 'site-default', $parsed_args['selected'], false ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1467 |
_x( 'Site Default', 'default site language' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1468 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1469 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1470 |
|
9 | 1471 |
if ( $parsed_args['show_option_en_us'] ) { |
1472 |
$structure[] = sprintf( |
|
1473 |
'<option value="" lang="en" data-installed="1"%s>English (United States)</option>', |
|
1474 |
selected( '', $parsed_args['selected'], false ) |
|
1475 |
); |
|
1476 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1477 |
|
9 | 1478 |
// List installed languages. |
5 | 1479 |
foreach ( $languages as $language ) { |
1480 |
$structure[] = sprintf( |
|
1481 |
'<option value="%s" lang="%s"%s data-installed="1">%s</option>', |
|
1482 |
esc_attr( $language['language'] ), |
|
1483 |
esc_attr( $language['lang'] ), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1484 |
selected( $language['language'], $parsed_args['selected'], false ), |
5 | 1485 |
esc_html( $language['native_name'] ) |
1486 |
); |
|
1487 |
} |
|
1488 |
if ( $translations_available ) { |
|
1489 |
$structure[] = '</optgroup>'; |
|
1490 |
} |
|
1491 |
||
1492 |
// List available translations. |
|
1493 |
if ( $translations_available ) { |
|
1494 |
$structure[] = '<optgroup label="' . esc_attr_x( 'Available', 'translations' ) . '">'; |
|
1495 |
foreach ( $translations as $translation ) { |
|
1496 |
$structure[] = sprintf( |
|
1497 |
'<option value="%s" lang="%s"%s>%s</option>', |
|
1498 |
esc_attr( $translation['language'] ), |
|
1499 |
esc_attr( current( $translation['iso'] ) ), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1500 |
selected( $translation['language'], $parsed_args['selected'], false ), |
5 | 1501 |
esc_html( $translation['native_name'] ) |
1502 |
); |
|
1503 |
} |
|
1504 |
$structure[] = '</optgroup>'; |
|
1505 |
} |
|
1506 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1507 |
// Combine the output string. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1508 |
$output = sprintf( '<select name="%s" id="%s">', esc_attr( $parsed_args['name'] ), esc_attr( $parsed_args['id'] ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1509 |
$output .= join( "\n", $structure ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1510 |
$output .= '</select>'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1511 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1512 |
if ( $parsed_args['echo'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1513 |
echo $output; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1514 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1515 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1516 |
return $output; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1517 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1518 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1519 |
/** |
9 | 1520 |
* Determines whether the current locale is right-to-left (RTL). |
1521 |
* |
|
1522 |
* For more information on this and similar theme functions, check out |
|
1523 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
1524 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1525 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1526 |
* @since 3.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1527 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1528 |
* @global WP_Locale $wp_locale |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1529 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1530 |
* @return bool Whether locale is RTL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1531 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1532 |
function is_rtl() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1533 |
global $wp_locale; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1534 |
if ( ! ( $wp_locale instanceof WP_Locale ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1535 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1536 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1537 |
return $wp_locale->is_rtl(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1538 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1539 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1540 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1541 |
* Switches the translations according to the given locale. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1542 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1543 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1544 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1545 |
* @global WP_Locale_Switcher $wp_locale_switcher |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1546 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1547 |
* @param string $locale The locale. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1548 |
* @return bool True on success, false on failure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1549 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1550 |
function switch_to_locale( $locale ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1551 |
/* @var WP_Locale_Switcher $wp_locale_switcher */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1552 |
global $wp_locale_switcher; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1553 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1554 |
return $wp_locale_switcher->switch_to_locale( $locale ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1555 |
} |
5 | 1556 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1557 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1558 |
* Restores the translations according to the previous locale. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1559 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1560 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1561 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1562 |
* @global WP_Locale_Switcher $wp_locale_switcher |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1563 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1564 |
* @return string|false Locale on success, false on error. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1565 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1566 |
function restore_previous_locale() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1567 |
/* @var WP_Locale_Switcher $wp_locale_switcher */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1568 |
global $wp_locale_switcher; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1569 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1570 |
return $wp_locale_switcher->restore_previous_locale(); |
5 | 1571 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1572 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1573 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1574 |
* Restores the translations according to the original locale. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1575 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1576 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1577 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1578 |
* @global WP_Locale_Switcher $wp_locale_switcher |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1579 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1580 |
* @return string|false Locale on success, false on error. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1581 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1582 |
function restore_current_locale() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1583 |
/* @var WP_Locale_Switcher $wp_locale_switcher */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1584 |
global $wp_locale_switcher; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1585 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1586 |
return $wp_locale_switcher->restore_current_locale(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1587 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1588 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1589 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1590 |
* Whether switch_to_locale() is in effect. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1591 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1592 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1593 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1594 |
* @global WP_Locale_Switcher $wp_locale_switcher |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1595 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1596 |
* @return bool True if the locale has been switched, false otherwise. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1597 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1598 |
function is_locale_switched() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1599 |
/* @var WP_Locale_Switcher $wp_locale_switcher */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1600 |
global $wp_locale_switcher; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1601 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1602 |
return $wp_locale_switcher->is_switched(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1603 |
} |