Nenhuma Descrição

l10n.php 55KB

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