l10n.php 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  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 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 = 0 ) {
  86. $user_object = false;
  87. if ( 0 === $user && function_exists( 'wp_get_current_user' ) ) {
  88. $user_object = wp_get_current_user();
  89. } elseif ( $user instanceof WP_User ) {
  90. $user_object = $user;
  91. } elseif ( $user && is_numeric( $user ) ) {
  92. $user_object = get_user_by( 'id', $user );
  93. }
  94. if ( ! $user_object ) {
  95. return get_locale();
  96. }
  97. $locale = $user_object->locale;
  98. return $locale ? $locale : get_locale();
  99. }
  100. /**
  101. * Determines 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. * Retrieves 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. * Removes 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. * Retrieves 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. * Retrieves 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. * Retrieves 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. * Retrieves 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. * Displays 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. * Displays 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. * Displays 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. * Retrieves 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. * Displays 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. * Translates 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. * Translates 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 int $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 int $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 int $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 int $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|null $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 returns 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 {
  621. * Array that is usually a return value from _n_noop() or _nx_noop().
  622. *
  623. * @type string $singular Singular form to be localized.
  624. * @type string $plural Plural form to be localized.
  625. * @type string|null $context Context information for the translators.
  626. * @type string|null $domain Text domain.
  627. * }
  628. * @param int $count Number of objects.
  629. * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. If $nooped_plural contains
  630. * a text domain passed to _n_noop() or _nx_noop(), it will override this value. Default 'default'.
  631. * @return string Either $singular or $plural translated text.
  632. */
  633. function translate_nooped_plural( $nooped_plural, $count, $domain = 'default' ) {
  634. if ( $nooped_plural['domain'] ) {
  635. $domain = $nooped_plural['domain'];
  636. }
  637. if ( $nooped_plural['context'] ) {
  638. return _nx( $nooped_plural['singular'], $nooped_plural['plural'], $count, $nooped_plural['context'], $domain );
  639. } else {
  640. return _n( $nooped_plural['singular'], $nooped_plural['plural'], $count, $domain );
  641. }
  642. }
  643. /**
  644. * Loads a .mo file into the text domain $domain.
  645. *
  646. * If the text domain already exists, the translations will be merged. If both
  647. * sets have the same string, the translation from the original value will be taken.
  648. *
  649. * On success, the .mo file will be placed in the $l10n global by $domain
  650. * and will be a MO object.
  651. *
  652. * @since 1.5.0
  653. * @since 6.1.0 Added the `$locale` parameter.
  654. *
  655. * @global MO[] $l10n An array of all currently loaded text domains.
  656. * @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again.
  657. * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
  658. *
  659. * @param string $domain Text domain. Unique identifier for retrieving translated strings.
  660. * @param string $mofile Path to the .mo file.
  661. * @param string $locale Optional. Locale. Default is the current locale.
  662. * @return bool True on success, false on failure.
  663. */
  664. function load_textdomain( $domain, $mofile, $locale = null ) {
  665. /** @var WP_Textdomain_Registry $wp_textdomain_registry */
  666. global $l10n, $l10n_unloaded, $wp_textdomain_registry;
  667. $l10n_unloaded = (array) $l10n_unloaded;
  668. /**
  669. * Filters whether to override the .mo file loading.
  670. *
  671. * @since 2.9.0
  672. *
  673. * @param bool $override Whether to override the .mo file loading. Default false.
  674. * @param string $domain Text domain. Unique identifier for retrieving translated strings.
  675. * @param string $mofile Path to the MO file.
  676. */
  677. $plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile );
  678. if ( true === (bool) $plugin_override ) {
  679. unset( $l10n_unloaded[ $domain ] );
  680. return true;
  681. }
  682. /**
  683. * Fires before the MO translation file is loaded.
  684. *
  685. * @since 2.9.0
  686. *
  687. * @param string $domain Text domain. Unique identifier for retrieving translated strings.
  688. * @param string $mofile Path to the .mo file.
  689. */
  690. do_action( 'load_textdomain', $domain, $mofile );
  691. /**
  692. * Filters MO file path for loading translations for a specific text domain.
  693. *
  694. * @since 2.9.0
  695. *
  696. * @param string $mofile Path to the MO file.
  697. * @param string $domain Text domain. Unique identifier for retrieving translated strings.
  698. */
  699. $mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain );
  700. if ( ! is_readable( $mofile ) ) {
  701. return false;
  702. }
  703. if ( ! $locale ) {
  704. $locale = determine_locale();
  705. }
  706. $mo = new MO();
  707. if ( ! $mo->import_from_file( $mofile ) ) {
  708. $wp_textdomain_registry->set( $domain, $locale, false );
  709. return false;
  710. }
  711. if ( isset( $l10n[ $domain ] ) ) {
  712. $mo->merge_with( $l10n[ $domain ] );
  713. }
  714. unset( $l10n_unloaded[ $domain ] );
  715. $l10n[ $domain ] = &$mo;
  716. $wp_textdomain_registry->set( $domain, $locale, dirname( $mofile ) );
  717. return true;
  718. }
  719. /**
  720. * Unloads translations for a text domain.
  721. *
  722. * @since 3.0.0
  723. * @since 6.1.0 Added the `$reloadable` parameter.
  724. *
  725. * @global MO[] $l10n An array of all currently loaded text domains.
  726. * @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again.
  727. *
  728. * @param string $domain Text domain. Unique identifier for retrieving translated strings.
  729. * @param bool $reloadable Whether the text domain can be loaded just-in-time again.
  730. * @return bool Whether textdomain was unloaded.
  731. */
  732. function unload_textdomain( $domain, $reloadable = false ) {
  733. global $l10n, $l10n_unloaded;
  734. $l10n_unloaded = (array) $l10n_unloaded;
  735. /**
  736. * Filters whether to override the text domain unloading.
  737. *
  738. * @since 3.0.0
  739. * @since 6.1.0 Added the `$reloadable` parameter.
  740. *
  741. * @param bool $override Whether to override the text domain unloading. Default false.
  742. * @param string $domain Text domain. Unique identifier for retrieving translated strings.
  743. * @param bool $reloadable Whether the text domain can be loaded just-in-time again.
  744. */
  745. $plugin_override = apply_filters( 'override_unload_textdomain', false, $domain, $reloadable );
  746. if ( $plugin_override ) {
  747. if ( ! $reloadable ) {
  748. $l10n_unloaded[ $domain ] = true;
  749. }
  750. return true;
  751. }
  752. /**
  753. * Fires before the text domain is unloaded.
  754. *
  755. * @since 3.0.0
  756. * @since 6.1.0 Added the `$reloadable` parameter.
  757. *
  758. * @param string $domain Text domain. Unique identifier for retrieving translated strings.
  759. * @param bool $reloadable Whether the text domain can be loaded just-in-time again.
  760. */
  761. do_action( 'unload_textdomain', $domain, $reloadable );
  762. if ( isset( $l10n[ $domain ] ) ) {
  763. unset( $l10n[ $domain ] );
  764. if ( ! $reloadable ) {
  765. $l10n_unloaded[ $domain ] = true;
  766. }
  767. return true;
  768. }
  769. return false;
  770. }
  771. /**
  772. * Loads default translated strings based on locale.
  773. *
  774. * Loads the .mo file in WP_LANG_DIR constant path from WordPress root.
  775. * The translated (.mo) file is named based on the locale.
  776. *
  777. * @see load_textdomain()
  778. *
  779. * @since 1.5.0
  780. *
  781. * @param string $locale Optional. Locale to load. Default is the value of get_locale().
  782. * @return bool Whether the textdomain was loaded.
  783. */
  784. function load_default_textdomain( $locale = null ) {
  785. if ( null === $locale ) {
  786. $locale = determine_locale();
  787. }
  788. // Unload previously loaded strings so we can switch translations.
  789. unload_textdomain( 'default' );
  790. $return = load_textdomain( 'default', WP_LANG_DIR . "/$locale.mo", $locale );
  791. if ( ( is_multisite() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) && ! file_exists( WP_LANG_DIR . "/admin-$locale.mo" ) ) {
  792. load_textdomain( 'default', WP_LANG_DIR . "/ms-$locale.mo", $locale );
  793. return $return;
  794. }
  795. if ( is_admin() || wp_installing() || ( defined( 'WP_REPAIRING' ) && WP_REPAIRING ) ) {
  796. load_textdomain( 'default', WP_LANG_DIR . "/admin-$locale.mo", $locale );
  797. }
  798. if ( is_network_admin() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) {
  799. load_textdomain( 'default', WP_LANG_DIR . "/admin-network-$locale.mo", $locale );
  800. }
  801. return $return;
  802. }
  803. /**
  804. * Loads a plugin's translated strings.
  805. *
  806. * If the path is not given then it will be the root of the plugin directory.
  807. *
  808. * The .mo file should be named based on the text domain with a dash, and then the locale exactly.
  809. *
  810. * @since 1.5.0
  811. * @since 4.6.0 The function now tries to load the .mo file from the languages directory first.
  812. *
  813. * @param string $domain Unique identifier for retrieving translated strings
  814. * @param string|false $deprecated Optional. Deprecated. Use the $plugin_rel_path parameter instead.
  815. * Default false.
  816. * @param string|false $plugin_rel_path Optional. Relative path to WP_PLUGIN_DIR where the .mo file resides.
  817. * Default false.
  818. * @return bool True when textdomain is successfully loaded, false otherwise.
  819. */
  820. function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path = false ) {
  821. /** @var WP_Textdomain_Registry $wp_textdomain_registry */
  822. global $wp_textdomain_registry;
  823. /**
  824. * Filters a plugin's locale.
  825. *
  826. * @since 3.0.0
  827. *
  828. * @param string $locale The plugin's current locale.
  829. * @param string $domain Text domain. Unique identifier for retrieving translated strings.
  830. */
  831. $locale = apply_filters( 'plugin_locale', determine_locale(), $domain );
  832. $mofile = $domain . '-' . $locale . '.mo';
  833. // Try to load from the languages directory first.
  834. if ( load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile, $locale ) ) {
  835. return true;
  836. }
  837. if ( false !== $plugin_rel_path ) {
  838. $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' );
  839. } elseif ( false !== $deprecated ) {
  840. _deprecated_argument( __FUNCTION__, '2.7.0' );
  841. $path = ABSPATH . trim( $deprecated, '/' );
  842. } else {
  843. $path = WP_PLUGIN_DIR;
  844. }
  845. $wp_textdomain_registry->set_custom_path( $domain, $path );
  846. return load_textdomain( $domain, $path . '/' . $mofile, $locale );
  847. }
  848. /**
  849. * Loads the translated strings for a plugin residing in the mu-plugins directory.
  850. *
  851. * @since 3.0.0
  852. * @since 4.6.0 The function now tries to load the .mo file from the languages directory first.
  853. *
  854. * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
  855. *
  856. * @param string $domain Text domain. Unique identifier for retrieving translated strings.
  857. * @param string $mu_plugin_rel_path Optional. Relative to `WPMU_PLUGIN_DIR` directory in which the .mo
  858. * file resides. Default empty string.
  859. * @return bool True when textdomain is successfully loaded, false otherwise.
  860. */
  861. function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) {
  862. /** @var WP_Textdomain_Registry $wp_textdomain_registry */
  863. global $wp_textdomain_registry;
  864. /** This filter is documented in wp-includes/l10n.php */
  865. $locale = apply_filters( 'plugin_locale', determine_locale(), $domain );
  866. $mofile = $domain . '-' . $locale . '.mo';
  867. // Try to load from the languages directory first.
  868. if ( load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile, $locale ) ) {
  869. return true;
  870. }
  871. $path = WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' );
  872. $wp_textdomain_registry->set_custom_path( $domain, $path );
  873. return load_textdomain( $domain, $path . '/' . $mofile, $locale );
  874. }
  875. /**
  876. * Loads the theme's translated strings.
  877. *
  878. * If the current locale exists as a .mo file in the theme's root directory, it
  879. * will be included in the translated strings by the $domain.
  880. *
  881. * The .mo files must be named based on the locale exactly.
  882. *
  883. * @since 1.5.0
  884. * @since 4.6.0 The function now tries to load the .mo file from the languages directory first.
  885. *
  886. * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
  887. *
  888. * @param string $domain Text domain. Unique identifier for retrieving translated strings.
  889. * @param string|false $path Optional. Path to the directory containing the .mo file.
  890. * Default false.
  891. * @return bool True when textdomain is successfully loaded, false otherwise.
  892. */
  893. function load_theme_textdomain( $domain, $path = false ) {
  894. /** @var WP_Textdomain_Registry $wp_textdomain_registry */
  895. global $wp_textdomain_registry;
  896. /**
  897. * Filters a theme's locale.
  898. *
  899. * @since 3.0.0
  900. *
  901. * @param string $locale The theme's current locale.
  902. * @param string $domain Text domain. Unique identifier for retrieving translated strings.
  903. */
  904. $locale = apply_filters( 'theme_locale', determine_locale(), $domain );
  905. $mofile = $domain . '-' . $locale . '.mo';
  906. // Try to load from the languages directory first.
  907. if ( load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile, $locale ) ) {
  908. return true;
  909. }
  910. if ( ! $path ) {
  911. $path = get_template_directory();
  912. }
  913. $wp_textdomain_registry->set_custom_path( $domain, $path );
  914. return load_textdomain( $domain, $path . '/' . $locale . '.mo', $locale );
  915. }
  916. /**
  917. * Loads the child themes translated strings.
  918. *
  919. * If the current locale exists as a .mo file in the child themes
  920. * root directory, it will be included in the translated strings by the $domain.
  921. *
  922. * The .mo files must be named based on the locale exactly.
  923. *
  924. * @since 2.9.0
  925. *
  926. * @param string $domain Text domain. Unique identifier for retrieving translated strings.
  927. * @param string|false $path Optional. Path to the directory containing the .mo file.
  928. * Default false.
  929. * @return bool True when the theme textdomain is successfully loaded, false otherwise.
  930. */
  931. function load_child_theme_textdomain( $domain, $path = false ) {
  932. if ( ! $path ) {
  933. $path = get_stylesheet_directory();
  934. }
  935. return load_theme_textdomain( $domain, $path );
  936. }
  937. /**
  938. * Loads the script translated strings.
  939. *
  940. * @since 5.0.0
  941. * @since 5.0.2 Uses load_script_translations() to load translation data.
  942. * @since 5.1.0 The `$domain` parameter was made optional.
  943. *
  944. * @see WP_Scripts::set_translations()
  945. *
  946. * @param string $handle Name of the script to register a translation domain to.
  947. * @param string $domain Optional. Text domain. Default 'default'.
  948. * @param string $path Optional. The full file path to the directory containing translation files.
  949. * @return string|false The translated strings in JSON encoding on success,
  950. * false if the script textdomain could not be loaded.
  951. */
  952. function load_script_textdomain( $handle, $domain = 'default', $path = '' ) {
  953. $wp_scripts = wp_scripts();
  954. if ( ! isset( $wp_scripts->registered[ $handle ] ) ) {
  955. return false;
  956. }
  957. $path = untrailingslashit( $path );
  958. $locale = determine_locale();
  959. // If a path was given and the handle file exists simply return it.
  960. $file_base = 'default' === $domain ? $locale : $domain . '-' . $locale;
  961. $handle_filename = $file_base . '-' . $handle . '.json';
  962. if ( $path ) {
  963. $translations = load_script_translations( $path . '/' . $handle_filename, $handle, $domain );
  964. if ( $translations ) {
  965. return $translations;
  966. }
  967. }
  968. $src = $wp_scripts->registered[ $handle ]->src;
  969. if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && 0 === strpos( $src, $wp_scripts->content_url ) ) ) {
  970. $src = $wp_scripts->base_url . $src;
  971. }
  972. $relative = false;
  973. $languages_path = WP_LANG_DIR;
  974. $src_url = wp_parse_url( $src );
  975. $content_url = wp_parse_url( content_url() );
  976. $plugins_url = wp_parse_url( plugins_url() );
  977. $site_url = wp_parse_url( site_url() );
  978. // If the host is the same or it's a relative URL.
  979. if (
  980. ( ! isset( $content_url['path'] ) || strpos( $src_url['path'], $content_url['path'] ) === 0 ) &&
  981. ( ! isset( $src_url['host'] ) || ! isset( $content_url['host'] ) || $src_url['host'] === $content_url['host'] )
  982. ) {
  983. // Make the src relative the specific plugin or theme.
  984. if ( isset( $content_url['path'] ) ) {
  985. $relative = substr( $src_url['path'], strlen( $content_url['path'] ) );
  986. } else {
  987. $relative = $src_url['path'];
  988. }
  989. $relative = trim( $relative, '/' );
  990. $relative = explode( '/', $relative );
  991. $languages_path = WP_LANG_DIR . '/' . $relative[0];
  992. $relative = array_slice( $relative, 2 ); // Remove plugins/<plugin name> or themes/<theme name>.
  993. $relative = implode( '/', $relative );
  994. } elseif (
  995. ( ! isset( $plugins_url['path'] ) || strpos( $src_url['path'], $plugins_url['path'] ) === 0 ) &&
  996. ( ! isset( $src_url['host'] ) || ! isset( $plugins_url['host'] ) || $src_url['host'] === $plugins_url['host'] )
  997. ) {
  998. // Make the src relative the specific plugin.
  999. if ( isset( $plugins_url['path'] ) ) {
  1000. $relative = substr( $src_url['path'], strlen( $plugins_url['path'] ) );
  1001. } else {
  1002. $relative = $src_url['path'];
  1003. }
  1004. $relative = trim( $relative, '/' );
  1005. $relative = explode( '/', $relative );
  1006. $languages_path = WP_LANG_DIR . '/plugins';
  1007. $relative = array_slice( $relative, 1 ); // Remove <plugin name>.
  1008. $relative = implode( '/', $relative );
  1009. } elseif ( ! isset( $src_url['host'] ) || ! isset( $site_url['host'] ) || $src_url['host'] === $site_url['host'] ) {
  1010. if ( ! isset( $site_url['path'] ) ) {
  1011. $relative = trim( $src_url['path'], '/' );
  1012. } elseif ( ( strpos( $src_url['path'], trailingslashit( $site_url['path'] ) ) === 0 ) ) {
  1013. // Make the src relative to the WP root.
  1014. $relative = substr( $src_url['path'], strlen( $site_url['path'] ) );
  1015. $relative = trim( $relative, '/' );
  1016. }
  1017. }
  1018. /**
  1019. * Filters the relative path of scripts used for finding translation files.
  1020. *
  1021. * @since 5.0.2
  1022. *
  1023. * @param string|false $relative The relative path of the script. False if it could not be determined.
  1024. * @param string $src The full source URL of the script.
  1025. */
  1026. $relative = apply_filters( 'load_script_textdomain_relative_path', $relative, $src );
  1027. // If the source is not from WP.
  1028. if ( false === $relative ) {
  1029. return load_script_translations( false, $handle, $domain );
  1030. }
  1031. // Translations are always based on the unminified filename.
  1032. if ( substr( $relative, -7 ) === '.min.js' ) {
  1033. $relative = substr( $relative, 0, -7 ) . '.js';
  1034. }
  1035. $md5_filename = $file_base . '-' . md5( $relative ) . '.json';
  1036. if ( $path ) {
  1037. $translations = load_script_translations( $path . '/' . $md5_filename, $handle, $domain );
  1038. if ( $translations ) {
  1039. return $translations;
  1040. }
  1041. }
  1042. $translations = load_script_translations( $languages_path . '/' . $md5_filename, $handle, $domain );
  1043. if ( $translations ) {
  1044. return $translations;
  1045. }
  1046. return load_script_translations( false, $handle, $domain );
  1047. }
  1048. /**
  1049. * Loads the translation data for the given script handle and text domain.
  1050. *
  1051. * @since 5.0.2
  1052. *
  1053. * @param string|false $file Path to the translation file to load. False if there isn't one.
  1054. * @param string $handle Name of the script to register a translation domain to.
  1055. * @param string $domain The text domain.
  1056. * @return string|false The JSON-encoded translated strings for the given script handle and text domain.
  1057. * False if there are none.
  1058. */
  1059. function load_script_translations( $file, $handle, $domain ) {
  1060. /**
  1061. * Pre-filters script translations for the given file, script handle and text domain.
  1062. *
  1063. * Returning a non-null value allows to override the default logic, effectively short-circuiting the function.
  1064. *
  1065. * @since 5.0.2
  1066. *
  1067. * @param string|false|null $translations JSON-encoded translation data. Default null.
  1068. * @param string|false $file Path to the translation file to load. False if there isn't one.
  1069. * @param string $handle Name of the script to register a translation domain to.
  1070. * @param string $domain The text domain.
  1071. */
  1072. $translations = apply_filters( 'pre_load_script_translations', null, $file, $handle, $domain );
  1073. if ( null !== $translations ) {
  1074. return $translations;
  1075. }
  1076. /**
  1077. * Filters the file path for loading script translations for the given script handle and text domain.
  1078. *
  1079. * @since 5.0.2
  1080. *
  1081. * @param string|false $file Path to the translation file to load. False if there isn't one.
  1082. * @param string $handle Name of the script to register a translation domain to.
  1083. * @param string $domain The text domain.
  1084. */
  1085. $file = apply_filters( 'load_script_translation_file', $file, $handle, $domain );
  1086. if ( ! $file || ! is_readable( $file ) ) {
  1087. return false;
  1088. }
  1089. $translations = file_get_contents( $file );
  1090. /**
  1091. * Filters script translations for the given file, script handle and text domain.
  1092. *
  1093. * @since 5.0.2
  1094. *
  1095. * @param string $translations JSON-encoded translation data.
  1096. * @param string $file Path to the translation file that was loaded.
  1097. * @param string $handle Name of the script to register a translation domain to.
  1098. * @param string $domain The text domain.
  1099. */
  1100. return apply_filters( 'load_script_translations', $translations, $file, $handle, $domain );
  1101. }
  1102. /**
  1103. * Loads plugin and theme text domains just-in-time.
  1104. *
  1105. * When a textdomain is encountered for the first time, we try to load
  1106. * the translation file from `wp-content/languages`, removing the need
  1107. * to call load_plugin_textdomain() or load_theme_textdomain().
  1108. *
  1109. * @since 4.6.0
  1110. * @access private
  1111. *
  1112. * @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again.
  1113. * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
  1114. *
  1115. * @param string $domain Text domain. Unique identifier for retrieving translated strings.
  1116. * @return bool True when the textdomain is successfully loaded, false otherwise.
  1117. */
  1118. function _load_textdomain_just_in_time( $domain ) {
  1119. /** @var WP_Textdomain_Registry $wp_textdomain_registry */
  1120. global $l10n_unloaded, $wp_textdomain_registry;
  1121. $l10n_unloaded = (array) $l10n_unloaded;
  1122. // Short-circuit if domain is 'default' which is reserved for core.
  1123. if ( 'default' === $domain || isset( $l10n_unloaded[ $domain ] ) ) {
  1124. return false;
  1125. }
  1126. if ( ! $wp_textdomain_registry->has( $domain ) ) {
  1127. return false;
  1128. }
  1129. $locale = determine_locale();
  1130. $path = $wp_textdomain_registry->get( $domain, $locale );
  1131. if ( ! $path ) {
  1132. return false;
  1133. }
  1134. // Themes with their language directory outside of WP_LANG_DIR have a different file name.
  1135. $template_directory = trailingslashit( get_template_directory() );
  1136. $stylesheet_directory = trailingslashit( get_stylesheet_directory() );
  1137. if ( str_starts_with( $path, $template_directory ) || str_starts_with( $path, $stylesheet_directory ) ) {
  1138. $mofile = "{$path}{$locale}.mo";
  1139. } else {
  1140. $mofile = "{$path}{$domain}-{$locale}.mo";
  1141. }
  1142. return load_textdomain( $domain, $mofile, $locale );
  1143. }
  1144. /**
  1145. * Returns the Translations instance for a text domain.
  1146. *
  1147. * If there isn't one, returns empty Translations instance.
  1148. *
  1149. * @since 2.8.0
  1150. *
  1151. * @global MO[] $l10n An array of all currently loaded text domains.
  1152. *
  1153. * @param string $domain Text domain. Unique identifier for retrieving translated strings.
  1154. * @return Translations|NOOP_Translations A Translations instance.
  1155. */
  1156. function get_translations_for_domain( $domain ) {
  1157. global $l10n;
  1158. if ( isset( $l10n[ $domain ] ) || ( _load_textdomain_just_in_time( $domain ) && isset( $l10n[ $domain ] ) ) ) {
  1159. return $l10n[ $domain ];
  1160. }
  1161. static $noop_translations = null;
  1162. if ( null === $noop_translations ) {
  1163. $noop_translations = new NOOP_Translations;
  1164. }
  1165. return $noop_translations;
  1166. }
  1167. /**
  1168. * Determines whether there are translations for the text domain.
  1169. *
  1170. * @since 3.0.0
  1171. *
  1172. * @global MO[] $l10n An array of all currently loaded text domains.
  1173. *
  1174. * @param string $domain Text domain. Unique identifier for retrieving translated strings.
  1175. * @return bool Whether there are translations.
  1176. */
  1177. function is_textdomain_loaded( $domain ) {
  1178. global $l10n;
  1179. return isset( $l10n[ $domain ] );
  1180. }
  1181. /**
  1182. * Translates role name.
  1183. *
  1184. * Since the role names are in the database and not in the source there
  1185. * are dummy gettext calls to get them into the POT file and this function
  1186. * properly translates them back.
  1187. *
  1188. * The before_last_bar() call is needed, because older installations keep the roles
  1189. * using the old context format: 'Role name|User role' and just skipping the
  1190. * content after the last bar is easier than fixing them in the DB. New installations
  1191. * won't suffer from that problem.
  1192. *
  1193. * @since 2.8.0
  1194. * @since 5.2.0 Added the `$domain` parameter.
  1195. *
  1196. * @param string $name The role name.
  1197. * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
  1198. * Default 'default'.
  1199. * @return string Translated role name on success, original name on failure.
  1200. */
  1201. function translate_user_role( $name, $domain = 'default' ) {
  1202. return translate_with_gettext_context( before_last_bar( $name ), 'User role', $domain );
  1203. }
  1204. /**
  1205. * Gets all available languages based on the presence of *.mo files in a given directory.
  1206. *
  1207. * The default directory is WP_LANG_DIR.
  1208. *
  1209. * @since 3.0.0
  1210. * @since 4.7.0 The results are now filterable with the {@see 'get_available_languages'} filter.
  1211. *
  1212. * @param string $dir A directory to search for language files.
  1213. * Default WP_LANG_DIR.
  1214. * @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.
  1215. */
  1216. function get_available_languages( $dir = null ) {
  1217. $languages = array();
  1218. $lang_files = glob( ( is_null( $dir ) ? WP_LANG_DIR : $dir ) . '/*.mo' );
  1219. if ( $lang_files ) {
  1220. foreach ( $lang_files as $lang_file ) {
  1221. $lang_file = basename( $lang_file, '.mo' );
  1222. if ( 0 !== strpos( $lang_file, 'continents-cities' ) && 0 !== strpos( $lang_file, 'ms-' ) &&
  1223. 0 !== strpos( $lang_file, 'admin-' ) ) {
  1224. $languages[] = $lang_file;
  1225. }
  1226. }
  1227. }
  1228. /**
  1229. * Filters the list of available language codes.
  1230. *
  1231. * @since 4.7.0
  1232. *
  1233. * @param string[] $languages An array of available language codes.
  1234. * @param string $dir The directory where the language files were found.
  1235. */
  1236. return apply_filters( 'get_available_languages', $languages, $dir );
  1237. }
  1238. /**
  1239. * Gets installed translations.
  1240. *
  1241. * Looks in the wp-content/languages directory for translations of
  1242. * plugins or themes.
  1243. *
  1244. * @since 3.7.0
  1245. *
  1246. * @param string $type What to search for. Accepts 'plugins', 'themes', 'core'.
  1247. * @return array Array of language data.
  1248. */
  1249. function wp_get_installed_translations( $type ) {
  1250. if ( 'themes' !== $type && 'plugins' !== $type && 'core' !== $type ) {
  1251. return array();
  1252. }
  1253. $dir = 'core' === $type ? '' : "/$type";
  1254. if ( ! is_dir( WP_LANG_DIR ) ) {
  1255. return array();
  1256. }
  1257. if ( $dir && ! is_dir( WP_LANG_DIR . $dir ) ) {
  1258. return array();
  1259. }
  1260. $files = scandir( WP_LANG_DIR . $dir );
  1261. if ( ! $files ) {
  1262. return array();
  1263. }
  1264. $language_data = array();
  1265. foreach ( $files as $file ) {
  1266. if ( '.' === $file[0] || is_dir( WP_LANG_DIR . "$dir/$file" ) ) {
  1267. continue;
  1268. }
  1269. if ( substr( $file, -3 ) !== '.po' ) {
  1270. continue;
  1271. }
  1272. if ( ! preg_match( '/(?:(.+)-)?([a-z]{2,3}(?:_[A-Z]{2})?(?:_[a-z0-9]+)?).po/', $file, $match ) ) {
  1273. continue;
  1274. }
  1275. if ( ! in_array( substr( $file, 0, -3 ) . '.mo', $files, true ) ) {
  1276. continue;
  1277. }
  1278. list( , $textdomain, $language ) = $match;
  1279. if ( '' === $textdomain ) {
  1280. $textdomain = 'default';
  1281. }
  1282. $language_data[ $textdomain ][ $language ] = wp_get_pomo_file_data( WP_LANG_DIR . "$dir/$file" );
  1283. }
  1284. return $language_data;
  1285. }
  1286. /**
  1287. * Extracts headers from a PO file.
  1288. *
  1289. * @since 3.7.0
  1290. *
  1291. * @param string $po_file Path to PO file.
  1292. * @return string[] Array of PO file header values keyed by header name.
  1293. */
  1294. function wp_get_pomo_file_data( $po_file ) {
  1295. $headers = get_file_data(
  1296. $po_file,
  1297. array(
  1298. 'POT-Creation-Date' => '"POT-Creation-Date',
  1299. 'PO-Revision-Date' => '"PO-Revision-Date',
  1300. 'Project-Id-Version' => '"Project-Id-Version',
  1301. 'X-Generator' => '"X-Generator',
  1302. )
  1303. );
  1304. foreach ( $headers as $header => $value ) {
  1305. // Remove possible contextual '\n' and closing double quote.
  1306. $headers[ $header ] = preg_replace( '~(\\\n)?"$~', '', $value );
  1307. }
  1308. return $headers;
  1309. }
  1310. /**
  1311. * Displays or returns a Language selector.
  1312. *
  1313. * @since 4.0.0
  1314. * @since 4.3.0 Introduced the `echo` argument.
  1315. * @since 4.7.0 Introduced the `show_option_site_default` argument.
  1316. * @since 5.1.0 Introduced the `show_option_en_us` argument.
  1317. * @since 5.9.0 Introduced the `explicit_option_en_us` argument.
  1318. *
  1319. * @see get_available_languages()
  1320. * @see wp_get_available_translations()
  1321. *
  1322. * @param string|array $args {
  1323. * Optional. Array or string of arguments for outputting the language selector.
  1324. *
  1325. * @type string $id ID attribute of the select element. Default 'locale'.
  1326. * @type string $name Name attribute of the select element. Default 'locale'.
  1327. * @type array $languages List of installed languages, contain only the locales.
  1328. * Default empty array.
  1329. * @type array $translations List of available translations. Default result of
  1330. * wp_get_available_translations().
  1331. * @type string $selected Language which should be selected. Default empty.
  1332. * @type bool|int $echo Whether to echo the generated markup. Accepts 0, 1, or their
  1333. * boolean equivalents. Default 1.
  1334. * @type bool $show_available_translations Whether to show available translations. Default true.
  1335. * @type bool $show_option_site_default Whether to show an option to fall back to the site's locale. Default false.
  1336. * @type bool $show_option_en_us Whether to show an option for English (United States). Default true.
  1337. * @type bool $explicit_option_en_us Whether the English (United States) option uses an explicit value of en_US
  1338. * instead of an empty value. Default false.
  1339. * }
  1340. * @return string HTML dropdown list of languages.
  1341. */
  1342. function wp_dropdown_languages( $args = array() ) {
  1343. $parsed_args = wp_parse_args(
  1344. $args,
  1345. array(
  1346. 'id' => 'locale',
  1347. 'name' => 'locale',
  1348. 'languages' => array(),
  1349. 'translations' => array(),
  1350. 'selected' => '',
  1351. 'echo' => 1,
  1352. 'show_available_translations' => true,
  1353. 'show_option_site_default' => false,
  1354. 'show_option_en_us' => true,
  1355. 'explicit_option_en_us' => false,
  1356. )
  1357. );
  1358. // Bail if no ID or no name.
  1359. if ( ! $parsed_args['id'] || ! $parsed_args['name'] ) {
  1360. return;
  1361. }
  1362. // English (United States) uses an empty string for the value attribute.
  1363. if ( 'en_US' === $parsed_args['selected'] && ! $parsed_args['explicit_option_en_us'] ) {
  1364. $parsed_args['selected'] = '';
  1365. }
  1366. $translations = $parsed_args['translations'];
  1367. if ( empty( $translations ) ) {
  1368. require_once ABSPATH . 'wp-admin/includes/translation-install.php';
  1369. $translations = wp_get_available_translations();
  1370. }
  1371. /*
  1372. * $parsed_args['languages'] should only contain the locales. Find the locale in
  1373. * $translations to get the native name. Fall back to locale.
  1374. */
  1375. $languages = array();
  1376. foreach ( $parsed_args['languages'] as $locale ) {
  1377. if ( isset( $translations[ $locale ] ) ) {
  1378. $translation = $translations[ $locale ];
  1379. $languages[] = array(
  1380. 'language' => $translation['language'],
  1381. 'native_name' => $translation['native_name'],
  1382. 'lang' => current( $translation['iso'] ),
  1383. );
  1384. // Remove installed language from available translations.
  1385. unset( $translations[ $locale ] );
  1386. } else {
  1387. $languages[] = array(
  1388. 'language' => $locale,
  1389. 'native_name' => $locale,
  1390. 'lang' => '',
  1391. );
  1392. }
  1393. }
  1394. $translations_available = ( ! empty( $translations ) && $parsed_args['show_available_translations'] );
  1395. // Holds the HTML markup.
  1396. $structure = array();
  1397. // List installed languages.
  1398. if ( $translations_available ) {
  1399. $structure[] = '<optgroup label="' . esc_attr_x( 'Installed', 'translations' ) . '">';
  1400. }
  1401. // Site default.
  1402. if ( $parsed_args['show_option_site_default'] ) {
  1403. $structure[] = sprintf(
  1404. '<option value="site-default" data-installed="1"%s>%s</option>',
  1405. selected( 'site-default', $parsed_args['selected'], false ),
  1406. _x( 'Site Default', 'default site language' )
  1407. );
  1408. }
  1409. if ( $parsed_args['show_option_en_us'] ) {
  1410. $value = ( $parsed_args['explicit_option_en_us'] ) ? 'en_US' : '';
  1411. $structure[] = sprintf(
  1412. '<option value="%s" lang="en" data-installed="1"%s>English (United States)</option>',
  1413. esc_attr( $value ),
  1414. selected( '', $parsed_args['selected'], false )
  1415. );
  1416. }
  1417. // List installed languages.
  1418. foreach ( $languages as $language ) {
  1419. $structure[] = sprintf(
  1420. '<option value="%s" lang="%s"%s data-installed="1">%s</option>',
  1421. esc_attr( $language['language'] ),
  1422. esc_attr( $language['lang'] ),
  1423. selected( $language['language'], $parsed_args['selected'], false ),
  1424. esc_html( $language['native_name'] )
  1425. );
  1426. }
  1427. if ( $translations_available ) {
  1428. $structure[] = '</optgroup>';
  1429. }
  1430. // List available translations.
  1431. if ( $translations_available ) {
  1432. $structure[] = '<optgroup label="' . esc_attr_x( 'Available', 'translations' ) . '">';
  1433. foreach ( $translations as $translation ) {
  1434. $structure[] = sprintf(
  1435. '<option value="%s" lang="%s"%s>%s</option>',
  1436. esc_attr( $translation['language'] ),
  1437. esc_attr( current( $translation['iso'] ) ),
  1438. selected( $translation['language'], $parsed_args['selected'], false ),
  1439. esc_html( $translation['native_name'] )
  1440. );
  1441. }
  1442. $structure[] = '</optgroup>';
  1443. }
  1444. // Combine the output string.
  1445. $output = sprintf( '<select name="%s" id="%s">', esc_attr( $parsed_args['name'] ), esc_attr( $parsed_args['id'] ) );
  1446. $output .= implode( "\n", $structure );
  1447. $output .= '</select>';
  1448. if ( $parsed_args['echo'] ) {
  1449. echo $output;
  1450. }
  1451. return $output;
  1452. }
  1453. /**
  1454. * Determines whether the current locale is right-to-left (RTL).
  1455. *
  1456. * For more information on this and similar theme functions, check out
  1457. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  1458. * Conditional Tags} article in the Theme Developer Handbook.
  1459. *
  1460. * @since 3.0.0
  1461. *
  1462. * @global WP_Locale $wp_locale WordPress date and time locale object.
  1463. *
  1464. * @return bool Whether locale is RTL.
  1465. */
  1466. function is_rtl() {
  1467. global $wp_locale;
  1468. if ( ! ( $wp_locale instanceof WP_Locale ) ) {
  1469. return false;
  1470. }
  1471. return $wp_locale->is_rtl();
  1472. }
  1473. /**
  1474. * Switches the translations according to the given locale.
  1475. *
  1476. * @since 4.7.0
  1477. *
  1478. * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object.
  1479. *
  1480. * @param string $locale The locale.
  1481. * @return bool True on success, false on failure.
  1482. */
  1483. function switch_to_locale( $locale ) {
  1484. /* @var WP_Locale_Switcher $wp_locale_switcher */
  1485. global $wp_locale_switcher;
  1486. return $wp_locale_switcher->switch_to_locale( $locale );
  1487. }
  1488. /**
  1489. * Restores the translations according to the previous locale.
  1490. *
  1491. * @since 4.7.0
  1492. *
  1493. * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object.
  1494. *
  1495. * @return string|false Locale on success, false on error.
  1496. */
  1497. function restore_previous_locale() {
  1498. /* @var WP_Locale_Switcher $wp_locale_switcher */
  1499. global $wp_locale_switcher;
  1500. return $wp_locale_switcher->restore_previous_locale();
  1501. }
  1502. /**
  1503. * Restores the translations according to the original locale.
  1504. *
  1505. * @since 4.7.0
  1506. *
  1507. * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object.
  1508. *
  1509. * @return string|false Locale on success, false on error.
  1510. */
  1511. function restore_current_locale() {
  1512. /* @var WP_Locale_Switcher $wp_locale_switcher */
  1513. global $wp_locale_switcher;
  1514. return $wp_locale_switcher->restore_current_locale();
  1515. }
  1516. /**
  1517. * Determines whether switch_to_locale() is in effect.
  1518. *
  1519. * @since 4.7.0
  1520. *
  1521. * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object.
  1522. *
  1523. * @return bool True if the locale has been switched, false otherwise.
  1524. */
  1525. function is_locale_switched() {
  1526. /* @var WP_Locale_Switcher $wp_locale_switcher */
  1527. global $wp_locale_switcher;
  1528. return $wp_locale_switcher->is_switched();
  1529. }
  1530. /**
  1531. * Translates the provided settings value using its i18n schema.
  1532. *
  1533. * @since 5.9.0
  1534. * @access private
  1535. *
  1536. * @param string|string[]|array[]|object $i18n_schema I18n schema for the setting.
  1537. * @param string|string[]|array[] $settings Value for the settings.
  1538. * @param string $textdomain Textdomain to use with translations.
  1539. *
  1540. * @return string|string[]|array[] Translated settings.
  1541. */
  1542. function translate_settings_using_i18n_schema( $i18n_schema, $settings, $textdomain ) {
  1543. if ( empty( $i18n_schema ) || empty( $settings ) || empty( $textdomain ) ) {
  1544. return $settings;
  1545. }
  1546. if ( is_string( $i18n_schema ) && is_string( $settings ) ) {
  1547. return translate_with_gettext_context( $settings, $i18n_schema, $textdomain );
  1548. }
  1549. if ( is_array( $i18n_schema ) && is_array( $settings ) ) {
  1550. $translated_settings = array();
  1551. foreach ( $settings as $value ) {
  1552. $translated_settings[] = translate_settings_using_i18n_schema( $i18n_schema[0], $value, $textdomain );
  1553. }
  1554. return $translated_settings;
  1555. }
  1556. if ( is_object( $i18n_schema ) && is_array( $settings ) ) {
  1557. $group_key = '*';
  1558. $translated_settings = array();
  1559. foreach ( $settings as $key => $value ) {
  1560. if ( isset( $i18n_schema->$key ) ) {
  1561. $translated_settings[ $key ] = translate_settings_using_i18n_schema( $i18n_schema->$key, $value, $textdomain );
  1562. } elseif ( isset( $i18n_schema->$group_key ) ) {
  1563. $translated_settings[ $key ] = translate_settings_using_i18n_schema( $i18n_schema->$group_key, $value, $textdomain );
  1564. } else {
  1565. $translated_settings[ $key ] = $value;
  1566. }
  1567. }
  1568. return $translated_settings;
  1569. }
  1570. return $settings;
  1571. }
  1572. /**
  1573. * Retrieves the list item separator based on the locale.
  1574. *
  1575. * @since 6.0.0
  1576. *
  1577. * @global WP_Locale $wp_locale WordPress date and time locale object.
  1578. *
  1579. * @return string Locale-specific list item separator.
  1580. */
  1581. function wp_get_list_item_separator() {
  1582. global $wp_locale;
  1583. return $wp_locale->get_list_item_separator();
  1584. }