/wp-includes/l10n.php

  1. <?php 
  2. /** 
  3. * Core Translation API 
  4. * 
  5. * @package WordPress 
  6. * @subpackage i18n 
  7. * @since 1.2.0 
  8. */ 
  9.  
  10. /** 
  11. * Retrieves the current locale. 
  12. * 
  13. * If the locale is set, then it will filter the locale in the {@see 'locale'} 
  14. * filter hook and return the value. 
  15. * 
  16. * If the locale is not set already, then the WPLANG constant is used if it is 
  17. * defined. Then it is filtered through the {@see 'locale'} filter hook and 
  18. * the value for the locale global set and the locale is returned. 
  19. * 
  20. * The process to get the locale should only be done once, but the locale will 
  21. * always be filtered using the {@see 'locale'} hook. 
  22. * 
  23. * @since 1.5.0 
  24. * 
  25. * @global string $locale 
  26. * @global string $wp_local_package 
  27. * 
  28. * @return string The locale of the blog or from the {@see 'locale'} hook. 
  29. */ 
  30. function get_locale() { 
  31. global $locale, $wp_local_package; 
  32.  
  33. if ( isset( $locale ) ) { 
  34. /** 
  35. * Filter WordPress install's locale ID. 
  36. * 
  37. * @since 1.5.0 
  38. * 
  39. * @param string $locale The locale ID. 
  40. */ 
  41. return apply_filters( 'locale', $locale ); 
  42.  
  43. if ( isset( $wp_local_package ) ) { 
  44. $locale = $wp_local_package; 
  45.  
  46. // WPLANG was defined in wp-config. 
  47. if ( defined( 'WPLANG' ) ) { 
  48. $locale = WPLANG; 
  49.  
  50. // If multisite, check options. 
  51. if ( is_multisite() ) { 
  52. // Don't check blog option when installing. 
  53. if ( wp_installing() || ( false === $ms_locale = get_option( 'WPLANG' ) ) ) { 
  54. $ms_locale = get_site_option( 'WPLANG' ); 
  55.  
  56. if ( $ms_locale !== false ) { 
  57. $locale = $ms_locale; 
  58. } else { 
  59. $db_locale = get_option( 'WPLANG' ); 
  60. if ( $db_locale !== false ) { 
  61. $locale = $db_locale; 
  62.  
  63. if ( empty( $locale ) ) { 
  64. $locale = 'en_US'; 
  65.  
  66. /** This filter is documented in wp-includes/l10n.php */ 
  67. return apply_filters( 'locale', $locale ); 
  68.  
  69. /** 
  70. * Retrieve the translation of $text. 
  71. * 
  72. * If there is no translation, or the text domain isn't loaded, the original text is returned. 
  73. * 
  74. * *Note:* Don't use translate() directly, use __() or related functions. 
  75. * 
  76. * @since 2.2.0 
  77. * 
  78. * @param string $text Text to translate. 
  79. * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. 
  80. * Default 'default'. 
  81. * @return string Translated text 
  82. */ 
  83. function translate( $text, $domain = 'default' ) { 
  84. $translations = get_translations_for_domain( $domain ); 
  85. $translations = $translations->translate( $text ); 
  86.  
  87. /** 
  88. * Filter text with its translation. 
  89. * 
  90. * @since 2.0.11 
  91. * 
  92. * @param string $translations Translated text. 
  93. * @param string $text Text to translate. 
  94. * @param string $domain Text domain. Unique identifier for retrieving translated strings. 
  95. */ 
  96. return apply_filters( 'gettext', $translations, $text, $domain ); 
  97.  
  98. /** 
  99. * Remove last item on a pipe-delimited string. 
  100. * 
  101. * Meant for removing the last item in a string, such as 'Role name|User role'. The original 
  102. * string will be returned if no pipe '|' characters are found in the string. 
  103. * 
  104. * @since 2.8.0 
  105. * 
  106. * @param string $string A pipe-delimited string. 
  107. * @return string Either $string or everything before the last pipe. 
  108. */ 
  109. function before_last_bar( $string ) { 
  110. $last_bar = strrpos( $string, '|' ); 
  111. if ( false === $last_bar ) { 
  112. return $string; 
  113. } else { 
  114. return substr( $string, 0, $last_bar ); 
  115.  
  116. /** 
  117. * Retrieve the translation of $text in the context defined in $context. 
  118. * 
  119. * If there is no translation, or the text domain isn't loaded the original 
  120. * text is returned. 
  121. * 
  122. * *Note:* Don't use translate_with_gettext_context() directly, use _x() or related functions. 
  123. * 
  124. * @since 2.8.0 
  125. * 
  126. * @param string $text Text to translate. 
  127. * @param string $context Context information for the translators. 
  128. * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. 
  129. * Default 'default'. 
  130. * @return string Translated text on success, original text on failure. 
  131. */ 
  132. function translate_with_gettext_context( $text, $context, $domain = 'default' ) { 
  133. $translations = get_translations_for_domain( $domain ); 
  134. $translations = $translations->translate( $text, $context ); 
  135. /** 
  136. * Filter text with its translation based on context information. 
  137. * 
  138. * @since 2.8.0 
  139. * 
  140. * @param string $translations Translated text. 
  141. * @param string $text Text to translate. 
  142. * @param string $context Context information for the translators. 
  143. * @param string $domain Text domain. Unique identifier for retrieving translated strings. 
  144. */ 
  145. return apply_filters( 'gettext_with_context', $translations, $text, $context, $domain ); 
  146.  
  147. /** 
  148. * Retrieve the translation of $text. 
  149. * 
  150. * If there is no translation, or the text domain isn't loaded, the original text is returned. 
  151. * 
  152. * @since 2.1.0 
  153. * 
  154. * @param string $text Text to translate. 
  155. * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. 
  156. * Default 'default'. 
  157. * @return string Translated text. 
  158. */ 
  159. function __( $text, $domain = 'default' ) { 
  160. return translate( $text, $domain ); 
  161.  
  162. /** 
  163. * Retrieve the translation of $text and escapes it for safe use in an attribute. 
  164. * 
  165. * If there is no translation, or the text domain isn't loaded, the original text is returned. 
  166. * 
  167. * @since 2.8.0 
  168. * 
  169. * @param string $text Text to translate. 
  170. * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. 
  171. * Default 'default'. 
  172. * @return string Translated text on success, original text on failure. 
  173. */ 
  174. function esc_attr__( $text, $domain = 'default' ) { 
  175. return esc_attr( translate( $text, $domain ) ); 
  176.  
  177. /** 
  178. * Retrieve the translation of $text and escapes it for safe use in HTML output. 
  179. * 
  180. * If there is no translation, or the text domain isn't loaded, the original text is returned. 
  181. * 
  182. * @since 2.8.0 
  183. * 
  184. * @param string $text Text to translate. 
  185. * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. 
  186. * Default 'default'. 
  187. * @return string Translated text 
  188. */ 
  189. function esc_html__( $text, $domain = 'default' ) { 
  190. return esc_html( translate( $text, $domain ) ); 
  191.  
  192. /** 
  193. * Display translated text. 
  194. * 
  195. * @since 1.2.0 
  196. * 
  197. * @param string $text Text to translate. 
  198. * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. 
  199. * Default 'default'. 
  200. */ 
  201. function _e( $text, $domain = 'default' ) { 
  202. echo translate( $text, $domain ); 
  203.  
  204. /** 
  205. * Display translated text that has been escaped for safe use in an attribute. 
  206. * 
  207. * @since 2.8.0 
  208. * 
  209. * @param string $text Text to translate. 
  210. * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. 
  211. * Default 'default'. 
  212. */ 
  213. function esc_attr_e( $text, $domain = 'default' ) { 
  214. echo esc_attr( translate( $text, $domain ) ); 
  215.  
  216. /** 
  217. * Display translated text that has been escaped for safe use in HTML output. 
  218. * 
  219. * @since 2.8.0 
  220. * 
  221. * @param string $text Text to translate. 
  222. * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. 
  223. * Default 'default'. 
  224. */ 
  225. function esc_html_e( $text, $domain = 'default' ) { 
  226. echo esc_html( translate( $text, $domain ) ); 
  227.  
  228. /** 
  229. * Retrieve translated string with gettext context. 
  230. * 
  231. * Quite a few times, there will be collisions with similar translatable text 
  232. * found in more than two places, but with different translated context. 
  233. * 
  234. * By including the context in the pot file, translators can translate the two 
  235. * strings differently. 
  236. * 
  237. * @since 2.8.0 
  238. * 
  239. * @param string $text Text to translate. 
  240. * @param string $context Context information for the translators. 
  241. * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. 
  242. * Default 'default'. 
  243. * @return string Translated context string without pipe. 
  244. */ 
  245. function _x( $text, $context, $domain = 'default' ) { 
  246. return translate_with_gettext_context( $text, $context, $domain ); 
  247.  
  248. /** 
  249. * Display translated string with gettext context. 
  250. * 
  251. * @since 3.0.0 
  252. * 
  253. * @param string $text Text to translate. 
  254. * @param string $context Context information for the translators. 
  255. * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. 
  256. * Default 'default'. 
  257. * @return string Translated context string without pipe. 
  258. */ 
  259. function _ex( $text, $context, $domain = 'default' ) { 
  260. echo _x( $text, $context, $domain ); 
  261.  
  262. /** 
  263. * Translate string with gettext context, and escapes it for safe use in an attribute. 
  264. * 
  265. * @since 2.8.0 
  266. * 
  267. * @param string $text Text to translate. 
  268. * @param string $context Context information for the translators. 
  269. * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. 
  270. * Default 'default'. 
  271. * @return string Translated text 
  272. */ 
  273. function esc_attr_x( $text, $context, $domain = 'default' ) { 
  274. return esc_attr( translate_with_gettext_context( $text, $context, $domain ) ); 
  275.  
  276. /** 
  277. * Translate string with gettext context, and escapes it for safe use in HTML output. 
  278. * 
  279. * @since 2.9.0 
  280. * 
  281. * @param string $text Text to translate. 
  282. * @param string $context Context information for the translators. 
  283. * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. 
  284. * Default 'default'. 
  285. * @return string Translated text. 
  286. */ 
  287. function esc_html_x( $text, $context, $domain = 'default' ) { 
  288. return esc_html( translate_with_gettext_context( $text, $context, $domain ) ); 
  289.  
  290. /** 
  291. * Translates and retrieves the singular or plural form based on the supplied number. 
  292. * 
  293. * Used when you want to use the appropriate form of a string based on whether a 
  294. * number is singular or plural. 
  295. * 
  296. * Example: 
  297. * 
  298. * $people = sprintf( _n( '%s person', '%s people', $count, 'text-domain' ), number_format_i18n( $count ) ); 
  299. * 
  300. * @since 2.8.0 
  301. * 
  302. * @param string $single The text to be used if the number is singular. 
  303. * @param string $plural The text to be used if the number is plural. 
  304. * @param int $number The number to compare against to use either the singular or plural form. 
  305. * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. 
  306. * Default 'default'. 
  307. * @return string The translated singular or plural form. 
  308. */ 
  309. function _n( $single, $plural, $number, $domain = 'default' ) { 
  310. $translations = get_translations_for_domain( $domain ); 
  311. $translation = $translations->translate_plural( $single, $plural, $number ); 
  312.  
  313. /** 
  314. * Filter the singular or plural form of a string. 
  315. * 
  316. * @since 2.2.0 
  317. * 
  318. * @param string $translation Translated text. 
  319. * @param string $single The text to be used if the number is singular. 
  320. * @param string $plural The text to be used if the number is plural. 
  321. * @param string $number The number to compare against to use either the singular or plural form. 
  322. * @param string $domain Text domain. Unique identifier for retrieving translated strings. 
  323. */ 
  324. return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain ); 
  325.  
  326. /** 
  327. * Translates and retrieves the singular or plural form based on the supplied number, with gettext context. 
  328. * 
  329. * This is a hybrid of _n() and _x(). It supports context and plurals. 
  330. * 
  331. * Used when you want to use the appropriate form of a string with context based on whether a 
  332. * number is singular or plural. 
  333. * 
  334. * Example: 
  335. * 
  336. * $people = sprintf( _n( '%s person', '%s people', $count, 'context', 'text-domain' ), number_format_i18n( $count ) ); 
  337. * 
  338. * @since 2.8.0 
  339. * 
  340. * @param string $single The text to be used if the number is singular. 
  341. * @param string $plural The text to be used if the number is plural. 
  342. * @param int $number The number to compare against to use either the singular or plural form. 
  343. * @param string $context Context information for the translators. 
  344. * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. 
  345. * Default 'default'. 
  346. * @return string The translated singular or plural form. 
  347. */ 
  348. function _nx($single, $plural, $number, $context, $domain = 'default') { 
  349. $translations = get_translations_for_domain( $domain ); 
  350. $translation = $translations->translate_plural( $single, $plural, $number, $context ); 
  351.  
  352. /** 
  353. * Filter the singular or plural form of a string with gettext context. 
  354. * 
  355. * @since 2.8.0 
  356. * 
  357. * @param string $translation Translated text. 
  358. * @param string $single The text to be used if the number is singular. 
  359. * @param string $plural The text to be used if the number is plural. 
  360. * @param string $number The number to compare against to use either the singular or plural form. 
  361. * @param string $context Context information for the translators. 
  362. * @param string $domain Text domain. Unique identifier for retrieving translated strings. 
  363. */ 
  364. return apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain ); 
  365.  
  366. /** 
  367. * Registers plural strings in POT file, but does not translate them. 
  368. * 
  369. * Used when you want to keep structures with translatable plural 
  370. * strings and use them later when the number is known. 
  371. * 
  372. * Example: 
  373. * 
  374. * $messages = array( 
  375. * 'post' => _n_noop( '%s post', '%s posts', 'text-domain' ),  
  376. * 'page' => _n_noop( '%s pages', '%s pages', 'text-domain' ),  
  377. * ); 
  378. * ... 
  379. * $message = $messages[ $type ]; 
  380. * $usable_text = sprintf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) ); 
  381. * 
  382. * @since 2.5.0 
  383. * 
  384. * @param string $singular Singular form to be localized. 
  385. * @param string $plural Plural form to be localized. 
  386. * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. 
  387. * Default null. 
  388. * @return array { 
  389. * Array of translation information for the strings. 
  390. * 
  391. * @type string $0 Singular form to be localized. No longer used. 
  392. * @type string $1 Plural form to be localized. No longer used. 
  393. * @type string $singular Singular form to be localized. 
  394. * @type string $plural Plural form to be localized. 
  395. * @type null $context Context information for the translators. 
  396. * @type string $domain Text domain. 
  397. * } 
  398. */ 
  399. function _n_noop( $singular, $plural, $domain = null ) { 
  400. return array( 0 => $singular, 1 => $plural, 'singular' => $singular, 'plural' => $plural, 'context' => null, 'domain' => $domain ); 
  401.  
  402. /** 
  403. * Registers plural strings with gettext context in POT file, but does not translate them. 
  404. * 
  405. * Used when you want to keep structures with translatable plural 
  406. * strings and use them later when the number is known. 
  407. * 
  408. * Example: 
  409. * 
  410. * $messages = array( 
  411. * 'post' => _n_noop( '%s post', '%s posts', 'context', 'text-domain' ),  
  412. * 'page' => _n_noop( '%s pages', '%s pages', 'context', 'text-domain' ),  
  413. * ); 
  414. * ... 
  415. * $message = $messages[ $type ]; 
  416. * $usable_text = sprintf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) ); 
  417. * 
  418. * @since 2.8.0 
  419. * 
  420. * @param string $singular Singular form to be localized. 
  421. * @param string $plural Plural form to be localized. 
  422. * @param string $context Context information for the translators. 
  423. * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. 
  424. * Default null. 
  425. * @return array { 
  426. * Array of translation information for the strings. 
  427. * 
  428. * @type string $0 Singular form to be localized. No longer used. 
  429. * @type string $1 Plural form to be localized. No longer used. 
  430. * @type string $2 Context information for the translators. No longer used. 
  431. * @type string $singular Singular form to be localized. 
  432. * @type string $plural Plural form to be localized. 
  433. * @type string $context Context information for the translators. 
  434. * @type string $domain Text domain. 
  435. * } 
  436. */ 
  437. function _nx_noop( $singular, $plural, $context, $domain = null ) { 
  438. return array( 0 => $singular, 1 => $plural, 2 => $context, 'singular' => $singular, 'plural' => $plural, 'context' => $context, 'domain' => $domain ); 
  439.  
  440. /** 
  441. * Translates and retrieves the singular or plural form of a string that's been registered 
  442. * with _n_noop() or _nx_noop(). 
  443. * 
  444. * Used when you want to use a translatable plural string once the number is known. 
  445. * 
  446. * Example: 
  447. * 
  448. * $messages = array( 
  449. * 'post' => _n_noop( '%s post', '%s posts', 'text-domain' ),  
  450. * 'page' => _n_noop( '%s pages', '%s pages', 'text-domain' ),  
  451. * ); 
  452. * ... 
  453. * $message = $messages[ $type ]; 
  454. * $usable_text = sprintf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) ); 
  455. * 
  456. * @since 3.1.0 
  457. * 
  458. * @param array $nooped_plural Array with singular, plural, and context keys, usually the result of _n_noop() or _nx_noop(). 
  459. * @param int $count Number of objects. 
  460. * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. If $nooped_plural contains 
  461. * a text domain passed to _n_noop() or _nx_noop(), it will override this value. Default 'default'. 
  462. * @return string Either $single or $plural translated text. 
  463. */ 
  464. function translate_nooped_plural( $nooped_plural, $count, $domain = 'default' ) { 
  465. if ( $nooped_plural['domain'] ) 
  466. $domain = $nooped_plural['domain']; 
  467.  
  468. if ( $nooped_plural['context'] ) 
  469. return _nx( $nooped_plural['singular'], $nooped_plural['plural'], $count, $nooped_plural['context'], $domain ); 
  470. else 
  471. return _n( $nooped_plural['singular'], $nooped_plural['plural'], $count, $domain ); 
  472.  
  473. /** 
  474. * Load a .mo file into the text domain $domain. 
  475. * 
  476. * If the text domain already exists, the translations will be merged. If both 
  477. * sets have the same string, the translation from the original value will be taken. 
  478. * 
  479. * On success, the .mo file will be placed in the $l10n global by $domain 
  480. * and will be a MO object. 
  481. * 
  482. * @since 1.5.0 
  483. * 
  484. * @global array $l10n 
  485. * 
  486. * @param string $domain Text domain. Unique identifier for retrieving translated strings. 
  487. * @param string $mofile Path to the .mo file. 
  488. * @return bool True on success, false on failure. 
  489. */ 
  490. function load_textdomain( $domain, $mofile ) { 
  491. global $l10n; 
  492.  
  493. /** 
  494. * Filter text domain and/or MO file path for loading translations. 
  495. * 
  496. * @since 2.9.0 
  497. * 
  498. * @param bool $override Whether to override the text domain. Default false. 
  499. * @param string $domain Text domain. Unique identifier for retrieving translated strings. 
  500. * @param string $mofile Path to the MO file. 
  501. */ 
  502. $plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile ); 
  503.  
  504. if ( true == $plugin_override ) { 
  505. return true; 
  506.  
  507. /** 
  508. * Fires before the MO translation file is loaded. 
  509. * 
  510. * @since 2.9.0 
  511. * 
  512. * @param string $domain Text domain. Unique identifier for retrieving translated strings. 
  513. * @param string $mofile Path to the .mo file. 
  514. */ 
  515. do_action( 'load_textdomain', $domain, $mofile ); 
  516.  
  517. /** 
  518. * Filter MO file path for loading translations for a specific text domain. 
  519. * 
  520. * @since 2.9.0 
  521. * 
  522. * @param string $mofile Path to the MO file. 
  523. * @param string $domain Text domain. Unique identifier for retrieving translated strings. 
  524. */ 
  525. $mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain ); 
  526.  
  527. if ( !is_readable( $mofile ) ) return false; 
  528.  
  529. $mo = new MO(); 
  530. if ( !$mo->import_from_file( $mofile ) ) return false; 
  531.  
  532. if ( isset( $l10n[$domain] ) ) 
  533. $mo->merge_with( $l10n[$domain] ); 
  534.  
  535. $l10n[$domain] = &$mo; 
  536.  
  537. return true; 
  538.  
  539. /** 
  540. * Unload translations for a text domain. 
  541. * 
  542. * @since 3.0.0 
  543. * 
  544. * @global array $l10n 
  545. * 
  546. * @param string $domain Text domain. Unique identifier for retrieving translated strings. 
  547. * @return bool Whether textdomain was unloaded. 
  548. */ 
  549. function unload_textdomain( $domain ) { 
  550. global $l10n; 
  551.  
  552. /** 
  553. * Filter the text domain for loading translation. 
  554. * 
  555. * @since 3.0.0 
  556. * 
  557. * @param bool $override Whether to override unloading the text domain. Default false. 
  558. * @param string $domain Text domain. Unique identifier for retrieving translated strings. 
  559. */ 
  560. $plugin_override = apply_filters( 'override_unload_textdomain', false, $domain ); 
  561.  
  562. if ( $plugin_override ) 
  563. return true; 
  564.  
  565. /** 
  566. * Fires before the text domain is unloaded. 
  567. * 
  568. * @since 3.0.0 
  569. * 
  570. * @param string $domain Text domain. Unique identifier for retrieving translated strings. 
  571. */ 
  572. do_action( 'unload_textdomain', $domain ); 
  573.  
  574. if ( isset( $l10n[$domain] ) ) { 
  575. unset( $l10n[$domain] ); 
  576. return true; 
  577.  
  578. return false; 
  579.  
  580. /** 
  581. * Load default translated strings based on locale. 
  582. * 
  583. * Loads the .mo file in WP_LANG_DIR constant path from WordPress root. 
  584. * The translated (.mo) file is named based on the locale. 
  585. * 
  586. * @see load_textdomain() 
  587. * 
  588. * @since 1.5.0 
  589. * 
  590. * @param string $locale Optional. Locale to load. Default is the value of {@see get_locale()}. 
  591. * @return bool Whether the textdomain was loaded. 
  592. */ 
  593. function load_default_textdomain( $locale = null ) { 
  594. if ( null === $locale ) { 
  595. $locale = get_locale(); 
  596.  
  597. // Unload previously loaded strings so we can switch translations. 
  598. unload_textdomain( 'default' ); 
  599.  
  600. $return = load_textdomain( 'default', WP_LANG_DIR . "/$locale.mo" ); 
  601.  
  602. if ( ( is_multisite() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) && ! file_exists( WP_LANG_DIR . "/admin-$locale.mo" ) ) { 
  603. load_textdomain( 'default', WP_LANG_DIR . "/ms-$locale.mo" ); 
  604. return $return; 
  605.  
  606. if ( is_admin() || wp_installing() || ( defined( 'WP_REPAIRING' ) && WP_REPAIRING ) ) { 
  607. load_textdomain( 'default', WP_LANG_DIR . "/admin-$locale.mo" ); 
  608.  
  609. if ( is_network_admin() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) 
  610. load_textdomain( 'default', WP_LANG_DIR . "/admin-network-$locale.mo" ); 
  611.  
  612. return $return; 
  613.  
  614. /** 
  615. * Load a plugin's translated strings. 
  616. * 
  617. * If the path is not given then it will be the root of the plugin directory. 
  618. * 
  619. * The .mo file should be named based on the text domain with a dash, and then the locale exactly. 
  620. * 
  621. * @since 1.5.0 
  622. * 
  623. * @param string $domain Unique identifier for retrieving translated strings 
  624. * @param string $deprecated Use the $plugin_rel_path parameter instead. 
  625. * @param string $plugin_rel_path Optional. Relative path to WP_PLUGIN_DIR where the .mo file resides. 
  626. * Default false. 
  627. * @return bool True when textdomain is successfully loaded, false otherwise. 
  628. */ 
  629. function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path = false ) { 
  630. $locale = get_locale(); 
  631. /** 
  632. * Filter a plugin's locale. 
  633. * 
  634. * @since 3.0.0 
  635. * 
  636. * @param string $locale The plugin's current locale. 
  637. * @param string $domain Text domain. Unique identifier for retrieving translated strings. 
  638. */ 
  639. $locale = apply_filters( 'plugin_locale', $locale, $domain ); 
  640.  
  641. if ( false !== $plugin_rel_path ) { 
  642. $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' ); 
  643. } elseif ( false !== $deprecated ) { 
  644. _deprecated_argument( __FUNCTION__, '2.7' ); 
  645. $path = ABSPATH . trim( $deprecated, '/' ); 
  646. } else { 
  647. $path = WP_PLUGIN_DIR; 
  648.  
  649. // Load the textdomain according to the plugin first 
  650. $mofile = $domain . '-' . $locale . '.mo'; 
  651. if ( $loaded = load_textdomain( $domain, $path . '/'. $mofile ) ) 
  652. return $loaded; 
  653.  
  654. // Otherwise, load from the languages directory 
  655. $mofile = WP_LANG_DIR . '/plugins/' . $mofile; 
  656. return load_textdomain( $domain, $mofile ); 
  657.  
  658. /** 
  659. * Load the translated strings for a plugin residing in the mu-plugins directory. 
  660. * 
  661. * @since 3.0.0 
  662. * 
  663. * @param string $domain Text domain. Unique identifier for retrieving translated strings. 
  664. * @param string $mu_plugin_rel_path Relative to WPMU_PLUGIN_DIR directory in which the .mo file resides. 
  665. * Default empty string. 
  666. * @return bool True when textdomain is successfully loaded, false otherwise. 
  667. */ 
  668. function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) { 
  669. /** This filter is documented in wp-includes/l10n.php */ 
  670. $locale = apply_filters( 'plugin_locale', get_locale(), $domain ); 
  671. $path = trailingslashit( WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' ) ); 
  672.  
  673. // Load the textdomain according to the plugin first 
  674. $mofile = $domain . '-' . $locale . '.mo'; 
  675. if ( $loaded = load_textdomain( $domain, $path . $mofile ) ) 
  676. return $loaded; 
  677.  
  678. // Otherwise, load from the languages directory 
  679. $mofile = WP_LANG_DIR . '/plugins/' . $mofile; 
  680. return load_textdomain( $domain, $mofile ); 
  681.  
  682. /** 
  683. * Load the theme's translated strings. 
  684. * 
  685. * If the current locale exists as a .mo file in the theme's root directory, it 
  686. * will be included in the translated strings by the $domain. 
  687. * 
  688. * The .mo files must be named based on the locale exactly. 
  689. * 
  690. * @since 1.5.0 
  691. * 
  692. * @param string $domain Text domain. Unique identifier for retrieving translated strings. 
  693. * @param string $path Optional. Path to the directory containing the .mo file. 
  694. * Default false. 
  695. * @return bool True when textdomain is successfully loaded, false otherwise. 
  696. */ 
  697. function load_theme_textdomain( $domain, $path = false ) { 
  698. $locale = get_locale(); 
  699. /** 
  700. * Filter a theme's locale. 
  701. * 
  702. * @since 3.0.0 
  703. * 
  704. * @param string $locale The theme's current locale. 
  705. * @param string $domain Text domain. Unique identifier for retrieving translated strings. 
  706. */ 
  707. $locale = apply_filters( 'theme_locale', $locale, $domain ); 
  708.  
  709. if ( ! $path ) 
  710. $path = get_template_directory(); 
  711.  
  712. // Load the textdomain according to the theme 
  713. $mofile = untrailingslashit( $path ) . "/{$locale}.mo"; 
  714. if ( $loaded = load_textdomain( $domain, $mofile ) ) 
  715. return $loaded; 
  716.  
  717. // Otherwise, load from the languages directory 
  718. $mofile = WP_LANG_DIR . "/themes/{$domain}-{$locale}.mo"; 
  719. return load_textdomain( $domain, $mofile ); 
  720.  
  721. /** 
  722. * Load the child themes translated strings. 
  723. * 
  724. * If the current locale exists as a .mo file in the child themes 
  725. * root directory, it will be included in the translated strings by the $domain. 
  726. * 
  727. * The .mo files must be named based on the locale exactly. 
  728. * 
  729. * @since 2.9.0 
  730. * 
  731. * @param string $domain Text domain. Unique identifier for retrieving translated strings. 
  732. * @param string $path Optional. Path to the directory containing the .mo file. 
  733. * Default false. 
  734. * @return bool True when the theme textdomain is successfully loaded, false otherwise. 
  735. */ 
  736. function load_child_theme_textdomain( $domain, $path = false ) { 
  737. if ( ! $path ) 
  738. $path = get_stylesheet_directory(); 
  739. return load_theme_textdomain( $domain, $path ); 
  740.  
  741. /** 
  742. * Return the Translations instance for a text domain. 
  743. * 
  744. * If there isn't one, returns empty Translations instance. 
  745. * 
  746. * @since 2.8.0 
  747. * 
  748. * @global array $l10n 
  749. * 
  750. * @param string $domain Text domain. Unique identifier for retrieving translated strings. 
  751. * @return NOOP_Translations A Translations instance. 
  752. */ 
  753. function get_translations_for_domain( $domain ) { 
  754. global $l10n; 
  755. if ( isset( $l10n[ $domain ] ) ) { 
  756. return $l10n[ $domain ]; 
  757.  
  758. static $noop_translations = null; 
  759. if ( null === $noop_translations ) { 
  760. $noop_translations = new NOOP_Translations; 
  761.  
  762. return $noop_translations; 
  763.  
  764. /** 
  765. * Whether there are translations for the text domain. 
  766. * 
  767. * @since 3.0.0 
  768. * 
  769. * @global array $l10n 
  770. * 
  771. * @param string $domain Text domain. Unique identifier for retrieving translated strings. 
  772. * @return bool Whether there are translations. 
  773. */ 
  774. function is_textdomain_loaded( $domain ) { 
  775. global $l10n; 
  776. return isset( $l10n[ $domain ] ); 
  777.  
  778. /** 
  779. * Translates role name. 
  780. * 
  781. * Since the role names are in the database and not in the source there 
  782. * are dummy gettext calls to get them into the POT file and this function 
  783. * properly translates them back. 
  784. * 
  785. * The before_last_bar() call is needed, because older installs keep the roles 
  786. * using the old context format: 'Role name|User role' and just skipping the 
  787. * content after the last bar is easier than fixing them in the DB. New installs 
  788. * won't suffer from that problem. 
  789. * 
  790. * @since 2.8.0 
  791. * 
  792. * @param string $name The role name. 
  793. * @return string Translated role name on success, original name on failure. 
  794. */ 
  795. function translate_user_role( $name ) { 
  796. return translate_with_gettext_context( before_last_bar($name), 'User role' ); 
  797.  
  798. /** 
  799. * Get all available languages based on the presence of *.mo files in a given directory. 
  800. * 
  801. * The default directory is WP_LANG_DIR. 
  802. * 
  803. * @since 3.0.0 
  804. * 
  805. * @param string $dir A directory to search for language files. 
  806. * Default WP_LANG_DIR. 
  807. * @return array An array of language codes or an empty array if no languages are present. Language codes are formed by stripping the .mo extension from the language file names. 
  808. */ 
  809. function get_available_languages( $dir = null ) { 
  810. $languages = array(); 
  811.  
  812. $lang_files = glob( ( is_null( $dir) ? WP_LANG_DIR : $dir ) . '/*.mo' ); 
  813. if ( $lang_files ) { 
  814. foreach ( $lang_files as $lang_file ) { 
  815. $lang_file = basename( $lang_file, '.mo' ); 
  816. if ( 0 !== strpos( $lang_file, 'continents-cities' ) && 0 !== strpos( $lang_file, 'ms-' ) && 
  817. 0 !== strpos( $lang_file, 'admin-' ) ) { 
  818. $languages[] = $lang_file; 
  819. } 
  820. } 
  821. } 
  822.   
  823. return $languages; 
  824. } 
  825.   
  826. /** 
  827. * Get installed translations. 
  828. * 
  829. * Looks in the wp-content/languages directory for translations of 
  830. * plugins or themes. 
  831. * 
  832. * @since 3.7.0 
  833. * 
  834. * @param string $type What to search for. Accepts 'plugins', 'themes', 'core'. 
  835. * @return array Array of language data. 
  836. */ 
  837. function wp_get_installed_translations( $type ) { 
  838. if ( $type !== 'themes' && $type !== 'plugins' && $type !== 'core' ) 
  839. return array(); 
  840.  
  841. $dir = 'core' === $type ? '' : "/$type"; 
  842.  
  843. if ( ! is_dir( WP_LANG_DIR ) ) 
  844. return array(); 
  845.  
  846. if ( $dir && ! is_dir( WP_LANG_DIR . $dir ) ) 
  847. return array(); 
  848.  
  849. $files = scandir( WP_LANG_DIR . $dir ); 
  850. if ( ! $files ) 
  851. return array(); 
  852.  
  853. $language_data = array(); 
  854.  
  855. foreach ( $files as $file ) { 
  856. if ( '.' === $file[0] || is_dir( WP_LANG_DIR . "$dir/$file" ) ) { 
  857. continue; 
  858. if ( substr( $file, -3 ) !== '.po' ) { 
  859. continue; 
  860. if ( ! preg_match( '/(?:(.+)-)?([a-z]{2, 3}(?:_[A-Z]{2})?(?:_[a-z0-9]+)?).po/', $file, $match ) ) { 
  861. continue; 
  862. if ( ! in_array( substr( $file, 0, -3 ) . '.mo', $files ) ) { 
  863. continue; 
  864.  
  865. list( , $textdomain, $language ) = $match; 
  866. if ( '' === $textdomain ) { 
  867. $textdomain = 'default'; 
  868. $language_data[ $textdomain ][ $language ] = wp_get_pomo_file_data( WP_LANG_DIR . "$dir/$file" ); 
  869. return $language_data; 
  870.  
  871. /** 
  872. * Extract headers from a PO file. 
  873. * 
  874. * @since 3.7.0 
  875. * 
  876. * @param string $po_file Path to PO file. 
  877. * @return array PO file headers. 
  878. */ 
  879. function wp_get_pomo_file_data( $po_file ) { 
  880. $headers = get_file_data( $po_file, array( 
  881. 'POT-Creation-Date' => '"POT-Creation-Date',  
  882. 'PO-Revision-Date' => '"PO-Revision-Date',  
  883. 'Project-Id-Version' => '"Project-Id-Version',  
  884. 'X-Generator' => '"X-Generator',  
  885. ) ); 
  886. foreach ( $headers as $header => $value ) { 
  887. // Remove possible contextual '\n' and closing double quote. 
  888. $headers[ $header ] = preg_replace( '~(\\\n)?"$~', '', $value ); 
  889. return $headers; 
  890.  
  891. /** 
  892. * Language selector. 
  893. * 
  894. * @since 4.0.0 
  895. * @since 4.3.0 Introduced the `echo` argument. 
  896. * 
  897. * @see get_available_languages() 
  898. * @see wp_get_available_translations() 
  899. * 
  900. * @param string|array $args { 
  901. * Optional. Array or string of arguments for outputting the language selector. 
  902. * 
  903. * @type string $id ID attribute of the select element. Default empty. 
  904. * @type string $name Name attribute of the select element. Default empty. 
  905. * @type array $languages List of installed languages, contain only the locales. 
  906. * Default empty array. 
  907. * @type array $translations List of available translations. Default result of 
  908. * wp_get_available_translations(). 
  909. * @type string $selected Language which should be selected. Default empty. 
  910. * @type bool|int $echo Whether to echo the generated markup. Accepts 0, 1, or their 
  911. * boolean equivalents. Default 1. 
  912. * @type bool $show_available_translations Whether to show available translations. Default true. 
  913. * } 
  914. * @return string HTML content 
  915. */ 
  916. function wp_dropdown_languages( $args = array() ) { 
  917.  
  918. $args = wp_parse_args( $args, array( 
  919. 'id' => '',  
  920. 'name' => '',  
  921. 'languages' => array(),  
  922. 'translations' => array(),  
  923. 'selected' => '',  
  924. 'echo' => 1,  
  925. 'show_available_translations' => true,  
  926. ) ); 
  927.  
  928. $translations = $args['translations']; 
  929. if ( empty( $translations ) ) { 
  930. require_once( ABSPATH . 'wp-admin/includes/translation-install.php' ); 
  931. $translations = wp_get_available_translations(); 
  932.  
  933. /** 
  934. * $args['languages'] should only contain the locales. Find the locale in 
  935. * $translations to get the native name. Fall back to locale. 
  936. */ 
  937. $languages = array(); 
  938. foreach ( $args['languages'] as $locale ) { 
  939. if ( isset( $translations[ $locale ] ) ) { 
  940. $translation = $translations[ $locale ]; 
  941. $languages[] = array( 
  942. 'language' => $translation['language'],  
  943. 'native_name' => $translation['native_name'],  
  944. 'lang' => current( $translation['iso'] ),  
  945. ); 
  946.  
  947. // Remove installed language from available translations. 
  948. unset( $translations[ $locale ] ); 
  949. } else { 
  950. $languages[] = array( 
  951. 'language' => $locale,  
  952. 'native_name' => $locale,  
  953. 'lang' => '',  
  954. ); 
  955.  
  956. $translations_available = ( ! empty( $translations ) && $args['show_available_translations'] ); 
  957.  
  958. $output = sprintf( '<select name="%s" id="%s">', esc_attr( $args['name'] ), esc_attr( $args['id'] ) ); 
  959.  
  960. // Holds the HTML markup. 
  961. $structure = array(); 
  962.  
  963. // List installed languages. 
  964. if ( $translations_available ) { 
  965. $structure[] = '<optgroup label="' . esc_attr_x( 'Installed', 'translations' ) . '">'; 
  966. $structure[] = '<option value="" lang="en" data-installed="1">English (United States)</option>'; 
  967. foreach ( $languages as $language ) { 
  968. $structure[] = sprintf( 
  969. '<option value="%s" lang="%s"%s data-installed="1">%s</option>',  
  970. esc_attr( $language['language'] ),  
  971. esc_attr( $language['lang'] ),  
  972. selected( $language['language'], $args['selected'], false ),  
  973. esc_html( $language['native_name'] ) 
  974. ); 
  975. if ( $translations_available ) { 
  976. $structure[] = '</optgroup>'; 
  977.  
  978. // List available translations. 
  979. if ( $translations_available ) { 
  980. $structure[] = '<optgroup label="' . esc_attr_x( 'Available', 'translations' ) . '">'; 
  981. foreach ( $translations as $translation ) { 
  982. $structure[] = sprintf( 
  983. '<option value="%s" lang="%s"%s>%s</option>',  
  984. esc_attr( $translation['language'] ),  
  985. esc_attr( current( $translation['iso'] ) ),  
  986. selected( $translation['language'], $args['selected'], false ),  
  987. esc_html( $translation['native_name'] ) 
  988. ); 
  989. $structure[] = '</optgroup>'; 
  990.  
  991. $output .= join( "\n", $structure ); 
  992.  
  993. $output .= '</select>'; 
  994.  
  995. if ( $args['echo'] ) { 
  996. echo $output; 
  997.  
  998. return $output; 
.