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