3 * WordPress Translation API
10 * get_locale() - Gets the current locale
12 * If the locale is set, then it will filter the locale
13 * in the 'locale' filter hook and return the value.
15 * If the locale is not set already, then the WPLANG
16 * constant is used if it is defined. Then it is filtered
17 * through the 'locale' filter hook and the value for the
18 * locale global set and the locale is returned.
20 * The process to get the locale should only be done once
21 * but the locale will always be filtered using the
25 * @uses apply_filters() Calls 'locale' hook on locale value
26 * @uses $locale Gets the locale stored in the global
28 * @return string The locale of the blog or from the 'locale' hook
30 function get_locale() {
34 return apply_filters( 'locale', $locale );
36 // WPLANG is defined in wp-config.
37 if (defined('WPLANG'))
43 $locale = apply_filters('locale', $locale);
49 * translate() - Retrieve the translated text
51 * If the domain is set in the $l10n global, then the text is run
52 * through the domain's translate method. After it is passed to
53 * the 'gettext' filter hook, along with the untranslated text as
54 * the second parameter.
56 * If the domain is not set, the $text is just returned.
59 * @uses $l10n Gets list of domain translated string (gettext_reader) objects
60 * @uses apply_filters() Calls 'gettext' on domain translated text
61 * with the untranslated text as second parameter
63 * @param string $text Text to translate
64 * @param string $domain Domain to retrieve the translated text
65 * @return string Translated text
67 function translate($text, $domain = 'default') {
70 if (isset($l10n[$domain]))
71 return apply_filters('gettext', $l10n[$domain]->translate($text), $text);
77 * translate_with_context() - Retrieve the translated text and strip context
79 * If the domain is set in the $l10n global, then the text is run
80 * through the domain's translate method. After it is passed to
81 * the 'gettext' filter hook, along with the untranslated text as
82 * the second parameter.
84 * If the domain is not set, the $text is just returned.
89 * @param string $text Text to translate
90 * @param string $domain Domain to retrieve the translated text
91 * @return string Translated text
93 function translate_with_context($text, $domain = 'default') {
94 $whole = translate($text, $domain);
95 $last_bar = strrpos($whole, '|');
96 if ( false == $last_bar ) {
99 return substr($whole, 0, $last_bar);
104 * __() - Retrieve a translated string
106 * __() is a convenience function which retrieves the translated
107 * string from the translate().
109 * @see translate() An alias of translate()
112 * @param string $text Text to translate
113 * @param string $domain Optional. Domain to retrieve the translated text
114 * @return string Translated text
116 function __($text, $domain = 'default') {
117 return translate($text, $domain);
122 * _e() - Display a translated string
124 * _e() is a convenience function which displays the returned
125 * translated text from translate().
127 * @see translate() Echos returned translate() string
130 * @param string $text Text to translate
131 * @param string $domain Optional. Domain to retrieve the translated text
133 function _e($text, $domain = 'default') {
134 echo translate($text, $domain);
138 * _c() - Retrieve context translated string
140 * Quite a few times, there will be collisions with similar
141 * translatable text found in more than two places but with
142 * different translated context.
144 * In order to use the separate contexts, the _c() function
145 * is used and the translatable string uses a pipe ('|')
146 * which has the context the string is in.
148 * When the translated string is returned, it is everything
149 * before the pipe, not including the pipe character. If
150 * there is no pipe in the translated text then everything
155 * @param string $text Text to translate
156 * @param string $domain Optional. Domain to retrieve the translated text
157 * @return string Translated context string without pipe
159 function _c($text, $domain = 'default') {
160 return translate_with_context($text, $domain);
164 * __ngettext() - Retrieve the plural or single form based on the amount
166 * If the domain is not set in the $l10n list, then a comparsion
167 * will be made and either $plural or $single parameters returned.
169 * If the domain does exist, then the parameters $single, $plural,
170 * and $number will first be passed to the domain's ngettext method.
171 * Then it will be passed to the 'ngettext' filter hook along with
172 * the same parameters. The expected type will be a string.
175 * @uses $l10n Gets list of domain translated string (gettext_reader) objects
176 * @uses apply_filters() Calls 'ngettext' hook on domains text returned,
177 * along with $single, $plural, and $number parameters. Expected to return string.
179 * @param string $single The text that will be used if $number is 1
180 * @param string $plural The text that will be used if $number is not 1
181 * @param int $number The number to compare against to use either $single or $plural
182 * @param string $domain Optional. The domain identifier the text should be retrieved in
183 * @return string Either $single or $plural translated text
185 function __ngettext($single, $plural, $number, $domain = 'default') {
188 if (isset($l10n[$domain])) {
189 return apply_filters('ngettext', $l10n[$domain]->ngettext($single, $plural, $number), $single, $plural, $number);
199 * __ngettext_noop() - register plural strings in POT file, but don't translate them
201 * Used when you want do keep structures with translatable plural strings and
206 * 'post' => ngettext_noop('%s post', '%s posts'),
207 * 'page' => ngettext_noop('%s pages', '%s pages')
210 * $message = $messages[$type];
211 * $usable_text = sprintf(__ngettext($message[0], $message[1], $count), $count);
214 * @param $single Single form to be i18ned
215 * @param $plural Plural form to be i18ned
216 * @param $number Not used, here for compatibility with __ngettext, optional
217 * @param $domain Not used, here for compatibility with __ngettext, optional
218 * @return array array($single, $plural)
220 function __ngettext_noop($single, $plural, $number=1, $domain = 'default') {
221 return array($single, $plural);
225 * load_textdomain() - Loads MO file into the list of domains
227 * If the domain already exists, the inclusion will fail. If the
228 * MO file is not readable, the inclusion will fail.
230 * On success, the mofile will be placed in the $l10n global by
231 * $domain and will be an gettext_reader object.
234 * @uses $l10n Gets list of domain translated string (gettext_reader) objects
235 * @uses CacheFileReader Reads the MO file
236 * @uses gettext_reader Allows for retrieving translated strings
238 * @param string $domain Unique identifier for retrieving translated strings
239 * @param string $mofile Path to the .mo file
240 * @return null On failure returns null and also on success returns nothing.
242 function load_textdomain($domain, $mofile) {
245 if ( is_readable($mofile))
246 $input = new CachedFileReader($mofile);
250 $gettext = new gettext_reader($input);
252 if (isset($l10n[$domain])) {
253 $l10n[$domain]->load_tables();
254 $gettext->load_tables();
255 $l10n[$domain]->cache_translations = array_merge($gettext->cache_translations, $l10n[$domain]->cache_translations);
257 $l10n[$domain] = $gettext;
259 unset($input, $gettext);
263 * load_default_textdomain() - Loads default translated strings based on locale
265 * Loads the .mo file in WP_LANG_DIR constant path from WordPress root.
266 * The translated (.mo) file is named based off of the locale.
270 function load_default_textdomain() {
271 $locale = get_locale();
273 $mofile = WP_LANG_DIR . "/$locale.mo";
275 load_textdomain('default', $mofile);
279 * load_plugin_textdomain() - Loads the plugin's translated strings
281 * If the path is not given then it will be the root of the plugin
282 * directory. The .mo file should be named based on the domain with a
283 * dash followed by a dash, and then the locale exactly.
287 * @param string $domain Unique identifier for retrieving translated strings
288 * @param string $abs_rel_path Optional. Relative path to ABSPATH of a folder,
289 * where the .mo file resides. Deprecated, but still functional until 2.7
290 * @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
292 function load_plugin_textdomain($domain, $abs_rel_path = false, $plugin_rel_path = false) {
293 $locale = get_locale();
295 if ( false !== $plugin_rel_path )
296 $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/');
297 else if ( false !== $abs_rel_path)
298 $path = ABSPATH . trim( $abs_rel_path, '/');
300 $path = WP_PLUGIN_DIR;
302 $mofile = $path . '/'. $domain . '-' . $locale . '.mo';
303 load_textdomain($domain, $mofile);
307 * load_theme_textdomain() - Includes theme's translated strings for the theme
309 * If the current locale exists as a .mo file in the theme's root directory, it
310 * will be included in the translated strings by the $domain.
312 * The .mo files must be named based on the locale exactly.
316 * @param string $domain Unique identifier for retrieving translated strings
318 function load_theme_textdomain($domain) {
319 $locale = get_locale();
321 $mofile = get_template_directory() . "/$locale.mo";
322 load_textdomain($domain, $mofile);