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