Imported drupal-5.5
[drupal.git] / modules / locale / locale.module
blob28db9ecd149b49b6ba0945fea06a773685259a52
1 <?php
2 // $Id: locale.module,v 1.155 2006/12/27 13:11:59 drumm Exp $
4 /**
5  * @file
6  * Enables administrators to manage the site interface languages.
7  *
8  * When enabled, the site interface can be displayed in different
9  * languages. The setup of languages and translations is completely
10  * web based. Gettext portable object files are supported.
11  */
13 // ---------------------------------------------------------------------------------
14 // Hook implementations (needed on all page loads)
16 /**
17  * Implementation of hook_help().
18  */
19 function locale_help($section) {
20   switch ($section) {
21     case 'admin/help#locale':
22       $output = '<p>'. t('The locale module allows you to present your Drupal site in a language other than the default English. You can use it to set up a multi-lingual web site or replace given <em>built-in</em> text with text which has been customized for your site. Whenever the locale module encounters text which needs to be displayed, it tries to translate it into the currently selected language. If a translation is not available, then the string is remembered, so you can look up untranslated strings easily.') .'</p>';
23       $output .= '<p>'. t('The locale module provides two options for providing translations. The first is the integrated web interface, via which you can search for untranslated strings, and specify their translations. An easier and less time-consuming method is to import existing translations for your language. These translations are available as <em>GNU gettext Portable Object files</em> (<em>.po</em> files for short). Translations for many languages are available for download from the translation page.') .'</p>';
24       $output .= '<p>'. t("If an existing translation does not meet your needs, the <em>.po</em> files are easily edited with special editing tools. The locale module's import feature allows you to add strings from such files into your site's database. The export functionality enables you to share your translations with others, generating Portable Object files from your site strings.") .'</p>';
25       $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="@locale">Locale page</a>.', array('@locale' => 'http://drupal.org/handbook/modules/locale/')) .'</p>';
26       return $output;
27     case 'admin/settings/locale':
28     case 'admin/settings/locale/language/overview':
29       return t("<p>Drupal provides support for the translation of its interface text into different languages. This page provides an overview of the installed languages. You can add a language on the <a href=\"@add-language\">add language page</a>, or directly by <a href=\"@import\">importing a translation</a>. If multiple languages are enabled, registered users will be able to set their preferred language. The site default will be used for anonymous visitors and for users without their own settings.</p><p>Drupal interface translations may be added or extended by several courses: by <a href=\"@import\">importing</a> an existing translation, by <a href=\"@search\">translating everything</a> from scratch, or by a combination of these approaches.</p>", array("@search" => url("admin/settings/locale/string/search"), "@import" => url("admin/settings/locale/language/import"), "@add-language" => url("admin/settings/locale/language/add")));
30     case 'admin/settings/locale/language/add':
31       return '<p>'. t("You need to add all languages in which you would like to display the site interface. If you can't find the desired language in the quick-add dropdown, then you will need to provide the proper language code yourself. The language code may be used to negotiate with browsers and to present flags, etc., so it is important to pick a code that is standardised for the desired language. You can also add a language by <a href=\"@import\">importing a translation</a>.", array("@import" => url("admin/settings/locale/language/import"))) .'</p>';
32     case 'admin/settings/locale/language/import':
33       return '<p>'. t("This page allows you to import a translation provided in the gettext Portable Object (.po) format. The easiest way to get your site translated is to obtain an existing Drupal translation and to import it. You can find existing translations on the <a href=\"@url\">Drupal translation page</a>. Note that importing a translation file might take a while.", array('@url' => 'http://drupal.org/project/translations')) .'</p>';
34     case 'admin/settings/locale/language/export':
35       return '<p>'. t("This page allows you to export Drupal strings. The first option is to export a translation so it can be shared. The second option generates a translation template, which contains all Drupal strings, but without their translations. You can use this template to start a new translation using various software packages designed for this task.") .'</p>';
36     case 'admin/settings/locale/string/search':
37       return '<p>'. t("It is often convenient to get the strings from your setup on the <a href=\"@export\">export page</a>, and use a desktop Gettext translation editor to edit the translations. On this page you can search in the translated and untranslated strings, and the default English texts provided by Drupal.", array("@export" => url("admin/settings/locale/language/export"))) .'</p>';
38   }
41 /**
42  * Implementation of hook_menu().
43  */
44 function locale_menu($may_cache) {
45   $items = array();
46   $access = user_access('administer locales');
48   if ($may_cache) {
49     // Main admin menu item
50     $items[] = array('path' => 'admin/settings/locale',
51       'title' => t('Localization'),
52       'description' => t('Configure site localization and user interface translation.'),
53       'callback' => 'locale_admin_manage',
54       'access' => $access);
56     // Top level tabs
57     $items[] = array('path' => 'admin/settings/locale/language',
58       'title' => t('Manage languages'),
59       'access' => $access,
60       'weight' => -10,
61       'type' => MENU_DEFAULT_LOCAL_TASK);
62     $items[] = array('path' => 'admin/settings/locale/string/search',
63       'title' => t('Manage strings'),
64       'callback' => 'locale_string_search',
65       'access' => $access,
66       'weight' => 10,
67       'type' => MENU_LOCAL_TASK);
69     // Manage languages subtabs
70     $items[] = array('path' => 'admin/settings/locale/language/overview',
71       'title' => t('List'),
72       'callback' => 'locale_admin_manage',
73       'access' => $access,
74       'weight' => 0,
75       'type' => MENU_DEFAULT_LOCAL_TASK);
76     $items[] = array('path' => 'admin/settings/locale/language/add',
77       'title' => t('Add language'),
78       'callback' => 'locale_admin_manage_add',
79       'access' => $access,
80       'weight' => 5,
81       'type' => MENU_LOCAL_TASK);
82     $items[] = array('path' => 'admin/settings/locale/language/import',
83       'title' => t('Import'),
84       'callback' => 'locale_admin_import',
85       'access' => $access,
86       'weight' => 10,
87       'type' => MENU_LOCAL_TASK);
88     $items[] = array('path' => 'admin/settings/locale/language/export',
89       'title' => t('Export'),
90       'callback' => 'locale_admin_export',
91       'access' => $access,
92       'weight' => 20,
93       'type' => MENU_LOCAL_TASK);
95     // Language related callbacks
96     $items[] = array('path' => 'admin/settings/locale/language/delete',
97       'title' => t('Confirm'),
98       'callback' => 'drupal_get_form',
99       'callback arguments' => array('locale_admin_manage_delete_form'),
100       'access' => $access,
101       'type' => MENU_CALLBACK);
102   }
103   else {
104     if (is_numeric(arg(5))) {
105       // String related callbacks
106       $items[] = array('path' => 'admin/settings/locale/string/edit/'. arg(5),
107         'title' => t('Edit string'),
108         'callback' => 'drupal_get_form',
109         'callback arguments' => array('locale_admin_string_edit', arg(5)),
110         'access' => $access,
111         'type' => MENU_CALLBACK);
112       $items[] = array('path' => 'admin/settings/locale/string/delete/'. arg(5),
113         'title' => t('Delete string'),
114         'callback' => 'locale_admin_string_delete',
115         'callback arguments' => array(arg(5)),
116         'access' => $access,
117         'type' => MENU_CALLBACK);
118     }
119   }
121   return $items;
125  * Implementation of hook_perm().
126  */
127 function locale_perm() {
128   return array('administer locales');
132  * Implementation of hook_user().
133  */
134 function locale_user($type, $edit, &$user, $category = NULL) {
135   $languages = locale_supported_languages();
136   if ($type == 'form' && $category == 'account' && count($languages['name']) > 1) {
137     if ($user->language == '') {
138       $user->language = key($languages['name']);
139     }
140     $languages['name'] = array_map('check_plain', array_map('t', $languages['name']));
141     $form['locale'] = array('#type' => 'fieldset',
142       '#title' => t('Interface language settings'),
143       '#weight' => 1,
144     );
145     $form['locale']['language'] = array('#type' => 'radios',
146       '#title' => t('Language'),
147       '#default_value' => $user->language,
148       '#options' => $languages['name'],
149       '#description' => t('Selecting a different locale will change the interface language of the site.'),
150     );
151     return $form;
152   }
155 // ---------------------------------------------------------------------------------
156 // Locale core functionality (needed on all page loads)
159  * Provides interface translation services.
161  * This function is called from t() to translate a string if needed.
162  */
163 function locale($string) {
164   global $locale;
165   static $locale_t;
167   // Store database cached translations in a static var.
168   if (!isset($locale_t)) {
169     $cache = cache_get("locale:$locale", 'cache');
171     if (!$cache) {
172       locale_refresh_cache();
173       $cache = cache_get("locale:$locale", 'cache');
174     }
175     $locale_t = unserialize($cache->data);
176   }
178   // We have the translation cached (if it is TRUE, then there is no
179   // translation, so there is no point in checking the database)
180   if (isset($locale_t[$string])) {
181     $string = ($locale_t[$string] === TRUE ? $string : $locale_t[$string]);
182   }
184   // We do not have this translation cached, so get it from the DB.
185   else {
186     $result = db_query("SELECT s.lid, t.translation FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid WHERE s.source = '%s' AND t.locale = '%s'", $string, $locale);
187     // Translation found
188     if ($trans = db_fetch_object($result)) {
189       if (!empty($trans->translation)) {
190         $locale_t[$string] = $trans->translation;
191         $string = $trans->translation;
192       }
193     }
195     // Either we have no such source string, or no translation
196     else {
197       $result = db_query("SELECT lid, source FROM {locales_source} WHERE source = '%s'", $string);
198       // We have no such translation
199       if ($obj = db_fetch_object($result)) {
200         if ($locale) {
201           db_query("INSERT INTO {locales_target} (lid, locale, translation) VALUES (%d, '%s', '')", $obj->lid, $locale);
202         }
203       }
204       // We have no such source string
205       else {
206         db_query("INSERT INTO {locales_source} (location, source) VALUES ('%s', '%s')", request_uri(), $string);
207         if ($locale) {
208           $lid = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s'", $string));
209           db_query("INSERT INTO {locales_target} (lid, locale, translation) VALUES (%d, '%s', '')", $lid->lid, $locale);
210         }
211       }
212       // Clear locale cache in DB
213       cache_clear_all("locale:$locale", 'cache');
214     }
215   }
217   return $string;
221  * Refreshes database stored cache of translations.
223  * We only store short strings to improve performance and consume less memory.
224  */
225 function locale_refresh_cache() {
226   $languages = locale_supported_languages();
228   foreach (array_keys($languages['name']) as $locale) {
229     $result = db_query("SELECT s.source, t.translation, t.locale FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid WHERE t.locale = '%s' AND LENGTH(s.source) < 75", $locale);
230     $t = array();
231     while ($data = db_fetch_object($result)) {
232       $t[$data->source] = (empty($data->translation) ? TRUE : $data->translation);
233     }
234     cache_set("locale:$locale", 'cache', serialize($t));
235   }
239  * Returns list of languages supported on this site.
241  * @param $reset Refresh cached language list.
242  * @param $getall Return all languages (even disabled ones)
243  */
244 function locale_supported_languages($reset = FALSE, $getall = FALSE) {
245   static $enabled = NULL;
246   static $all = NULL;
248   if ($reset) {
249     unset($enabled); unset($all);
250   }
252   if (is_null($enabled)) {
253     $enabled = $all = array();
254     $all['name'] = $all['formula'] = $enabled['name'] = $enabled['formula'] = array();
255     $result = db_query('SELECT locale, name, formula, enabled FROM {locales_meta} ORDER BY isdefault DESC, enabled DESC, name ASC');
256     while ($row = db_fetch_object($result)) {
257       $all['name'][$row->locale] = $row->name;
258       $all['formula'][$row->locale] = $row->formula;
259       if ($row->enabled) {
260         $enabled['name'][$row->locale] = $row->name;
261         $enabled['formula'][$row->locale] = $row->formula;
262       }
263     }
264   }
265   return $getall ? $all : $enabled;
269  * Returns plural form index for a specific number.
271  * The index is computed from the formula of this language.
272  */
273 function locale_get_plural($count) {
274   global $locale;
275   static $locale_formula, $plurals = array();
277   if (!isset($plurals[$count])) {
278     if (!isset($locale_formula)) {
279       $languages = locale_supported_languages();
280       $locale_formula = $languages['formula'][$locale];
281     }
282     if ($locale_formula) {
283       $n = $count;
284       $plurals[$count] = @eval("return intval($locale_formula);");
285       return $plurals[$count];
286     }
287     else {
288       $plurals[$count] = -1;
289       return -1;
290     }
291   }
292   return $plurals[$count];
295 // ---------------------------------------------------------------------------------
296 // Language management functionality (administration only)
299  * Page handler for the language management screen.
300  */
301 function locale_admin_manage() {
302   include_once './includes/locale.inc';
303   return drupal_get_form('_locale_admin_manage_screen');
307  * User interface for the language deletion confirmation screen.
308  */
309 function locale_admin_manage_delete_form($langcode) {
310   include_once './includes/locale.inc';
312   // Do not allow deletion of English locale.
313   if ($langcode == 'en') {
314     drupal_set_message(t('The English locale cannot be deleted.'));
315     drupal_goto('admin/settings/locale/language/overview');
316   }
318   // For other locales, warn user that data loss is ahead.
319   $languages = locale_supported_languages(FALSE, TRUE);
321   if (!isset($languages['name'][$langcode])) {
322     drupal_not_found();
323   }
324   else {
325     $form['langcode'] = array('#type' => 'value', '#value' => $langcode);
326     return confirm_form($form, t('Are you sure you want to delete the language %name?', array('%name' => t($languages['name'][$langcode]))), 'admin/settings/locale/language/overview', t('Deleting a language will remove all data associated with it. This action cannot be undone.'), t('Delete'), t('Cancel'));
327   }
331  * Process language deletion submissions.
332  */
333 function locale_admin_manage_delete_form_submit($form_id, $form_values) {
334   $languages = locale_supported_languages(FALSE, TRUE);
335   if (isset($languages['name'][$form_values['langcode']])) {
336     db_query("DELETE FROM {locales_meta} WHERE locale = '%s'", $form_values['langcode']);
337     db_query("DELETE FROM {locales_target} WHERE locale = '%s'", $form_values['langcode']);
338     $message = t('The language %locale has been removed.', array('%locale' => t($languages['name'][$form_values['langcode']])));
339     drupal_set_message($message);
340     watchdog('locale', $message);
341   }
343   // Changing the locale settings impacts the interface:
344   cache_clear_all('*', 'cache_menu', TRUE);
345   cache_clear_all('*', 'cache_page', TRUE);
347   return 'admin/settings/locale/language/overview';
351  * Page handler for the language addition screen
352  */
353 function locale_admin_manage_add() {
354   include_once './includes/locale.inc';
355   return _locale_admin_manage_add_screen();
358 // ---------------------------------------------------------------------------------
359 // Gettext Portable Object import functionality (administration only)
362  * Page handler for the translation import screen
363  */
364 function locale_admin_import() {
365   include_once './includes/locale.inc';
366   return drupal_get_form('_locale_admin_import');
369 // ---------------------------------------------------------------------------------
370 // Gettext Portable Object export functionality (administration only)
373  * Page handler for the translation export screen
374  */
375 function locale_admin_export() {
376   include_once './includes/locale.inc';
377   return _locale_admin_export_screen();
380 // ---------------------------------------------------------------------------------
381 // String search and editing functionality (administration only)
384  * Page handler for the string search.
385  */
386 function locale_string_search() {
387   include_once './includes/locale.inc';
388   $output = _locale_string_seek();
389   $output .= drupal_get_form('_locale_string_seek_form');
390   return $output;
394  * Display the string edit form.
395  */
396 function locale_admin_string_edit($lid) {
397   include_once './includes/locale.inc';
398   return _locale_string_edit($lid);
402  * Process the string edit form.
403  */
404 function locale_admin_string_edit_submit($form_id, $form_values) {
405   include_once './includes/locale.inc';
406   return _locale_string_edit_submit($form_id, $form_values);
410  * Delete a string.
411  */
412 function locale_admin_string_delete($lid) {
413   include_once './includes/locale.inc';
414   _locale_string_delete($lid);