/bp-forums/bbpress/bb-includes/functions.bb-l10n.php

  1. <?php 
  2. /** 
  3. * bbPress Translation API 
  4. * 
  5. * @package bbPress 
  6. * @subpackage i18n 
  7. */ 
  8.  
  9. /** 
  10. * Gets the current locale. 
  11. * 
  12. * If the locale is set, then it will filter the locale in the 'locale' filter 
  13. * hook and return the value. 
  14. * 
  15. * If the locale is not set already, then the BB_LANG constant is used if it is 
  16. * defined. Then it is filtered through the 'locale' filter hook and the value 
  17. * 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 'locale' hook. 
  21. * 
  22. * @since 0.7.2 
  23. * @uses apply_filters() Calls 'locale' hook on locale value. 
  24. * @uses $locale Gets the locale stored in the global. 
  25. * 
  26. * @return string The locale of the blog or from the 'locale' hook. 
  27. */ 
  28. function bb_get_locale() { 
  29. global $locale; 
  30.  
  31. if (isset($locale)) 
  32. return apply_filters( 'locale', $locale ); 
  33.  
  34. // BB_LANG is defined in bb-config.php 
  35. if (defined('BB_LANG')) 
  36. $locale = BB_LANG; 
  37.  
  38. if (empty($locale)) 
  39. $locale = 'en_US'; 
  40.  
  41. return apply_filters('locale', $locale); 
  42.  
  43. if ( !function_exists( 'get_locale' ) ) : 
  44. function get_locale() { 
  45. return bb_get_locale(); 
  46. endif; 
  47.  
  48. if ( !function_exists( 'translate' ) ) : 
  49. /** 
  50. * Retrieves the translation of $text. If there is no translation, or 
  51. * the domain isn't loaded the original text is returned. 
  52. * 
  53. * @see __() Don't use translate() directly, use __() 
  54. * @since 1.0 
  55. * @uses apply_filters() Calls 'gettext' on domain translated text 
  56. * with the untranslated text as second parameter. 
  57. * 
  58. * @param string $text Text to translate. 
  59. * @param string $domain Domain to retrieve the translated text. 
  60. * @return string Translated text 
  61. */ 
  62. function translate( $text, $domain = 'default' ) { 
  63. $translations = &get_translations_for_domain( $domain ); 
  64. return apply_filters('gettext', $translations->translate($text), $text, $domain); 
  65. endif; 
  66.  
  67. if ( !function_exists( 'before_last_bar' ) ) : 
  68. /** 
  69. * @since 1.0 
  70. */ 
  71. function before_last_bar( $string ) { 
  72. $last_bar = strrpos( $string, '|' ); 
  73. if ( false == $last_bar ) 
  74. return $string; 
  75. else 
  76. return substr( $string, 0, $last_bar ); 
  77. endif; 
  78.  
  79. if ( !function_exists( 'translate_with_context' ) ) : 
  80. /** 
  81. * Translate $text like translate(), but assumes that the text 
  82. * contains a context after its last vertical bar. 
  83. * 
  84. * @since 1.0 
  85. * @uses translate() 
  86. * 
  87. * @param string $text Text to translate 
  88. * @param string $domain Domain to retrieve the translated text 
  89. * @return string Translated text 
  90. */ 
  91. function translate_with_context( $text, $domain = 'default' ) { 
  92. return before_last_bar( translate( $text, $domain ) ); 
  93.  
  94. endif; 
  95.  
  96. if ( !function_exists( 'translate_with_gettext_context' ) ) : 
  97. /** 
  98. * @since 1.0 
  99. */ 
  100. function translate_with_gettext_context( $text, $context, $domain = 'default' ) { 
  101. $translations = &get_translations_for_domain( $domain ); 
  102. return apply_filters( 'gettext_with_context', $translations->translate( $text, $context ), $text, $context, $domain); 
  103. endif; 
  104.  
  105. if ( !function_exists( '__' ) ) : 
  106. /** 
  107. * Retrieves the translation of $text. If there is no translation, or 
  108. * the domain isn't loaded the original text is returned. 
  109. * 
  110. * @see translate() An alias of translate() 
  111. * @since 0.7.2 
  112. * 
  113. * @param string $text Text to translate 
  114. * @param string $domain Optional. Domain to retrieve the translated text 
  115. * @return string Translated text 
  116. */ 
  117. function __( $text, $domain = 'default' ) { 
  118. return translate( $text, $domain ); 
  119. endif; 
  120.  
  121. if ( !function_exists( 'esc_attr__' ) ) : 
  122. /** 
  123. * Retrieves the translation of $text and escapes it for safe use in an attribute. 
  124. * If there is no translation, or the domain isn't loaded the original text is returned. 
  125. * 
  126. * @see translate() An alias of translate() 
  127. * @see esc_attr() 
  128. * @since 1.0 
  129. * 
  130. * @param string $text Text to translate 
  131. * @param string $domain Optional. Domain to retrieve the translated text 
  132. * @return string Translated text 
  133. */ 
  134. function esc_attr__( $text, $domain = 'default' ) { 
  135. return esc_attr( translate( $text, $domain ) ); 
  136. endif; 
  137.  
  138. if ( !function_exists( 'esc_html__' ) ) : 
  139. /** 
  140. * Retrieves the translation of $text and escapes it for safe use in HTML output. 
  141. * If there is no translation, or the domain isn't loaded the original text is returned. 
  142. * 
  143. * @see translate() An alias of translate() 
  144. * @see esc_html() 
  145. * @since 1.0 
  146. * 
  147. * @param string $text Text to translate 
  148. * @param string $domain Optional. Domain to retrieve the translated text 
  149. * @return string Translated text 
  150. */ 
  151. function esc_html__( $text, $domain = 'default' ) { 
  152. return esc_html( translate( $text, $domain ) ); 
  153. endif; 
  154.  
  155. if ( !function_exists( '_e' ) ) : 
  156. /** 
  157. * Displays the returned translated text from translate(). 
  158. * 
  159. * @see translate() Echos returned translate() string 
  160. * @since 0.7.2 
  161. * 
  162. * @param string $text Text to translate 
  163. * @param string $domain Optional. Domain to retrieve the translated text 
  164. */ 
  165. function _e( $text, $domain = 'default' ) { 
  166. echo translate( $text, $domain ); 
  167. endif; 
  168.  
  169. if ( !function_exists( 'esc_attr_e' ) ) : 
  170. /** 
  171. * Displays translated text that has been escaped for safe use in an attribute. 
  172. * 
  173. * @see translate() Echoes returned translate() string 
  174. * @see esc_attr() 
  175. * @since 1.0 
  176. * 
  177. * @param string $text Text to translate 
  178. * @param string $domain Optional. Domain to retrieve the translated text 
  179. */ 
  180. function esc_attr_e( $text, $domain = 'default' ) { 
  181. echo esc_attr( translate( $text, $domain ) ); 
  182. endif; 
  183.  
  184. if ( !function_exists( 'esc_html_e' ) ) : 
  185. /** 
  186. * Displays translated text that has been escaped for safe use in HTML output. 
  187. * 
  188. * @see translate() Echoes returned translate() string 
  189. * @see esc_html() 
  190. * @since 1.0 
  191. * 
  192. * @param string $text Text to translate 
  193. * @param string $domain Optional. Domain to retrieve the translated text 
  194. */ 
  195. function esc_html_e( $text, $domain = 'default' ) { 
  196. echo esc_html( translate( $text, $domain ) ); 
  197. endif; 
  198.  
  199. if ( !function_exists( '_c' ) ) : 
  200. /** 
  201. * Retrieve translated string with vertical bar context 
  202. * 
  203. * Quite a few times, there will be collisions with similar translatable text 
  204. * found in more than two places but with different translated context. 
  205. * 
  206. * In order to use the separate contexts, the _c() function is used and the 
  207. * translatable string uses a pipe ('|') which has the context the string is in. 
  208. * 
  209. * When the translated string is returned, it is everything before the pipe, not 
  210. * including the pipe character. If there is no pipe in the translated text then 
  211. * everything is returned. 
  212. * 
  213. * @since 1.0 
  214. * 
  215. * @param string $text Text to translate 
  216. * @param string $domain Optional. Domain to retrieve the translated text 
  217. * @return string Translated context string without pipe 
  218. */ 
  219. function _c($text, $domain = 'default') { 
  220. return translate_with_context($text, $domain); 
  221. endif; 
  222.  
  223. if ( !function_exists( '_x' ) ) : 
  224. /** 
  225. * @since 1.0 
  226. */ 
  227. function _x( $single, $context, $domain = 'default' ) { 
  228. return translate_with_gettext_context( $single, $context, $domain ); 
  229. endif; 
  230.  
  231. if ( !function_exists( 'esc_attr_x' ) ) : 
  232. function esc_attr_x( $single, $context, $domain = 'default' ) { 
  233. return esc_attr( translate_with_gettext_context( $single, $context, $domain ) ); 
  234. endif; 
  235.  
  236. if ( !function_exists( '__ngettext' ) ) : 
  237. /** 
  238. * @deprecated Use _n() 
  239. */ 
  240. function __ngettext() { 
  241. //_deprecated_function( __FUNCTION__, '2.8', '_n()' ); 
  242. $args = func_get_args(); 
  243. return call_user_func_array('_n', $args); 
  244. endif; 
  245.  
  246. if ( !function_exists( '_n' ) ) : 
  247. /** 
  248. * Retrieve the plural or single form based on the amount. 
  249. * 
  250. * If the domain is not set in the $l10n list, then a comparsion will be made 
  251. * and either $plural or $single parameters returned. 
  252. * 
  253. * If the domain does exist, then the parameters $single, $plural, and $number 
  254. * will first be passed to the domain's ngettext method. Then it will be passed 
  255. * to the 'ngettext' filter hook along with the same parameters. The expected 
  256. * type will be a string. 
  257. * 
  258. * @since 1.0 
  259. * @uses $l10n Gets list of domain translated string (gettext_reader) objects 
  260. * @uses apply_filters() Calls 'ngettext' hook on domains text returned,  
  261. * along with $single, $plural, and $number parameters. Expected to return string. 
  262. * 
  263. * @param string $single The text that will be used if $number is 1 
  264. * @param string $plural The text that will be used if $number is not 1 
  265. * @param int $number The number to compare against to use either $single or $plural 
  266. * @param string $domain Optional. The domain identifier the text should be retrieved in 
  267. * @return string Either $single or $plural translated text 
  268. */ 
  269. function _n($single, $plural, $number, $domain = 'default') { 
  270. $translations = &get_translations_for_domain( $domain ); 
  271. $translation = $translations->translate_plural( $single, $plural, $number ); 
  272. return apply_filters( 'ngettext', $translation, $single, $plural, $number ); 
  273. endif; 
  274.  
  275. if ( !function_exists( '_nc' ) ) : 
  276. /** 
  277. * @see _n() A version of _n(), which supports contexts -- 
  278. * strips everything from the translation after the last bar 
  279. * @since 1.0 
  280. */ 
  281. function _nc( $single, $plural, $number, $domain = 'default' ) { 
  282. return before_last_bar( _n( $single, $plural, $number, $domain ) ); 
  283. endif; 
  284.  
  285. if ( !function_exists( '_nx' ) ) : 
  286. /** 
  287. * @since 1.0 
  288. */ 
  289. function _nx($single, $plural, $number, $context, $domain = 'default') { 
  290. $translations = &get_translations_for_domain( $domain ); 
  291. $translation = $translations->translate_plural( $single, $plural, $number, $context ); 
  292. return apply_filters( 'ngettext_with_context ', $translation, $single, $plural, $number, $context ); 
  293. endif; 
  294.  
  295. if ( !function_exists( '__ngettext_noop' ) ) : 
  296. /** 
  297. * @deprecated Use _n_noop() 
  298. */ 
  299. function __ngettext_noop() { 
  300. //_deprecated_function( __FUNCTION__, '2.8', '_n_noop()' ); 
  301. $args = func_get_args(); 
  302. return call_user_func_array('_n_noop', $args); 
  303. endif; 
  304.  
  305. if ( !function_exists( '_n_noop' ) ) : 
  306. /** 
  307. * Register plural strings in POT file, but don't translate them. 
  308. * 
  309. * Used when you want do keep structures with translatable plural strings and 
  310. * use them later. 
  311. * 
  312. * Example: 
  313. * $messages = array( 
  314. * 'post' => _n_noop('%s post', '%s posts'),  
  315. * 'page' => _n_noop('%s pages', '%s pages') 
  316. * ); 
  317. * ... 
  318. * $message = $messages[$type]; 
  319. * $usable_text = sprintf(_n($message[0], $message[1], $count), $count); 
  320. * 
  321. * @since 1.0 
  322. * @param $single Single form to be i18ned 
  323. * @param $plural Plural form to be i18ned 
  324. * @return array array($single, $plural) 
  325. */ 
  326. function _n_noop( $single, $plural ) { 
  327. return array( $single, $plural ); 
  328. endif; 
  329.  
  330. if ( !function_exists( '_nx_noop' ) ) : 
  331. /** 
  332. * Register plural strings with context in POT file, but don't translate them. 
  333. * 
  334. * @see _n_noop() 
  335. */ 
  336. function _nx_noop( $single, $plural, $context ) { 
  337. return array( $single, $plural, $context ); 
  338. endif; 
  339.  
  340. if ( !function_exists( 'load_textdomain' ) ) : 
  341. /** 
  342. * Loads MO file into the list of domains. 
  343. * 
  344. * If the domain already exists, the inclusion will fail. If the MO file is not 
  345. * readable, the inclusion will fail. 
  346. * 
  347. * On success, the mofile will be placed in the $l10n global by $domain and will 
  348. * be an gettext_reader object. 
  349. * 
  350. * @since 0.7.2 
  351. * @uses $l10n Gets list of domain translated string (gettext_reader) objects 
  352. * @uses CacheFileReader Reads the MO file 
  353. * @uses gettext_reader Allows for retrieving translated strings 
  354. * 
  355. * @param string $domain Unique identifier for retrieving translated strings 
  356. * @param string $mofile Path to the .mo file 
  357. * @return null On failure returns null and also on success returns nothing. 
  358. */ 
  359. function load_textdomain($domain, $mofile) { 
  360. global $l10n; 
  361.  
  362. if ( !is_readable($mofile)) return; 
  363.  
  364. $mo = new MO(); 
  365. $mo->import_from_file( $mofile ); 
  366.  
  367. if (isset($l10n[$domain])) 
  368. $mo->merge_with( $l10n[$domain] ); 
  369.  
  370. $l10n[$domain] = &$mo; 
  371. endif; 
  372.  
  373. /** 
  374. * Loads default translated strings based on locale. 
  375. * 
  376. * Loads the .mo file in WP_LANG_DIR constant path from WordPress root. The 
  377. * translated (.mo) file is named based off of the locale. 
  378. * 
  379. * @since 1.5.0 
  380. */ 
  381. function bb_load_default_textdomain() { 
  382. $locale = bb_get_locale(); 
  383.  
  384. $mofile = BB_LANG_DIR . "/$locale.mo"; 
  385.  
  386. load_textdomain('default', $mofile); 
  387.  
  388. if ( !function_exists( 'load_default_textdomain' ) ) : 
  389. function load_default_textdomain() { 
  390. bb_load_default_textdomain(); 
  391. endif; 
  392.  
  393. /** 
  394. * Loads the plugin's translated strings. 
  395. * 
  396. * If the path is not given then it will be the root of the plugin directory. 
  397. * The .mo file should be named based on the domain with a dash followed by a 
  398. * dash, and then the locale exactly. 
  399. * 
  400. * @since 0.7.2 
  401. * 
  402. * @param string $domain Unique identifier for retrieving translated strings 
  403. * @param string $path Optional. Absolute path to folder where the .mo file resides 
  404. */ 
  405. function bb_load_plugin_textdomain($domain, $path = false) { 
  406. $locale = bb_get_locale(); 
  407.  
  408. if ( false === $path ) { 
  409. global $bb; 
  410. $path = $bb->plugin_locations['core']['dir']; 
  411.  
  412. $mofile = rtrim( trim( $path ), " \t\n\r\0\x0B/" ) . '/'. $domain . '-' . $locale . '.mo'; 
  413. load_textdomain($domain, $mofile); 
  414.  
  415. if ( !function_exists( 'load_plugin_textdomain' ) ) : 
  416. function load_plugin_textdomain( $domain, $path = false ) { 
  417. bb_load_plugin_textdomain( $domain, $path ); 
  418. endif; 
  419.  
  420. /** 
  421. * Loads the theme's translated strings. 
  422. * 
  423. * If the current locale exists as a .mo file in the theme's root directory, it 
  424. * will be included in the translated strings by the $domain. 
  425. * 
  426. * The .mo files must be named based on the locale exactly. 
  427. * 
  428. * @since 0.7.2 
  429. * 
  430. * @param string $domain Unique identifier for retrieving translated strings 
  431. */ 
  432. function bb_load_theme_textdomain($domain, $path = false) { 
  433. $locale = bb_get_locale(); 
  434.  
  435. $mofile = ( empty( $path ) ) ? bb_get_template( $locale . '.mo' ) : "$path/$locale.mo"; 
  436.  
  437. load_textdomain($domain, $mofile); 
  438.  
  439. if ( !function_exists( 'load_theme_textdomain' ) ) : 
  440. function load_theme_textdomain( $domain, $path = false ) { 
  441. bb_load_theme_textdomain( $domain, $path ); 
  442. endif; 
  443.  
  444. if ( !function_exists( 'get_translations_for_domain' ) ) : 
  445. /** 
  446. * Returns the Translations instance for a domain. If there isn't one,  
  447. * returns empty Translations instance. 
  448. * 
  449. * @param string $domain 
  450. * @return object A Translation instance 
  451. */ 
  452. function &get_translations_for_domain( $domain ) { 
  453. global $l10n; 
  454. $empty = new Translations; 
  455. if ( isset($l10n[$domain]) ) 
  456. return $l10n[$domain]; 
  457. else 
  458. return $empty; 
  459. endif; 
.