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