PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/l10n.php

https://bitbucket.org/betaimages/chakalos
PHP | 553 lines | 183 code | 63 blank | 307 comment | 38 complexity | 82a5b4e9f8e79b661d31431d9143450d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * WordPress Translation API
  4. *
  5. * @package WordPress
  6. * @subpackage i18n
  7. */
  8. /**
  9. * Gets the current locale.
  10. *
  11. * If the locale is set, then it will filter the locale in the 'locale' filter
  12. * hook and return the value.
  13. *
  14. * If the locale is not set already, then the WPLANG constant is used if it is
  15. * defined. Then it is filtered through the 'locale' filter hook and the value
  16. * for the locale global set and the locale is returned.
  17. *
  18. * The process to get the locale should only be done once, but the locale will
  19. * always be filtered using the 'locale' hook.
  20. *
  21. * @since 1.5.0
  22. * @uses apply_filters() Calls 'locale' hook on locale value.
  23. * @uses $locale Gets the locale stored in the global.
  24. *
  25. * @return string The locale of the blog or from the 'locale' hook.
  26. */
  27. function get_locale() {
  28. global $locale;
  29. if ( isset( $locale ) )
  30. return apply_filters( 'locale', $locale );
  31. // WPLANG is defined in wp-config.
  32. if ( defined( 'WPLANG' ) )
  33. $locale = WPLANG;
  34. // If multisite, check options.
  35. if ( is_multisite() ) {
  36. // Don't check blog option when installing.
  37. if ( defined( 'WP_INSTALLING' ) || ( false === $ms_locale = get_option( 'WPLANG' ) ) )
  38. $ms_locale = get_site_option('WPLANG');
  39. if ( $ms_locale !== false )
  40. $locale = $ms_locale;
  41. }
  42. if ( empty( $locale ) )
  43. $locale = 'en_US';
  44. return apply_filters( 'locale', $locale );
  45. }
  46. /**
  47. * Retrieves the translation of $text. If there is no translation, or
  48. * the domain isn't loaded, the original text is returned.
  49. *
  50. * @see __() Don't use translate() directly, use __()
  51. * @since 2.2.0
  52. * @uses apply_filters() Calls 'gettext' on domain translated text
  53. * with the untranslated text as second parameter.
  54. *
  55. * @param string $text Text to translate.
  56. * @param string $domain Domain to retrieve the translated text.
  57. * @return string Translated text
  58. */
  59. function translate( $text, $domain = 'default' ) {
  60. $translations = get_translations_for_domain( $domain );
  61. return apply_filters( 'gettext', $translations->translate( $text ), $text, $domain );
  62. }
  63. function before_last_bar( $string ) {
  64. $last_bar = strrpos( $string, '|' );
  65. if ( false == $last_bar )
  66. return $string;
  67. else
  68. return substr( $string, 0, $last_bar );
  69. }
  70. function translate_with_gettext_context( $text, $context, $domain = 'default' ) {
  71. $translations = get_translations_for_domain( $domain );
  72. return apply_filters( 'gettext_with_context', $translations->translate( $text, $context ), $text, $context, $domain );
  73. }
  74. /**
  75. * Retrieves the translation of $text. If there is no translation, or
  76. * the domain isn't loaded, the original text is returned.
  77. *
  78. * @see translate() An alias of translate()
  79. * @since 2.1.0
  80. *
  81. * @param string $text Text to translate
  82. * @param string $domain Optional. Domain to retrieve the translated text
  83. * @return string Translated text
  84. */
  85. function __( $text, $domain = 'default' ) {
  86. return translate( $text, $domain );
  87. }
  88. /**
  89. * Retrieves the translation of $text and escapes it for safe use in an attribute.
  90. * If there is no translation, or the domain isn't loaded, the original text is returned.
  91. *
  92. * @see translate() An alias of translate()
  93. * @see esc_attr()
  94. * @since 2.8.0
  95. *
  96. * @param string $text Text to translate
  97. * @param string $domain Optional. Domain to retrieve the translated text
  98. * @return string Translated text
  99. */
  100. function esc_attr__( $text, $domain = 'default' ) {
  101. return esc_attr( translate( $text, $domain ) );
  102. }
  103. /**
  104. * Retrieves the translation of $text and escapes it for safe use in HTML output.
  105. * If there is no translation, or the domain isn't loaded, the original text is returned.
  106. *
  107. * @see translate() An alias of translate()
  108. * @see esc_html()
  109. * @since 2.8.0
  110. *
  111. * @param string $text Text to translate
  112. * @param string $domain Optional. Domain to retrieve the translated text
  113. * @return string Translated text
  114. */
  115. function esc_html__( $text, $domain = 'default' ) {
  116. return esc_html( translate( $text, $domain ) );
  117. }
  118. /**
  119. * Displays the returned translated text from translate().
  120. *
  121. * @see translate() Echoes returned translate() string
  122. * @since 1.2.0
  123. *
  124. * @param string $text Text to translate
  125. * @param string $domain Optional. Domain to retrieve the translated text
  126. */
  127. function _e( $text, $domain = 'default' ) {
  128. echo translate( $text, $domain );
  129. }
  130. /**
  131. * Displays translated text that has been escaped for safe use in an attribute.
  132. *
  133. * @see translate() Echoes returned translate() string
  134. * @see esc_attr()
  135. * @since 2.8.0
  136. *
  137. * @param string $text Text to translate
  138. * @param string $domain Optional. Domain to retrieve the translated text
  139. */
  140. function esc_attr_e( $text, $domain = 'default' ) {
  141. echo esc_attr( translate( $text, $domain ) );
  142. }
  143. /**
  144. * Displays translated text that has been escaped for safe use in HTML output.
  145. *
  146. * @see translate() Echoes returned translate() string
  147. * @see esc_html()
  148. * @since 2.8.0
  149. *
  150. * @param string $text Text to translate
  151. * @param string $domain Optional. Domain to retrieve the translated text
  152. */
  153. function esc_html_e( $text, $domain = 'default' ) {
  154. echo esc_html( translate( $text, $domain ) );
  155. }
  156. /**
  157. * Retrieve translated string with gettext context
  158. *
  159. * Quite a few times, there will be collisions with similar translatable text
  160. * found in more than two places but with different translated context.
  161. *
  162. * By including the context in the pot file translators can translate the two
  163. * strings differently.
  164. *
  165. * @since 2.8.0
  166. *
  167. * @param string $text Text to translate
  168. * @param string $context Context information for the translators
  169. * @param string $domain Optional. Domain to retrieve the translated text
  170. * @return string Translated context string without pipe
  171. */
  172. function _x( $text, $context, $domain = 'default' ) {
  173. return translate_with_gettext_context( $text, $context, $domain );
  174. }
  175. /**
  176. * Displays translated string with gettext context
  177. *
  178. * @see _x
  179. * @since 3.0.0
  180. *
  181. * @param string $text Text to translate
  182. * @param string $context Context information for the translators
  183. * @param string $domain Optional. Domain to retrieve the translated text
  184. * @return string Translated context string without pipe
  185. */
  186. function _ex( $text, $context, $domain = 'default' ) {
  187. echo _x( $text, $context, $domain );
  188. }
  189. function esc_attr_x( $single, $context, $domain = 'default' ) {
  190. return esc_attr( translate_with_gettext_context( $single, $context, $domain ) );
  191. }
  192. function esc_html_x( $single, $context, $domain = 'default' ) {
  193. return esc_html( translate_with_gettext_context( $single, $context, $domain ) );
  194. }
  195. /**
  196. * Retrieve the plural or single form based on the amount.
  197. *
  198. * If the domain is not set in the $l10n list, then a comparison will be made
  199. * and either $plural or $single parameters returned.
  200. *
  201. * If the domain does exist, then the parameters $single, $plural, and $number
  202. * will first be passed to the domain's ngettext method. Then it will be passed
  203. * to the 'ngettext' filter hook along with the same parameters. The expected
  204. * type will be a string.
  205. *
  206. * @since 2.8.0
  207. * @uses $l10n Gets list of domain translated string (gettext_reader) objects
  208. * @uses apply_filters() Calls 'ngettext' hook on domains text returned,
  209. * along with $single, $plural, and $number parameters. Expected to return string.
  210. *
  211. * @param string $single The text that will be used if $number is 1
  212. * @param string $plural The text that will be used if $number is not 1
  213. * @param int $number The number to compare against to use either $single or $plural
  214. * @param string $domain Optional. The domain identifier the text should be retrieved in
  215. * @return string Either $single or $plural translated text
  216. */
  217. function _n( $single, $plural, $number, $domain = 'default' ) {
  218. $translations = get_translations_for_domain( $domain );
  219. $translation = $translations->translate_plural( $single, $plural, $number );
  220. return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
  221. }
  222. /**
  223. * A hybrid of _n() and _x(). It supports contexts and plurals.
  224. *
  225. * @see _n()
  226. * @see _x()
  227. *
  228. */
  229. function _nx($single, $plural, $number, $context, $domain = 'default') {
  230. $translations = get_translations_for_domain( $domain );
  231. $translation = $translations->translate_plural( $single, $plural, $number, $context );
  232. return apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain );
  233. }
  234. /**
  235. * Register plural strings in POT file, but don't translate them.
  236. *
  237. * Used when you want to keep structures with translatable plural strings and
  238. * use them later.
  239. *
  240. * Example:
  241. * $messages = array(
  242. * 'post' => _n_noop('%s post', '%s posts'),
  243. * 'page' => _n_noop('%s pages', '%s pages')
  244. * );
  245. * ...
  246. * $message = $messages[$type];
  247. * $usable_text = sprintf( translate_nooped_plural( $message, $count ), $count );
  248. *
  249. * @since 2.5
  250. * @param string $singular Single form to be i18ned
  251. * @param string $plural Plural form to be i18ned
  252. * @param string $domain Optional. The domain identifier the text will be retrieved in
  253. * @return array array($singular, $plural)
  254. */
  255. function _n_noop( $singular, $plural, $domain = null ) {
  256. return array( 0 => $singular, 1 => $plural, 'singular' => $singular, 'plural' => $plural, 'context' => null, 'domain' => $domain );
  257. }
  258. /**
  259. * Register plural strings with context in POT file, but don't translate them.
  260. *
  261. * @see _n_noop()
  262. */
  263. function _nx_noop( $singular, $plural, $context, $domain = null ) {
  264. return array( 0 => $singular, 1 => $plural, 2 => $context, 'singular' => $singular, 'plural' => $plural, 'context' => $context, 'domain' => $domain );
  265. }
  266. /**
  267. * Translate the result of _n_noop() or _nx_noop()
  268. *
  269. * @since 3.1
  270. * @param array $nooped_plural Array with singular, plural and context keys, usually the result of _n_noop() or _nx_noop()
  271. * @param int $count Number of objects
  272. * @param string $domain Optional. The domain identifier the text should be retrieved in. If $nooped_plural contains
  273. * a domain passed to _n_noop() or _nx_noop(), it will override this value.
  274. */
  275. function translate_nooped_plural( $nooped_plural, $count, $domain = 'default' ) {
  276. if ( $nooped_plural['domain'] )
  277. $domain = $nooped_plural['domain'];
  278. if ( $nooped_plural['context'] )
  279. return _nx( $nooped_plural['singular'], $nooped_plural['plural'], $count, $nooped_plural['context'], $domain );
  280. else
  281. return _n( $nooped_plural['singular'], $nooped_plural['plural'], $count, $domain );
  282. }
  283. /**
  284. * Loads a MO file into the domain $domain.
  285. *
  286. * If the domain already exists, the translations will be merged. If both
  287. * sets have the same string, the translation from the original value will be taken.
  288. *
  289. * On success, the .mo file will be placed in the $l10n global by $domain
  290. * and will be a MO object.
  291. *
  292. * @since 1.5.0
  293. * @uses $l10n Gets list of domain translated string objects
  294. *
  295. * @param string $domain Unique identifier for retrieving translated strings
  296. * @param string $mofile Path to the .mo file
  297. * @return bool True on success, false on failure
  298. */
  299. function load_textdomain( $domain, $mofile ) {
  300. global $l10n;
  301. $plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile );
  302. if ( true == $plugin_override ) {
  303. return true;
  304. }
  305. do_action( 'load_textdomain', $domain, $mofile );
  306. $mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain );
  307. if ( !is_readable( $mofile ) ) return false;
  308. $mo = new MO();
  309. if ( !$mo->import_from_file( $mofile ) ) return false;
  310. if ( isset( $l10n[$domain] ) )
  311. $mo->merge_with( $l10n[$domain] );
  312. $l10n[$domain] = &$mo;
  313. return true;
  314. }
  315. /**
  316. * Unloads translations for a domain
  317. *
  318. * @since 3.0.0
  319. * @param string $domain Textdomain to be unloaded
  320. * @return bool Whether textdomain was unloaded
  321. */
  322. function unload_textdomain( $domain ) {
  323. global $l10n;
  324. $plugin_override = apply_filters( 'override_unload_textdomain', false, $domain );
  325. if ( $plugin_override )
  326. return true;
  327. do_action( 'unload_textdomain', $domain );
  328. if ( isset( $l10n[$domain] ) ) {
  329. unset( $l10n[$domain] );
  330. return true;
  331. }
  332. return false;
  333. }
  334. /**
  335. * Loads default translated strings based on locale.
  336. *
  337. * Loads the .mo file in WP_LANG_DIR constant path from WordPress root. The
  338. * translated (.mo) file is named based on the locale.
  339. *
  340. * @since 1.5.0
  341. */
  342. function load_default_textdomain() {
  343. $locale = get_locale();
  344. load_textdomain( 'default', WP_LANG_DIR . "/$locale.mo" );
  345. if ( ( is_multisite() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) && ! file_exists( WP_LANG_DIR . "/admin-$locale.mo" ) ) {
  346. load_textdomain( 'default', WP_LANG_DIR . "/ms-$locale.mo" );
  347. return;
  348. }
  349. if ( is_admin() || ( defined( 'WP_REPAIRING' ) && WP_REPAIRING ) )
  350. load_textdomain( 'default', WP_LANG_DIR . "/admin-$locale.mo" );
  351. if ( is_network_admin() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) )
  352. load_textdomain( 'default', WP_LANG_DIR . "/admin-network-$locale.mo" );
  353. }
  354. /**
  355. * Loads the plugin's translated strings.
  356. *
  357. * If the path is not given then it will be the root of the plugin directory.
  358. * The .mo file should be named based on the domain with a dash, and then the locale exactly.
  359. *
  360. * @since 1.5.0
  361. *
  362. * @param string $domain Unique identifier for retrieving translated strings
  363. * @param string $abs_rel_path Optional. Relative path to ABSPATH of a folder,
  364. * where the .mo file resides. Deprecated, but still functional until 2.7
  365. * @param string $plugin_rel_path Optional. Relative path to WP_PLUGIN_DIR. This is the preferred argument to use. It takes precedence over $abs_rel_path
  366. */
  367. function load_plugin_textdomain( $domain, $abs_rel_path = false, $plugin_rel_path = false ) {
  368. $locale = apply_filters( 'plugin_locale', get_locale(), $domain );
  369. if ( false !== $plugin_rel_path ) {
  370. $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' );
  371. } else if ( false !== $abs_rel_path ) {
  372. _deprecated_argument( __FUNCTION__, '2.7' );
  373. $path = ABSPATH . trim( $abs_rel_path, '/' );
  374. } else {
  375. $path = WP_PLUGIN_DIR;
  376. }
  377. $mofile = $path . '/'. $domain . '-' . $locale . '.mo';
  378. return load_textdomain( $domain, $mofile );
  379. }
  380. /**
  381. * Load the translated strings for a plugin residing in the mu-plugins dir.
  382. *
  383. * @since 3.0.0
  384. *
  385. * @param string $domain Unique identifier for retrieving translated strings
  386. * @param string $mu_plugin_rel_path Relative to WPMU_PLUGIN_DIR directory in which
  387. * the MO file resides. Defaults to empty string.
  388. */
  389. function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) {
  390. $locale = apply_filters( 'plugin_locale', get_locale(), $domain );
  391. $path = WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' );
  392. load_textdomain( $domain, trailingslashit( $path ) . "$domain-$locale.mo" );
  393. }
  394. /**
  395. * Loads the theme's translated strings.
  396. *
  397. * If the current locale exists as a .mo file in the theme's root directory, it
  398. * will be included in the translated strings by the $domain.
  399. *
  400. * The .mo files must be named based on the locale exactly.
  401. *
  402. * @since 1.5.0
  403. *
  404. * @param string $domain Unique identifier for retrieving translated strings
  405. */
  406. function load_theme_textdomain( $domain, $path = false ) {
  407. $locale = apply_filters( 'theme_locale', get_locale(), $domain );
  408. if ( ! $path )
  409. $path = get_template_directory();
  410. // Load the textdomain from the Theme provided location, or theme directory first
  411. $mofile = "{$path}/{$locale}.mo";
  412. if ( $loaded = load_textdomain($domain, $mofile) )
  413. return $loaded;
  414. // Else, load textdomain from the Language directory
  415. $mofile = WP_LANG_DIR . "/themes/{$domain}-{$locale}.mo";
  416. return load_textdomain($domain, $mofile);
  417. }
  418. /**
  419. * Loads the child themes translated strings.
  420. *
  421. * If the current locale exists as a .mo file in the child themes root directory, it
  422. * will be included in the translated strings by the $domain.
  423. *
  424. * The .mo files must be named based on the locale exactly.
  425. *
  426. * @since 2.9.0
  427. *
  428. * @param string $domain Unique identifier for retrieving translated strings
  429. */
  430. function load_child_theme_textdomain( $domain, $path = false ) {
  431. if ( ! $path )
  432. $path = get_stylesheet_directory();
  433. return load_theme_textdomain( $domain, $path );
  434. }
  435. /**
  436. * Returns the Translations instance for a domain. If there isn't one,
  437. * returns empty Translations instance.
  438. *
  439. * @param string $domain
  440. * @return object A Translation instance
  441. */
  442. function get_translations_for_domain( $domain ) {
  443. global $l10n;
  444. if ( !isset( $l10n[$domain] ) ) {
  445. $l10n[$domain] = new NOOP_Translations;
  446. }
  447. return $l10n[$domain];
  448. }
  449. /**
  450. * Whether there are translations for the domain
  451. *
  452. * @since 3.0.0
  453. * @param string $domain
  454. * @return bool Whether there are translations
  455. */
  456. function is_textdomain_loaded( $domain ) {
  457. global $l10n;
  458. return isset( $l10n[$domain] );
  459. }
  460. /**
  461. * Translates role name. Since the role names are in the database and
  462. * not in the source there are dummy gettext calls to get them into the POT
  463. * file and this function properly translates them back.
  464. *
  465. * The before_last_bar() call is needed, because older installs keep the roles
  466. * using the old context format: 'Role name|User role' and just skipping the
  467. * content after the last bar is easier than fixing them in the DB. New installs
  468. * won't suffer from that problem.
  469. */
  470. function translate_user_role( $name ) {
  471. return translate_with_gettext_context( before_last_bar($name), 'User role' );
  472. }
  473. /**
  474. * Get all available languages based on the presence of *.mo files in a given directory. The default directory is WP_LANG_DIR.
  475. *
  476. * @since 3.0.0
  477. *
  478. * @param string $dir A directory in which to search for language files. The default directory is WP_LANG_DIR.
  479. * @return array 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.
  480. */
  481. function get_available_languages( $dir = null ) {
  482. $languages = array();
  483. foreach( (array)glob( ( is_null( $dir) ? WP_LANG_DIR : $dir ) . '/*.mo' ) as $lang_file ) {
  484. $lang_file = basename($lang_file, '.mo');
  485. if ( 0 !== strpos( $lang_file, 'continents-cities' ) && 0 !== strpos( $lang_file, 'ms-' ) &&
  486. 0 !== strpos( $lang_file, 'admin-' ))
  487. $languages[] = $lang_file;
  488. }
  489. return $languages;
  490. }