3 * WordPress Translation API
10 * Gets the current locale.
12 * If the locale is set, then it will filter the locale in the 'locale' filter
13 * hook and return the value.
15 * If the locale is not set already, then the WPLANG 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.
19 * The process to get the locale should only be done once but the locale will
20 * always be filtered using the 'locale' hook.
23 * @uses apply_filters() Calls 'locale' hook on locale value.
24 * @uses $locale Gets the locale stored in the global.
26 * @return string The locale of the blog or from the 'locale' hook.
28 function get_locale() {
32 return apply_filters( 'locale', $locale );
34 // WPLANG is defined in wp-config.
35 if (defined('WPLANG'))
41 return apply_filters('locale', $locale);
45 * Retrieves the translation of $text. If there is no translation, or
46 * the domain isn't loaded the original text is returned.
48 * @see __() Don't use translate() directly, use __()
50 * @uses apply_filters() Calls 'gettext' on domain translated text
51 * with the untranslated text as second parameter.
53 * @param string $text Text to translate.
54 * @param string $domain Domain to retrieve the translated text.
55 * @return string Translated text
57 function translate( $text, $domain = 'default' ) {
58 $translations = &get_translations_for_domain( $domain );
59 return apply_filters('gettext', $translations->translate($text), $text, $domain);
62 function before_last_bar( $string ) {
63 $last_bar = strrpos( $string, '|' );
64 if ( false == $last_bar )
67 return substr( $string, 0, $last_bar );
71 * Translates $text like translate(), but assumes that the text
72 * contains a context after its last vertical bar.
77 * @param string $text Text to translate
78 * @param string $domain Domain to retrieve the translated text
79 * @return string Translated text
81 function translate_with_context( $text, $domain = 'default' ) {
82 return before_last_bar( translate( $text, $domain ) );
86 function translate_with_gettext_context( $text, $context, $domain = 'default' ) {
87 $translations = &get_translations_for_domain( $domain );
88 return apply_filters( 'gettext_with_context', $translations->translate( $text, $context ), $text, $context, $domain);
92 * Retrieves the translation of $text. If there is no translation, or
93 * the domain isn't loaded the original text is returned.
95 * @see translate() An alias of translate()
98 * @param string $text Text to translate
99 * @param string $domain Optional. Domain to retrieve the translated text
100 * @return string Translated text
102 function __( $text, $domain = 'default' ) {
103 return translate( $text, $domain );
107 * Retrieves the translation of $text and escapes it for safe use in an attribute.
108 * If there is no translation, or the domain isn't loaded the original text is returned.
110 * @see translate() An alias of translate()
114 * @param string $text Text to translate
115 * @param string $domain Optional. Domain to retrieve the translated text
116 * @return string Translated text
118 function esc_attr__( $text, $domain = 'default' ) {
119 return esc_attr( translate( $text, $domain ) );
123 * Retrieves the translation of $text and escapes it for safe use in HTML output.
124 * If there is no translation, or the domain isn't loaded the original text is returned.
126 * @see translate() An alias of translate()
130 * @param string $text Text to translate
131 * @param string $domain Optional. Domain to retrieve the translated text
132 * @return string Translated text
134 function esc_html__( $text, $domain = 'default' ) {
135 return esc_html( translate( $text, $domain ) );
139 * Displays the returned translated text from translate().
141 * @see translate() Echoes returned translate() string
144 * @param string $text Text to translate
145 * @param string $domain Optional. Domain to retrieve the translated text
147 function _e( $text, $domain = 'default' ) {
148 echo translate( $text, $domain );
152 * Displays translated text that has been escaped for safe use in an attribute.
154 * @see translate() Echoes returned translate() string
158 * @param string $text Text to translate
159 * @param string $domain Optional. Domain to retrieve the translated text
161 function esc_attr_e( $text, $domain = 'default' ) {
162 echo esc_attr( translate( $text, $domain ) );
166 * Displays translated text that has been escaped for safe use in HTML output.
168 * @see translate() Echoes returned translate() string
172 * @param string $text Text to translate
173 * @param string $domain Optional. Domain to retrieve the translated text
175 function esc_html_e( $text, $domain = 'default' ) {
176 echo esc_html( translate( $text, $domain ) );
180 * Retrieve translated string with vertical bar context
182 * Quite a few times, there will be collisions with similar translatable text
183 * found in more than two places but with different translated context.
185 * In order to use the separate contexts, the _c() function is used and the
186 * translatable string uses a pipe ('|') which has the context the string is in.
188 * When the translated string is returned, it is everything before the pipe, not
189 * including the pipe character. If there is no pipe in the translated text then
190 * everything is returned.
194 * @param string $text Text to translate
195 * @param string $domain Optional. Domain to retrieve the translated text
196 * @return string Translated context string without pipe
198 function _c($text, $domain = 'default') {
199 return translate_with_context($text, $domain);
202 function _x( $single, $context, $domain = 'default' ) {
203 return translate_with_gettext_context( $single, $context, $domain );
206 function esc_attr_x( $single, $context, $domain = 'default' ) {
207 return esc_attr( translate_with_gettext_context( $single, $context, $domain ) );
210 function __ngettext() {
211 _deprecated_function( __FUNCTION__, '2.8', '_n()' );
212 $args = func_get_args();
213 return call_user_func_array('_n', $args);
217 * Retrieve the plural or single form based on the amount.
219 * If the domain is not set in the $l10n list, then a comparison will be made
220 * and either $plural or $single parameters returned.
222 * If the domain does exist, then the parameters $single, $plural, and $number
223 * will first be passed to the domain's ngettext method. Then it will be passed
224 * to the 'ngettext' filter hook along with the same parameters. The expected
225 * type will be a string.
228 * @uses $l10n Gets list of domain translated string (gettext_reader) objects
229 * @uses apply_filters() Calls 'ngettext' hook on domains text returned,
230 * along with $single, $plural, and $number parameters. Expected to return string.
232 * @param string $single The text that will be used if $number is 1
233 * @param string $plural The text that will be used if $number is not 1
234 * @param int $number The number to compare against to use either $single or $plural
235 * @param string $domain Optional. The domain identifier the text should be retrieved in
236 * @return string Either $single or $plural translated text
238 function _n($single, $plural, $number, $domain = 'default') {
239 $translations = &get_translations_for_domain( $domain );
240 $translation = $translations->translate_plural( $single, $plural, $number );
241 return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
245 * @see _n() A version of _n(), which supports contexts --
246 * strips everything from the translation after the last bar
249 function _nc( $single, $plural, $number, $domain = 'default' ) {
250 return before_last_bar( _n( $single, $plural, $number, $domain ) );
253 function _nx($single, $plural, $number, $context, $domain = 'default') {
254 $translations = &get_translations_for_domain( $domain );
255 $translation = $translations->translate_plural( $single, $plural, $number, $context );
256 return apply_filters( 'ngettext_with_context ', $translation, $single, $plural, $number, $context, $domain );
260 * @deprecated Use _n_noop()
262 function __ngettext_noop() {
263 _deprecated_function( __FUNCTION__, '2.8', '_n_noop()' );
264 $args = func_get_args();
265 return call_user_func_array('_n_noop', $args);
270 * Register plural strings in POT file, but don't translate them.
272 * Used when you want do keep structures with translatable plural strings and
277 * 'post' => _n_noop('%s post', '%s posts'),
278 * 'page' => _n_noop('%s pages', '%s pages')
281 * $message = $messages[$type];
282 * $usable_text = sprintf(_n($message[0], $message[1], $count), $count);
285 * @param $single Single form to be i18ned
286 * @param $plural Plural form to be i18ned
287 * @return array array($single, $plural)
289 function _n_noop( $single, $plural ) {
290 return array( $single, $plural );
294 * Register plural strings with context in POT file, but don't translate them.
298 function _nx_noop( $single, $plural, $context ) {
299 return array( $single, $plural, $context );
304 * Loads a MO file into the domain $domain.
306 * If the domain already exists, the translations will be merged. If both
307 * sets have the same string, the translation from the original value will be taken.
309 * On success, the .mo file will be placed in the $l10n global by $domain
310 * and will be a MO object.
313 * @uses $l10n Gets list of domain translated string objects
315 * @param string $domain Unique identifier for retrieving translated strings
316 * @param string $mofile Path to the .mo file
317 * @return bool true on success, false on failure
319 function load_textdomain($domain, $mofile) {
322 if ( !is_readable( $mofile ) ) return false;
325 if ( !$mo->import_from_file( $mofile ) ) return false;
327 if ( isset( $l10n[$domain] ) )
328 $mo->merge_with( $l10n[$domain] );
330 $l10n[$domain] = &$mo;
335 * Loads default translated strings based on locale.
337 * Loads the .mo file in WP_LANG_DIR constant path from WordPress root. The
338 * translated (.mo) file is named based off of the locale.
342 function load_default_textdomain() {
343 $locale = get_locale();
345 $mofile = WP_LANG_DIR . "/$locale.mo";
347 return load_textdomain('default', $mofile);
351 * Loads the plugin's translated strings.
353 * If the path is not given then it will be the root of the plugin directory.
354 * The .mo file should be named based on the domain with a dash, and then the locale exactly.
358 * @param string $domain Unique identifier for retrieving translated strings
359 * @param string $abs_rel_path Optional. Relative path to ABSPATH of a folder,
360 * where the .mo file resides. Deprecated, but still functional until 2.7
361 * @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
363 function load_plugin_textdomain($domain, $abs_rel_path = false, $plugin_rel_path = false) {
364 $locale = get_locale();
366 if ( false !== $plugin_rel_path )
367 $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/');
368 else if ( false !== $abs_rel_path)
369 $path = ABSPATH . trim( $abs_rel_path, '/');
371 $path = WP_PLUGIN_DIR;
373 $mofile = $path . '/'. $domain . '-' . $locale . '.mo';
374 return load_textdomain($domain, $mofile);
378 * Loads the theme's translated strings.
380 * If the current locale exists as a .mo file in the theme's root directory, it
381 * will be included in the translated strings by the $domain.
383 * The .mo files must be named based on the locale exactly.
387 * @param string $domain Unique identifier for retrieving translated strings
389 function load_theme_textdomain($domain, $path = false) {
390 $locale = get_locale();
392 $path = ( empty( $path ) ) ? get_template_directory() : $path;
394 $mofile = "$path/$locale.mo";
395 return load_textdomain($domain, $mofile);
399 * Returns the Translations instance for a domain. If there isn't one,
400 * returns empty Translations instance.
402 * @param string $domain
403 * @return object A Translation instance
405 function &get_translations_for_domain( $domain ) {
407 $empty = &new Translations;
408 if ( isset($l10n[$domain]) )
409 return $l10n[$domain];
415 * Translates role name. Since the role names are in the database and
416 * not in the source there are dummy gettext calls to get them into the POT
417 * file and this function properly translates them back.
419 * The before_last_bar() call is needed, because older installs keep the roles
420 * using the old context format: 'Role name|User role' and just skipping the
421 * content after the last bar is easier than fixing them in the DB. New installs
422 * won't suffer from that problem.
424 function translate_user_role( $name ) {
425 return translate_with_gettext_context( before_last_bar($name), 'User role' );