Aucune description

l10n.php 58KB

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