PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/blog/stavBlog.old/wp-includes/l10n.php

https://github.com/mikitracey/ifusa
PHP | 455 lines | 140 code | 52 blank | 263 comment | 14 complexity | 46ce39941ff0535d6a609d12644a35c5 MD5 | raw file
  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 ( empty( $locale ) )
  35. $locale = 'en_US';
  36. return apply_filters( 'locale', $locale );
  37. }
  38. /**
  39. * Retrieves the translation of $text. If there is no translation, or
  40. * the domain isn't loaded the original text is returned.
  41. *
  42. * @see __() Don't use translate() directly, use __()
  43. * @since 2.2.0
  44. * @uses apply_filters() Calls 'gettext' on domain translated text
  45. * with the untranslated text as second parameter.
  46. *
  47. * @param string $text Text to translate.
  48. * @param string $domain Domain to retrieve the translated text.
  49. * @return string Translated text
  50. */
  51. function translate( $text, $domain = 'default' ) {
  52. $translations = &get_translations_for_domain( $domain );
  53. return apply_filters( 'gettext', $translations->translate( $text ), $text, $domain );
  54. }
  55. function before_last_bar( $string ) {
  56. $last_bar = strrpos( $string, '|' );
  57. if ( false == $last_bar )
  58. return $string;
  59. else
  60. return substr( $string, 0, $last_bar );
  61. }
  62. /**
  63. * Translates $text like translate(), but assumes that the text
  64. * contains a context after its last vertical bar.
  65. *
  66. * @since 2.5
  67. * @uses translate()
  68. *
  69. * @param string $text Text to translate
  70. * @param string $domain Domain to retrieve the translated text
  71. * @return string Translated text
  72. */
  73. function translate_with_context( $text, $domain = 'default' ) {
  74. return before_last_bar( translate( $text, $domain ) );
  75. }
  76. function translate_with_gettext_context( $text, $context, $domain = 'default' ) {
  77. $translations = &get_translations_for_domain( $domain );
  78. return apply_filters( 'gettext_with_context', $translations->translate( $text, $context ), $text, $context, $domain );
  79. }
  80. /**
  81. * Retrieves the translation of $text. If there is no translation, or
  82. * the domain isn't loaded the original text is returned.
  83. *
  84. * @see translate() An alias of translate()
  85. * @since 2.1.0
  86. *
  87. * @param string $text Text to translate
  88. * @param string $domain Optional. Domain to retrieve the translated text
  89. * @return string Translated text
  90. */
  91. function __( $text, $domain = 'default' ) {
  92. return translate( $text, $domain );
  93. }
  94. /**
  95. * Retrieves the translation of $text and escapes it for safe use in an attribute.
  96. * If there is no translation, or the domain isn't loaded the original text is returned.
  97. *
  98. * @see translate() An alias of translate()
  99. * @see esc_attr()
  100. * @since 2.8.0
  101. *
  102. * @param string $text Text to translate
  103. * @param string $domain Optional. Domain to retrieve the translated text
  104. * @return string Translated text
  105. */
  106. function esc_attr__( $text, $domain = 'default' ) {
  107. return esc_attr( translate( $text, $domain ) );
  108. }
  109. /**
  110. * Retrieves the translation of $text and escapes it for safe use in HTML output.
  111. * If there is no translation, or the domain isn't loaded the original text is returned.
  112. *
  113. * @see translate() An alias of translate()
  114. * @see esc_html()
  115. * @since 2.8.0
  116. *
  117. * @param string $text Text to translate
  118. * @param string $domain Optional. Domain to retrieve the translated text
  119. * @return string Translated text
  120. */
  121. function esc_html__( $text, $domain = 'default' ) {
  122. return esc_html( translate( $text, $domain ) );
  123. }
  124. /**
  125. * Displays the returned translated text from translate().
  126. *
  127. * @see translate() Echoes returned translate() string
  128. * @since 1.2.0
  129. *
  130. * @param string $text Text to translate
  131. * @param string $domain Optional. Domain to retrieve the translated text
  132. */
  133. function _e( $text, $domain = 'default' ) {
  134. echo translate( $text, $domain );
  135. }
  136. /**
  137. * Displays translated text that has been escaped for safe use in an attribute.
  138. *
  139. * @see translate() Echoes returned translate() string
  140. * @see esc_attr()
  141. * @since 2.8.0
  142. *
  143. * @param string $text Text to translate
  144. * @param string $domain Optional. Domain to retrieve the translated text
  145. */
  146. function esc_attr_e( $text, $domain = 'default' ) {
  147. echo esc_attr( translate( $text, $domain ) );
  148. }
  149. /**
  150. * Displays translated text that has been escaped for safe use in HTML output.
  151. *
  152. * @see translate() Echoes returned translate() string
  153. * @see esc_html()
  154. * @since 2.8.0
  155. *
  156. * @param string $text Text to translate
  157. * @param string $domain Optional. Domain to retrieve the translated text
  158. */
  159. function esc_html_e( $text, $domain = 'default' ) {
  160. echo esc_html( translate( $text, $domain ) );
  161. }
  162. /**
  163. * Retrieve translated string with gettext context
  164. *
  165. * Quite a few times, there will be collisions with similar translatable text
  166. * found in more than two places but with different translated context.
  167. *
  168. * By including the context in the pot file translators can translate the two
  169. * string differently
  170. *
  171. * @since 2.8
  172. *
  173. * @param string $text Text to translate
  174. * @param string $context Context information for the translators
  175. * @param string $domain Optional. Domain to retrieve the translated text
  176. * @return string Translated context string without pipe
  177. */
  178. function _x( $single, $context, $domain = 'default' ) {
  179. return translate_with_gettext_context( $single, $context, $domain );
  180. }
  181. function esc_attr_x( $single, $context, $domain = 'default' ) {
  182. return esc_attr( translate_with_gettext_context( $single, $context, $domain ) );
  183. }
  184. function esc_html_x( $single, $context, $domain = 'default' ) {
  185. return esc_html( translate_with_gettext_context( $single, $context, $domain ) );
  186. }
  187. function __ngettext() {
  188. _deprecated_function( __FUNCTION__, '2.8', '_n()' );
  189. $args = func_get_args();
  190. return call_user_func_array('_n', $args);
  191. }
  192. /**
  193. * Retrieve the plural or single form based on the amount.
  194. *
  195. * If the domain is not set in the $l10n list, then a comparison will be made
  196. * and either $plural or $single parameters returned.
  197. *
  198. * If the domain does exist, then the parameters $single, $plural, and $number
  199. * will first be passed to the domain's ngettext method. Then it will be passed
  200. * to the 'ngettext' filter hook along with the same parameters. The expected
  201. * type will be a string.
  202. *
  203. * @since 1.2.0
  204. * @uses $l10n Gets list of domain translated string (gettext_reader) objects
  205. * @uses apply_filters() Calls 'ngettext' hook on domains text returned,
  206. * along with $single, $plural, and $number parameters. Expected to return string.
  207. *
  208. * @param string $single The text that will be used if $number is 1
  209. * @param string $plural The text that will be used if $number is not 1
  210. * @param int $number The number to compare against to use either $single or $plural
  211. * @param string $domain Optional. The domain identifier the text should be retrieved in
  212. * @return string Either $single or $plural translated text
  213. */
  214. function _n( $single, $plural, $number, $domain = 'default' ) {
  215. $translations = &get_translations_for_domain( $domain );
  216. $translation = $translations->translate_plural( $single, $plural, $number );
  217. return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
  218. }
  219. /**
  220. * @see _n() A version of _n(), which supports contexts --
  221. * strips everything from the translation after the last bar
  222. *
  223. */
  224. function _nc( $single, $plural, $number, $domain = 'default' ) {
  225. return before_last_bar( _n( $single, $plural, $number, $domain ) );
  226. }
  227. function _nx($single, $plural, $number, $context, $domain = 'default') {
  228. $translations = &get_translations_for_domain( $domain );
  229. $translation = $translations->translate_plural( $single, $plural, $number, $context );
  230. return apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain );
  231. }
  232. /**
  233. * @deprecated Use _n_noop()
  234. */
  235. function __ngettext_noop() {
  236. _deprecated_function( __FUNCTION__, '2.8', '_n_noop()' );
  237. $args = func_get_args();
  238. return call_user_func_array('_n_noop', $args);
  239. }
  240. /**
  241. * Register plural strings in POT file, but don't translate them.
  242. *
  243. * Used when you want do keep structures with translatable plural strings and
  244. * use them later.
  245. *
  246. * Example:
  247. * $messages = array(
  248. * 'post' => _n_noop('%s post', '%s posts'),
  249. * 'page' => _n_noop('%s pages', '%s pages')
  250. * );
  251. * ...
  252. * $message = $messages[$type];
  253. * $usable_text = sprintf(_n($message[0], $message[1], $count), $count);
  254. *
  255. * @since 2.5
  256. * @param $single Single form to be i18ned
  257. * @param $plural Plural form to be i18ned
  258. * @return array array($single, $plural)
  259. */
  260. function _n_noop( $single, $plural ) {
  261. return array( $single, $plural );
  262. }
  263. /**
  264. * Register plural strings with context in POT file, but don't translate them.
  265. *
  266. * @see _n_noop()
  267. */
  268. function _nx_noop( $single, $plural, $context ) {
  269. return array( $single, $plural, $context );
  270. }
  271. /**
  272. * Loads a MO file into the domain $domain.
  273. *
  274. * If the domain already exists, the translations will be merged. If both
  275. * sets have the same string, the translation from the original value will be taken.
  276. *
  277. * On success, the .mo file will be placed in the $l10n global by $domain
  278. * and will be a MO object.
  279. *
  280. * @since 1.5.0
  281. * @uses $l10n Gets list of domain translated string objects
  282. *
  283. * @param string $domain Unique identifier for retrieving translated strings
  284. * @param string $mofile Path to the .mo file
  285. * @return bool true on success, false on failure
  286. */
  287. function load_textdomain( $domain, $mofile ) {
  288. global $l10n;
  289. $plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile );
  290. if ( true == $plugin_override ) {
  291. return true;
  292. }
  293. do_action( 'load_textdomain', $domain, $mofile );
  294. $mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain );
  295. if ( !is_readable( $mofile ) ) return false;
  296. $mo = new MO();
  297. if ( !$mo->import_from_file( $mofile ) ) return false;
  298. if ( isset( $l10n[$domain] ) )
  299. $mo->merge_with( $l10n[$domain] );
  300. $l10n[$domain] = &$mo;
  301. return true;
  302. }
  303. /**
  304. * Loads default translated strings based on locale.
  305. *
  306. * Loads the .mo file in WP_LANG_DIR constant path from WordPress root. The
  307. * translated (.mo) file is named based off of the locale.
  308. *
  309. * @since 1.5.0
  310. */
  311. function load_default_textdomain() {
  312. $locale = get_locale();
  313. $mofile = WP_LANG_DIR . "/$locale.mo";
  314. return load_textdomain( 'default', $mofile );
  315. }
  316. /**
  317. * Loads the plugin's translated strings.
  318. *
  319. * If the path is not given then it will be the root of the plugin directory.
  320. * The .mo file should be named based on the domain with a dash, and then the locale exactly.
  321. *
  322. * @since 1.5.0
  323. *
  324. * @param string $domain Unique identifier for retrieving translated strings
  325. * @param string $abs_rel_path Optional. Relative path to ABSPATH of a folder,
  326. * where the .mo file resides. Deprecated, but still functional until 2.7
  327. * @param string $plugin_rel_path Optional. Relative path to WP_PLUGIN_DIR. This is the preferred argument to use. It takes precendence over $abs_rel_path
  328. */
  329. function load_plugin_textdomain( $domain, $abs_rel_path = false, $plugin_rel_path = false ) {
  330. $locale = get_locale();
  331. if ( false !== $plugin_rel_path )
  332. $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' );
  333. else if ( false !== $abs_rel_path )
  334. $path = ABSPATH . trim( $abs_rel_path, '/' );
  335. else
  336. $path = WP_PLUGIN_DIR;
  337. $mofile = $path . '/'. $domain . '-' . $locale . '.mo';
  338. return load_textdomain( $domain, $mofile );
  339. }
  340. /**
  341. * Loads the theme's translated strings.
  342. *
  343. * If the current locale exists as a .mo file in the theme's root directory, it
  344. * will be included in the translated strings by the $domain.
  345. *
  346. * The .mo files must be named based on the locale exactly.
  347. *
  348. * @since 1.5.0
  349. *
  350. * @param string $domain Unique identifier for retrieving translated strings
  351. */
  352. function load_theme_textdomain($domain, $path = false) {
  353. $locale = get_locale();
  354. $path = ( empty( $path ) ) ? get_template_directory() : $path;
  355. $mofile = "$path/$locale.mo";
  356. return load_textdomain($domain, $mofile);
  357. }
  358. /**
  359. * Loads the child themes translated strings.
  360. *
  361. * If the current locale exists as a .mo file in the child themes root directory, it
  362. * will be included in the translated strings by the $domain.
  363. *
  364. * The .mo files must be named based on the locale exactly.
  365. *
  366. * @since 2.9.0
  367. *
  368. * @param string $domain Unique identifier for retrieving translated strings
  369. */
  370. function load_child_theme_textdomain($domain, $path = false) {
  371. $locale = get_locale();
  372. $path = ( empty( $path ) ) ? get_stylesheet_directory() : $path;
  373. $mofile = "$path/$locale.mo";
  374. return load_textdomain($domain, $mofile);
  375. }
  376. /**
  377. * Returns the Translations instance for a domain. If there isn't one,
  378. * returns empty Translations instance.
  379. *
  380. * @param string $domain
  381. * @return object A Translation instance
  382. */
  383. function &get_translations_for_domain( $domain ) {
  384. global $l10n;
  385. if ( !isset( $l10n[$domain] ) ) {
  386. $l10n[$domain] = &new NOOP_Translations;
  387. }
  388. return $l10n[$domain];
  389. }
  390. /**
  391. * Translates role name. Since the role names are in the database and
  392. * not in the source there are dummy gettext calls to get them into the POT
  393. * file and this function properly translates them back.
  394. *
  395. * The before_last_bar() call is needed, because older installs keep the roles
  396. * using the old context format: 'Role name|User role' and just skipping the
  397. * content after the last bar is easier than fixing them in the DB. New installs
  398. * won't suffer from that problem.
  399. */
  400. function translate_user_role( $name ) {
  401. return translate_with_gettext_context( before_last_bar($name), 'User role' );
  402. }
  403. ?>