PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Modifications Since 08/www/wp-includes/l10n.php

https://github.com/holsinger/openfloor
PHP | 343 lines | 101 code | 35 blank | 207 comment | 15 complexity | b1069213f15143f4189ded907363690e 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. $locale = apply_filters('locale', $locale);
  37. return $locale;
  38. }
  39. /**
  40. * Retrieve the translated text.
  41. *
  42. * If the domain is set in the $l10n global, then the text is run through the
  43. * domain's translate method. After it is passed to the 'gettext' filter hook,
  44. * along with the untranslated text as the second parameter.
  45. *
  46. * If the domain is not set, the $text is just returned.
  47. *
  48. * @since 2.2.0
  49. * @uses $l10n Gets list of domain translated string (gettext_reader) objects.
  50. * @uses apply_filters() Calls 'gettext' on domain translated text
  51. * with the untranslated text as second parameter.
  52. *
  53. * @param string $text Text to translate.
  54. * @param string $domain Domain to retrieve the translated text.
  55. * @return string Translated text
  56. */
  57. function translate($text, $domain = 'default') {
  58. global $l10n;
  59. if (isset($l10n[$domain]))
  60. return apply_filters('gettext', $l10n[$domain]->translate($text), $text, $domain);
  61. else
  62. return apply_filters('gettext', $text, $text, $domain);
  63. }
  64. function before_last_bar( $string ) {
  65. $last_bar = strrpos( $string, '|' );
  66. if ( false == $last_bar )
  67. return $string;
  68. else
  69. return substr( $string, 0, $last_bar );
  70. }
  71. /**
  72. * Retrieve the translated text and strip context.
  73. *
  74. * If the domain is set in the $l10n global, then the text is run through the
  75. * domain's translate method. After it is passed to the 'gettext' filter hook,
  76. * along with the untranslated text as the second parameter.
  77. *
  78. * If the domain is not set, the $text is just returned.
  79. *
  80. * @since 2.5
  81. * @uses translate()
  82. *
  83. * @param string $text Text to translate
  84. * @param string $domain Domain to retrieve the translated text
  85. * @return string Translated text
  86. */
  87. function translate_with_context( $text, $domain = 'default' ) {
  88. return before_last_bar( translate( $text, $domain ) );
  89. }
  90. /**
  91. * Retrieves the translated string from the translate().
  92. *
  93. * @see translate() An alias of translate()
  94. * @since 2.1.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 __($text, $domain = 'default') {
  101. return translate($text, $domain);
  102. }
  103. /**
  104. * Displays the returned translated text from translate().
  105. *
  106. * @see translate() Echos returned translate() string
  107. * @since 1.2.0
  108. *
  109. * @param string $text Text to translate
  110. * @param string $domain Optional. Domain to retrieve the translated text
  111. */
  112. function _e($text, $domain = 'default') {
  113. echo translate($text, $domain);
  114. }
  115. /**
  116. * Retrieve context translated string.
  117. *
  118. * Quite a few times, there will be collisions with similar translatable text
  119. * found in more than two places but with different translated context.
  120. *
  121. * In order to use the separate contexts, the _c() function is used and the
  122. * translatable string uses a pipe ('|') which has the context the string is in.
  123. *
  124. * When the translated string is returned, it is everything before the pipe, not
  125. * including the pipe character. If there is no pipe in the translated text then
  126. * everything is returned.
  127. *
  128. * @since 2.2.0
  129. *
  130. * @param string $text Text to translate
  131. * @param string $domain Optional. Domain to retrieve the translated text
  132. * @return string Translated context string without pipe
  133. */
  134. function _c($text, $domain = 'default') {
  135. return translate_with_context($text, $domain);
  136. }
  137. /**
  138. * Retrieve the plural or single form based on the amount.
  139. *
  140. * If the domain is not set in the $l10n list, then a comparsion will be made
  141. * and either $plural or $single parameters returned.
  142. *
  143. * If the domain does exist, then the parameters $single, $plural, and $number
  144. * will first be passed to the domain's ngettext method. Then it will be passed
  145. * to the 'ngettext' filter hook along with the same parameters. The expected
  146. * type will be a string.
  147. *
  148. * @since 1.2.0
  149. * @uses $l10n Gets list of domain translated string (gettext_reader) objects
  150. * @uses apply_filters() Calls 'ngettext' hook on domains text returned,
  151. * along with $single, $plural, and $number parameters. Expected to return string.
  152. *
  153. * @param string $single The text that will be used if $number is 1
  154. * @param string $plural The text that will be used if $number is not 1
  155. * @param int $number The number to compare against to use either $single or $plural
  156. * @param string $domain Optional. The domain identifier the text should be retrieved in
  157. * @return string Either $single or $plural translated text
  158. */
  159. function __ngettext($single, $plural, $number, $domain = 'default') {
  160. global $l10n;
  161. if (isset($l10n[$domain])) {
  162. return apply_filters('ngettext', $l10n[$domain]->ngettext($single, $plural, $number), $single, $plural, $number);
  163. } else {
  164. if ($number != 1)
  165. return $plural;
  166. else
  167. return $single;
  168. }
  169. }
  170. /**
  171. * @see __ngettext() An alias of __ngettext
  172. *
  173. */
  174. function _n() {
  175. $args = func_get_args();
  176. return call_user_func_array('__ngettext', $args);
  177. }
  178. /**
  179. * @see _n() A version of _n(), which supports contexts --
  180. * strips everything from the translation after the last bar
  181. *
  182. */
  183. function _nc( $single, $plural, $number, $domain = 'default' ) {
  184. return before_last_bar( __ngettext( $single, $plural, $number, $domain ) );
  185. }
  186. /**
  187. * Register plural strings in POT file, but don't translate them.
  188. *
  189. * Used when you want do keep structures with translatable plural strings and
  190. * use them later.
  191. *
  192. * Example:
  193. * $messages = array(
  194. * 'post' => ngettext_noop('%s post', '%s posts'),
  195. * 'page' => ngettext_noop('%s pages', '%s pages')
  196. * );
  197. * ...
  198. * $message = $messages[$type];
  199. * $usable_text = sprintf(__ngettext($message[0], $message[1], $count), $count);
  200. *
  201. * @since 2.5
  202. * @param $single Single form to be i18ned
  203. * @param $plural Plural form to be i18ned
  204. * @param $number Not used, here for compatibility with __ngettext, optional
  205. * @param $domain Not used, here for compatibility with __ngettext, optional
  206. * @return array array($single, $plural)
  207. */
  208. function __ngettext_noop($single, $plural, $number=1, $domain = 'default') {
  209. return array($single, $plural);
  210. }
  211. /**
  212. * @see __ngettext_noop() An alias of __ngettext_noop()
  213. *
  214. */
  215. function _n_noop() {
  216. $args = func_get_args();
  217. return call_user_func_array('__ngettext_noop', $args);
  218. }
  219. /**
  220. * Loads MO file into the list of domains.
  221. *
  222. * If the domain already exists, the inclusion will fail. If the MO file is not
  223. * readable, the inclusion will fail.
  224. *
  225. * On success, the mofile will be placed in the $l10n global by $domain and will
  226. * be an gettext_reader object.
  227. *
  228. * @since 1.5.0
  229. * @uses $l10n Gets list of domain translated string (gettext_reader) objects
  230. * @uses CacheFileReader Reads the MO file
  231. * @uses gettext_reader Allows for retrieving translated strings
  232. *
  233. * @param string $domain Unique identifier for retrieving translated strings
  234. * @param string $mofile Path to the .mo file
  235. * @return null On failure returns null and also on success returns nothing.
  236. */
  237. function load_textdomain($domain, $mofile) {
  238. global $l10n;
  239. if ( is_readable($mofile))
  240. $input = new CachedFileReader($mofile);
  241. else
  242. return;
  243. $gettext = new gettext_reader($input);
  244. if (isset($l10n[$domain])) {
  245. $l10n[$domain]->load_tables();
  246. $gettext->load_tables();
  247. $l10n[$domain]->cache_translations = array_merge($gettext->cache_translations, $l10n[$domain]->cache_translations);
  248. } else
  249. $l10n[$domain] = $gettext;
  250. unset($input, $gettext);
  251. }
  252. /**
  253. * Loads default translated strings based on locale.
  254. *
  255. * Loads the .mo file in WP_LANG_DIR constant path from WordPress root. The
  256. * translated (.mo) file is named based off of the locale.
  257. *
  258. * @since 1.5.0
  259. */
  260. function load_default_textdomain() {
  261. $locale = get_locale();
  262. $mofile = WP_LANG_DIR . "/$locale.mo";
  263. load_textdomain('default', $mofile);
  264. }
  265. /**
  266. * Loads the plugin's translated strings.
  267. *
  268. * If the path is not given then it will be the root of the plugin directory.
  269. * The .mo file should be named based on the domain with a dash followed by a
  270. * dash, and then the locale exactly.
  271. *
  272. * @since 1.5.0
  273. *
  274. * @param string $domain Unique identifier for retrieving translated strings
  275. * @param string $abs_rel_path Optional. Relative path to ABSPATH of a folder,
  276. * where the .mo file resides. Deprecated, but still functional until 2.7
  277. * @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
  278. */
  279. function load_plugin_textdomain($domain, $abs_rel_path = false, $plugin_rel_path = false) {
  280. $locale = get_locale();
  281. if ( false !== $plugin_rel_path )
  282. $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/');
  283. else if ( false !== $abs_rel_path)
  284. $path = ABSPATH . trim( $abs_rel_path, '/');
  285. else
  286. $path = WP_PLUGIN_DIR;
  287. $mofile = $path . '/'. $domain . '-' . $locale . '.mo';
  288. load_textdomain($domain, $mofile);
  289. }
  290. /**
  291. * Loads the theme's translated strings.
  292. *
  293. * If the current locale exists as a .mo file in the theme's root directory, it
  294. * will be included in the translated strings by the $domain.
  295. *
  296. * The .mo files must be named based on the locale exactly.
  297. *
  298. * @since 1.5.0
  299. *
  300. * @param string $domain Unique identifier for retrieving translated strings
  301. */
  302. function load_theme_textdomain($domain, $path = false) {
  303. $locale = get_locale();
  304. $path = ( empty( $path ) ) ? get_template_directory() : $path;
  305. $mofile = "$path/$locale.mo";
  306. load_textdomain($domain, $mofile);
  307. }
  308. ?>