]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/l10n.php
Wordpress 2.5.1-scripts
[autoinstalls/wordpress.git] / wp-includes / l10n.php
1 <?php
2 /**
3  * WordPress Translation API
4  *
5  * @package WordPress
6  * @subpackage i18n
7  */
8
9 /**
10  * get_locale() - Gets the current locale
11  *
12  * If the locale is set, then it will filter the locale
13  * in the 'locale' filter hook and return the value.
14  *
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.
19  *
20  * The process to get the locale should only be done once
21  * but the locale will always be filtered using the
22  * 'locale' hook.
23  *
24  * @since 1.5.0
25  * @uses apply_filters() Calls 'locale' hook on locale value
26  * @uses $locale Gets the locale stored in the global
27  *
28  * @return string The locale of the blog or from the 'locale' hook
29  */
30 function get_locale() {
31         global $locale;
32
33         if (isset($locale))
34                 return apply_filters( 'locale', $locale );
35
36         // WPLANG is defined in wp-config.
37         if (defined('WPLANG'))
38                 $locale = WPLANG;
39
40         if (empty($locale))
41                 $locale = '';
42
43         $locale = apply_filters('locale', $locale);
44
45         return $locale;
46 }
47
48 /**
49  * translate() - Retrieve the translated text
50  *
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.
55  *
56  * If the domain is not set, the $text is just returned.
57  *
58  * @since 2.2.0
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
62  *
63  * @param string $text Text to translate
64  * @param string $domain Domain to retrieve the translated text
65  * @return string Translated text
66  */
67 function translate($text, $domain = 'default') {
68         global $l10n;
69
70         if (isset($l10n[$domain]))
71                 return apply_filters('gettext', $l10n[$domain]->translate($text), $text);
72         else
73                 return $text;
74 }
75
76 /**
77  * translate_with_context() - Retrieve the translated text and strip context
78  *
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.
83  *
84  * If the domain is not set, the $text is just returned.
85  *
86  * @since 2.5
87  * @uses translate()
88  *
89  * @param string $text Text to translate
90  * @param string $domain Domain to retrieve the translated text
91  * @return string Translated text
92  */
93 function translate_with_context($text, $domain = 'default') {
94         $whole = translate($text, $domain);
95         $last_bar = strrpos($whole, '|');
96         if ( false == $last_bar ) {
97                 return $whole;
98         } else {
99                 return substr($whole, 0, $last_bar);
100         }
101 }
102
103 /**
104  * __() - Retrieve a translated string
105  *
106  * __() is a convenience function which retrieves the translated
107  * string from the translate().
108  *
109  * @see translate() An alias of translate()
110  * @since 2.1.0
111  *
112  * @param string $text Text to translate
113  * @param string $domain Optional. Domain to retrieve the translated text
114  * @return string Translated text
115  */
116 function __($text, $domain = 'default') {
117         return translate($text, $domain);
118 }
119
120 // .
121 /**
122  * _e() - Display a translated string
123  *
124  * _e() is a convenience function which displays the returned
125  * translated text from translate().
126  *
127  * @see translate() Echos returned translate() string
128  * @since 1.2.0
129  *
130  * @param string $text Text to translate
131  * @param string $domain Optional. Domain to retrieve the translated text
132  */
133 function _e($text, $domain = 'default') {
134         echo translate($text, $domain);
135 }
136
137 /**
138  * _c() - Retrieve context translated string
139  *
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.
143  *
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.
147  *
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
151  * is returned.
152  *
153  * @since 2.2.0
154  *
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
158  */
159 function _c($text, $domain = 'default') {
160         return translate_with_context($text, $domain);
161 }
162
163 /**
164  * __ngettext() - Retrieve the plural or single form based on the amount
165  *
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.
168  *
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.
173  *
174  * @since 1.2.0
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.
178  *
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
184  */
185 function __ngettext($single, $plural, $number, $domain = 'default') {
186         global $l10n;
187
188         if (isset($l10n[$domain])) {
189                 return apply_filters('ngettext', $l10n[$domain]->ngettext($single, $plural, $number), $single, $plural, $number);
190         } else {
191                 if ($number != 1)
192                         return $plural;
193                 else
194                         return $single;
195         }
196 }
197
198 /**
199  * __ngettext_noop() - register plural strings in POT file, but don't translate them
200  *
201  * Used when you want do keep structures with translatable plural strings and
202  * use them later.
203  *
204  * Example:
205  *  $messages = array(
206  *      'post' => ngettext_noop('%s post', '%s posts'),
207  *      'page' => ngettext_noop('%s pages', '%s pages')
208  *  );
209  *  ...
210  *  $message = $messages[$type];
211  *  $usable_text = sprintf(__ngettext($message[0], $message[1], $count), $count);
212  *
213  * @since 2.5
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)
219  */
220 function __ngettext_noop($single, $plural, $number=1, $domain = 'default') {
221         return array($single, $plural);
222 }
223
224 /**
225  * load_textdomain() - Loads MO file into the list of domains
226  *
227  * If the domain already exists, the inclusion will fail. If the
228  * MO file is not readable, the inclusion will fail.
229  *
230  * On success, the mofile will be placed in the $l10n global by
231  * $domain and will be an gettext_reader object.
232  *
233  * @since 1.5.0
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
237  *
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.
241  */
242 function load_textdomain($domain, $mofile) {
243         global $l10n;
244
245         if (isset($l10n[$domain]))
246                 return;
247
248         if ( is_readable($mofile))
249                 $input = new CachedFileReader($mofile);
250         else
251                 return;
252
253         $l10n[$domain] = new gettext_reader($input);
254 }
255
256 /**
257  * load_default_textdomain() - Loads default translated strings based on locale
258  *
259  * Loads the .mo file in LANGDIR constant path from WordPress root.
260  * The translated (.mo) file is named based off of the locale.
261  *
262  * @since 1.5.0
263  */
264 function load_default_textdomain() {
265         $locale = get_locale();
266         if ( empty($locale) )
267                 $locale = 'en_US';
268
269         $mofile = ABSPATH . LANGDIR . "/$locale.mo";
270
271         load_textdomain('default', $mofile);
272 }
273
274 /**
275  * load_plugin_textdomain() - Loads the plugin's translated strings
276  *
277  * If the path is not given then it will be the root of the plugin
278  * directory. The .mo file should be named based on the domain with a
279  * dash followed by a dash, and then the locale exactly.
280  *
281  * The plugin may place all of the .mo files in another folder and set
282  * the $path based on the relative location from ABSPATH constant. The
283  * plugin may use the constant PLUGINDIR and/or plugin_basename() to
284  * get path of the plugin and then add the folder which holds the .mo
285  * files.
286  *
287  * @since 1.5.0
288  *
289  * @param string $domain Unique identifier for retrieving translated strings
290  * @param string $path Optional. Path of the folder where the .mo files reside.
291  */
292 function load_plugin_textdomain($domain, $path = false) {
293         $locale = get_locale();
294         if ( empty($locale) )
295                 $locale = 'en_US';
296
297         if ( false === $path )
298                 $path = PLUGINDIR;
299
300         $mofile = ABSPATH . "$path/$domain-$locale.mo";
301         load_textdomain($domain, $mofile);
302 }
303
304 /**
305  * load_theme_textdomain() - Includes theme's translated strings for the theme
306  *
307  * If the current locale exists as a .mo file in the theme's root directory, it
308  * will be included in the translated strings by the $domain.
309  *
310  * The .mo files must be named based on the locale exactly.
311  *
312  * @since 1.5.0
313  *
314  * @param string $domain Unique identifier for retrieving translated strings
315  */
316 function load_theme_textdomain($domain) {
317         $locale = get_locale();
318         if ( empty($locale) )
319                 $locale = 'en_US';
320
321         $mofile = get_template_directory() . "/$locale.mo";
322         load_textdomain($domain, $mofile);
323 }
324
325 ?>