locale.module 15 KB
Newer Older
Dries's avatar
 
Dries committed
1
<?php
2
// $Id$
Dries's avatar
 
Dries committed
3

Dries's avatar
 
Dries committed
4 5 6 7
/**
 * Implementation of hook_help().
 */
function locale_help($section) {
Dries's avatar
 
Dries committed
8
  switch ($section) {
Dries's avatar
 
Dries committed
9
    case 'admin/help#locale':
Dries's avatar
 
Dries committed
10
      return t("
Dries's avatar
 
Dries committed
11 12 13 14 15
      <p>Most programs are written and documented in English, and use English to interact with users.  This is also true for a great deal of web sites.  However, most people are less comfortable with English than with their native language, and would prefer to use their mother tongue as much as possible.  Many people love see their web site showing a lot less English, and far more of their own language.</p>
      <p>Therefore Drupal provides a framework to setup a multi-lingual web site, or to overwrite the default English texts.  We explored the various alternatives to support internationalization (I18N) and decided to design the framework in such a way that the impact of internationalization on drupal's sources is minimized, modular and doesn't require a HTML or PHP wizard to maintain translations.  Maintaining translations had to be simple so it became as easy as filling out forms on the administration page.</p>
      <h3>How to translate texts</h3>
      <p>The actual translation starts at the <a href=\"%overview\">overview</a> page of the locale section in the administration pages. In the menu on the left under \"localization\" you will see a list of the languages you have configured. Click on the name of the language to start translating. Looking at a page full of all the strings in the site can be a bit overwhelming, so Drupal allows you to limit the strings you are working on. If you want to limit based on translated strings click \"translated strings\", if you want to limit the string based on the untranslated strings click \"untranslated strings\". Both will take you to the same page. Once there enter the string pattern to limit on, choose the language to search for, and the status, weather translated, untranslated or both, finally where you want to look, modules, specific modules, or pages.</p>
      <p>At the locale page, users with the proper access rights will see the various texts that need translation on the left column of the table.</p>
16
      <p>Below the text you can see an example URI where this text shows up on your site.  Chances are most of these texts will be used and displayed on more than one page, though only one example URI is presented.</p>
Dries's avatar
 
Dries committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
      <p>The second column displays the supported languages as defined in the configuration file.  See below for more information on how to support new languages.  If the symbol for a language is seen like <strike>this</strike>, it means that this entry still needs to be translated into that language.  If not, it has been translated already.</p>
      <p>To add or change a translation click the \"edit locale\" link in the third column, the \"operations\" column.  You'll be presented the original text and fields for translation in the supported languages.  Enter the translations and confirm by clicking the \"Save translations\" button.  The translations need not be accurate; they are for your site so you can choose what to show to your users.</p>
      <p>To delete a translation, click the \"delete locale\" link at the overview page and the translation will be immediately deleted without confirmation.  Deleting translations is convenient for removing texts that belonged to an obsolete module.</p>
      <p>In some texts special strings such as \"%a\" and \"%b\" show up.  Those get replaced by some string at run-time when Drupal dynamically generate pages.  You can find out which string this is by looking at the page where the text appears.  This is where the above mentioned URI can come in handy.</p>
      <h3>How to add new languages</h3>
      <p>Adding a new language requires you to edit your configuration file and your SQL database.  Assuming you want to support Dutch (ISO 639 code: \"nl\") and French (ISO 639 code: \"fr\"), you add the following line to your configuration file's <code>\$languages</code>-variable:</p>
      <pre>
      \$languages = array(\"nl\" => \"Dutch / Nederlands\", \"fr\" => \"French / Francais\");
      </pre>

      <p>Note that the default language must come first and that if you want to overwrite the default text you can add an entry for English (ISO 639 code: \"en\"):</p>
      <pre>
      \$languages = array(\"en\" => \"English\", \"nl\" => \"Dutch / Nederlands\", \"fr\" => \"French / Francais\");
      </pre>
      <p>After having edited your configuration file, make sure your SQL table \"locales\" has the required database fields setup to host your new translations.  You can add the required rows to your \"locales\" table from the MySQL prompt:</p>
      <pre>
Dries's avatar
 
Dries committed
33 34 35
    mysql> ALTER TABLE {locales} ADD en TEXT DEFAULT '' NOT NULL;
    mysql> ALTER TABLE {locales} ADD nl TEXT DEFAULT '' NOT NULL;
    mysql> ALTER TABLE {locales} ADD fr TEXT DEFAULT '' NOT NULL;
Dries's avatar
 
Dries committed
36
  </pre>", array('%overview' => url('admin/locale')));
Dries's avatar
 
Dries committed
37
      break;
Dries's avatar
 
Dries committed
38
    case 'admin/modules#description':
Dries's avatar
 
Dries committed
39
      return t('Enables the translation of the user interface to languages other than English.');
Dries's avatar
 
Dries committed
40
    case 'admin/locale':
Dries's avatar
 
Dries committed
41
      return t('The locale module handles translations into new languages.  It also enables you to add jargon, slang or other special language as fits the web site.  For each language you want to support, a line needs to be added to your configuration file.');
Dries's avatar
 
Dries committed
42
    case 'admin/locale/search':
Dries's avatar
 
Dries committed
43
      return t('Search the localization database. ("*" can be used as a wildcard)');
Dries's avatar
 
Dries committed
44 45
  }

Dries's avatar
 
Dries committed
46 47
}

Dries's avatar
 
Dries committed
48 49 50
/**
 * Menu callback; prints locale-specific help text from admin/help.
 */
Dries's avatar
 
Dries committed
51
function locale_help_page() {
Dries's avatar
 
Dries committed
52
  print theme('page', locale_help('admin/help#locale'));
Dries's avatar
 
Dries committed
53 54
}

Dries's avatar
 
Dries committed
55 56 57
/**
 * Implementation of hook_perm().
 */
Dries's avatar
 
Dries committed
58
function locale_perm() {
Dries's avatar
 
Dries committed
59
  return array('administer locales');
Dries's avatar
 
Dries committed
60 61
}

Dries's avatar
 
Dries committed
62
/**
Dries's avatar
 
Dries committed
63
 * Implementation of hook_menu().
Dries's avatar
 
Dries committed
64
 */
Dries's avatar
 
Dries committed
65
function locale_menu() {
Dries's avatar
 
Dries committed
66 67
  global $languages;

Dries's avatar
 
Dries committed
68 69
  $items = array();
  $access = user_access('administer locales');
Dries's avatar
 
Dries committed
70

Dries's avatar
 
Dries committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  $items[] = array('path' => 'admin/locale', 'title' => t('localization'),
    'callback' => 'locale_admin', 'access' => $access);
  $items[] = array('path' => 'admin/locale/edit', 'title' => t('edit string'),
    'callback' => 'locale_admin', 'access' => $access,
    'type' => MENU_CALLBACK);
  $items[] = array('path' => 'admin/locale/delete', 'title' => t('delete string'),
    'callback' => 'locale_admin', 'access' => $access,
    'type' => MENU_CALLBACK);

  foreach ($languages as $key => $value) {
    $items[] = array('path' => "admin/locale/$key", 'title' => $value,
      'callback' => 'locale_admin', 'access' => $access);
    $items[] = array('path' => "admin/locale/$key/translated", 'title' => t('translated strings'),
      'callback' => 'locale_admin', 'access' => $access);
    $items[] = array('path' => "admin/locale/$key/untranslated", 'title' => t('untranslated strings'),
      'callback' => 'locale_admin', 'access' => $access);
Dries's avatar
 
Dries committed
87
  }
Dries's avatar
 
Dries committed
88 89

  return $items;
Dries's avatar
 
Dries committed
90 91
}

Dries's avatar
 
Dries committed
92 93 94
/**
 * Implementation of hook_user(). Allows each user to select an interface language.
 */
95
function locale_user($type, &$edit, &$user, $category = NULL) {
Dries's avatar
 
Dries committed
96
  global $languages;
97 98 99 100 101
  if ($type == 'form' && count($languages) > 1 && $category == 'account') {
    return array(array(
      'title' => t('Locale settings'),
      'data' => form_radios(t('Language'), 'language', $user->language, $languages, t('Selecting a different language will change the language of the site.')),
      'weight' => 2));
Dries's avatar
 
Dries committed
102 103 104
  }
}

Dries's avatar
 
Dries committed
105
function locale_delete($lid) {
Dries's avatar
 
Dries committed
106
  db_query('DELETE FROM {locales} WHERE lid = %d', $lid);
Dries's avatar
 
Dries committed
107
  locale_refresh_cache();
Dries's avatar
 
Dries committed
108

Dries's avatar
 
Dries committed
109
  drupal_set_message(t('deleted string'));
Dries's avatar
 
Dries committed
110 111
}

Dries's avatar
 
Dries committed
112
function locale_save($lid) {
Dries's avatar
 
Dries committed
113
  $edit =& $_POST['edit'];
Dries's avatar
 
Dries committed
114
  foreach ($edit as $key => $value) {
Dries's avatar
 
Dries committed
115
    db_query("UPDATE {locales} SET $key = '%s' WHERE lid = %d", $value, $lid);
Dries's avatar
 
Dries committed
116 117
  }
  locale_refresh_cache();
Dries's avatar
 
Dries committed
118 119
  // delete form data so it will remember where it came from
  $edit = '';
Dries's avatar
 
Dries committed
120

Dries's avatar
 
Dries committed
121
  drupal_set_message(t('saved string'));
Dries's avatar
 
Dries committed
122 123 124 125 126 127
}

function locale_refresh_cache() {
  global $languages;

  foreach (array_keys($languages) as $locale) {
Dries's avatar
 
Dries committed
128 129 130 131
    /*
    ** We only load short strings into the cache to improve both performance
    ** and memory usages.
    */
Dries's avatar
 
Dries committed
132
    $result = db_query('SELECT string, %s FROM {locales} WHERE LENGTH(string) < 75', $locale);
Dries's avatar
 
Dries committed
133
    while ($data = db_fetch_object($result)) {
134 135 136 137 138 139
      if (empty($data->$locale)) {
        $t[$data->string] = $data->string;
      }
      else {
        $t[$data->string] = $data->$locale;
      }
Dries's avatar
 
Dries committed
140 141
    }
    cache_set("locale:$locale", serialize($t));
Dries's avatar
 
Dries committed
142 143 144
  }
}

Dries's avatar
 
Dries committed
145
function locale_edit($lid) {
Dries's avatar
 
Dries committed
146
  global $languages;
Dries's avatar
 
Dries committed
147

Dries's avatar
 
Dries committed
148
  $result = db_query("SELECT * FROM {locales} WHERE lid = '$lid'");
Dries's avatar
 
Dries committed
149
  if ($translation = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
150

Dries's avatar
 
Dries committed
151
    $form .= form_item(t('Original text'), wordwrap(drupal_specialchars($translation->string, 0)));
Dries's avatar
 
Dries committed
152 153 154 155 156

    foreach ($languages as $code=>$language) {
      $form .= (strlen($translation->string) > 30) ? form_textarea($language, $code, $translation->$code, 50, 10) : form_textfield($language, $code, $translation->$code, 50, 128);
    }

Dries's avatar
 
Dries committed
157
    $form .= form_submit(t('Save translations'));
Dries's avatar
 
Dries committed
158

Dries's avatar
 
Dries committed
159
    return form($form);
Dries's avatar
 
Dries committed
160 161 162 163
  }
}

function locale_languages($translation) {
Dries's avatar
 
Dries committed
164 165
  global $languages;

Dries's avatar
 
Dries committed
166
  foreach ($languages as $key => $value) {
Dries's avatar
 
Dries committed
167
    $output .= ($translation->$key) ? '<a href="#" title="'. $translation->$key ."\">$key</a> " : "<strike>$key</strike> ";
Dries's avatar
 
Dries committed
168 169 170 171 172
  }

  return $output;
}

173
function locale_seek_query() {
Dries's avatar
 
Dries committed
174 175 176
  $fields = array('string', 'language', 'status');
  if (is_array($_REQUEST['edit'])) {
    foreach ($_REQUEST['edit'] as $key => $value) {
177 178 179 180
      if (!empty($value) && in_array($key, $fields)) {
        $query->$key = $value;
      }
    }
Kjartan's avatar
Kjartan committed
181
  }
Dries's avatar
 
Dries committed
182
  else {
183 184
    foreach ($_REQUEST as $key => $value) {
      if (!empty($value) && in_array($key, $fields)) {
Dries's avatar
 
Dries committed
185
        $query->$key = strpos(',', $value) ? explode(',', $value) : $value;
186 187
      }
    }
Dries's avatar
 
Dries committed
188
  }
189 190
  return $query;
}
Dries's avatar
 
Dries committed
191

192 193
function locale_seek() {
  global $id, $languages;
Dries's avatar
 
Dries committed
194
  $op = $_POST['op'];
195
  $query = locale_seek_query();
Dries's avatar
 
Dries committed
196

197 198 199 200
  if ($query) {

    if ($query->status) {
      switch ($query->language) {
Dries's avatar
 
Dries committed
201
        case 'all':
Kjartan's avatar
Kjartan committed
202
          foreach ($languages as $key=>$value) {
Dries's avatar
 
Dries committed
203
            $tmp[] = $key . (check_query($query->status) == 1 ? ' !=' : ' =') ." ''";
Kjartan's avatar
Kjartan committed
204
          }
Dries's avatar
 
Dries committed
205
          $sql[] = implode(' AND ', $tmp);
Kjartan's avatar
Kjartan committed
206
          break;
Dries's avatar
 
Dries committed
207
        case 'any':
Kjartan's avatar
Kjartan committed
208
          foreach ($languages as $key=>$value) {
Dries's avatar
 
Dries committed
209
            $tmp[] = $key . (check_query($query->status) == 1 ? ' !=' : ' =') ." ''";
Kjartan's avatar
Kjartan committed
210
          }
Dries's avatar
 
Dries committed
211
          $sql[] = '('. implode(' OR ', $tmp) .')';
Kjartan's avatar
Kjartan committed
212 213
          break;
        default:
Dries's avatar
 
Dries committed
214
          $sql[] = check_query($query->language) . (check_query($query->status) == 1 ? ' !=' : ' =') ." ''";
Kjartan's avatar
Kjartan committed
215 216
      }
    }
Dries's avatar
 
Dries committed
217

218 219 220 221 222
    if ($query->string) {
      $string_query[] = "string LIKE '%". check_query($query->string) ."%'";
      if ($query->status != 2) {
        if (strlen($query->language) == 2) {
          $string_query[] = check_query($query->language) ." LIKE '%". check_query($query->string) ."%'";
Dries's avatar
 
Dries committed
223 224 225
        }
        else {
          foreach ($languages as $key=>$value) {
226
            $string_query[] = check_query($key) ." LIKE '%". check_query($query->string) ."%'";
Dries's avatar
 
Dries committed
227 228 229
          }
        }
      }
Dries's avatar
 
Dries committed
230
      $sql[] = '('. implode(' OR ', $string_query) .')';
Dries's avatar
 
Dries committed
231 232
    }

Dries's avatar
 
Dries committed
233
    $result = pager_query('SELECT * FROM {locales} '. (count($sql) ? ' WHERE '. implode(' AND ', $sql) : '') .' ORDER BY string', 50);
Dries's avatar
 
Dries committed
234

Dries's avatar
 
Dries committed
235
    $header = array(t('string'), (($query->status != 2 && strlen($query->language) == 2) ? t('translated string') : t('languages')), array('data' => t('operations'), 'colspan' => '2'));
Kjartan's avatar
Kjartan committed
236
    while ($locale = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
237
      $rows[] = array("$locale->string<br /><small><i>$locale->location</i></small>", array('data' => (($query->status != 2 && strlen($query->language) == 2) ? $locale->{$query->language} : locale_languages($locale)), 'align' => 'center'), array('data' => l(t('edit locale'), "admin/locale/edit/$locale->lid"), 'nowrap' => 'nowrap'), array('data' => l(t('delete locale'), "admin/locale/delete/$locale->lid"), 'nowrap' => 'nowrap'));
Kjartan's avatar
Kjartan committed
238
    }
239

240 241 242
    $request = array();
    if (count($query)) {
      foreach ($query as $key => $value) {
Dries's avatar
 
Dries committed
243
        $request[$key] = (is_array($value)) ? implode(',', $value) : $value;
244 245 246
      }
    }

Dries's avatar
 
Dries committed
247 248
    if ($pager = theme('pager', NULL, 50, 0, $request)) {
      $rows[] = array(array('data' => "$pager", 'colspan' => '5'));
Dries's avatar
 
Dries committed
249 250
    }

Dries's avatar
 
Dries committed
251
    $output .= theme('table', $header, $rows);
252

Kjartan's avatar
Kjartan committed
253
  }
Kjartan's avatar
Kjartan committed
254

Dries's avatar
 
Dries committed
255 256
  return $output;
}
Dries's avatar
 
Dries committed
257

Dries's avatar
 
Dries committed
258 259
function locale_seek_form() {
  global $languages;
Dries's avatar
 
Dries committed
260 261
  $edit =& $_POST['edit'];
  $form .= form_textfield(t('Strings to search for'), 'string', $edit['string'], 30, 30, t('Leave blank to show all strings.'));
Dries's avatar
 
Dries committed
262
  if (count($languages) > 1) {
Dries's avatar
 
Dries committed
263
    $form .= form_radios(t('Language'), 'language', ($edit['language'] ? $edit['language'] : 'all'), array_merge(array('any' => t('Any language'), 'all' => t('All languages')), $languages), t('In which language must the string be translated/untranslated (see status)?'));
Dries's avatar
 
Dries committed
264 265 266
  }
  else {
    foreach ($languages as $key => $value) {
Dries's avatar
 
Dries committed
267
      $form .= form_hidden('language', $key);
Dries's avatar
 
Dries committed
268 269
    }
  }
Dries's avatar
 
Dries committed
270 271
  $form .= form_radios(t('Status'), 'status', $edit['status'], array(2 => t('Untranslated'), 1 => t('Translated'), 0 => t('All')));
  $form .= form_submit(t('Search'));
Dries's avatar
 
Dries committed
272

Dries's avatar
 
Dries committed
273
  $output .= form($form);
Kjartan's avatar
Kjartan committed
274 275 276 277

  return $output;
}

Dries's avatar
 
Dries committed
278
function locale_admin() {
Dries's avatar
 
Dries committed
279 280
  $op = $_POST['op'];
  $edit =& $_POST['edit'];
Dries's avatar
 
Dries committed
281

Dries's avatar
 
Dries committed
282 283 284
  if (empty($op)) {
    $op = arg(2);
  }
Dries's avatar
 
Dries committed
285

Dries's avatar
 
Dries committed
286 287 288 289 290 291 292 293 294 295 296
  switch ($op) {
    case 'delete':
      $output .= locale_delete(check_query(arg(3)));
      $output .= locale_seek();
      break;
    case 'edit':
      $output .= locale_edit(check_query(arg(3)));
      $output .= locale_seek();
      break;
    case 'search':
      if (locale_seek_query()) {
Dries's avatar
 
Dries committed
297
        $output = locale_seek();
Dries's avatar
 
Dries committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
      }
      $output .= locale_seek_form();
      break;
    case t('Search'):
      $output = locale_seek();
      $output .= locale_seek_form();
      break;
    case t('Save translations'):
      $output .= locale_save(check_query(arg(3)));
      $output .= locale_seek_form();
      break;
    default:
      if (arg(3) == 'translated') {
        $edit['status'] = 1;
        $edit['language'] = arg(2);
      }
      else {
        $edit['status'] = 2;
        $edit['language'] = arg(2);
      }
      $output = locale_seek();
      $output .= locale_seek_form();
Dries's avatar
 
Dries committed
320
  }
Dries's avatar
 
Dries committed
321
  print theme('page', $output);
Dries's avatar
 
Dries committed
322 323
}

Dries's avatar
 
Dries committed
324 325 326 327 328 329 330
/**
 * Does the work of localizing a string of text.
 *
 * This function is called by the universally-used t() function each time
 * an internationalized string is encountered. The string is then localized
 * by this function if the locale module is enabled.
 */
Dries's avatar
 
Dries committed
331 332
function locale($string) {
  global $locale;
Dries's avatar
 
Dries committed
333 334
  static $locale_t;

Dries's avatar
 
Dries committed
335
  if (!isset($locale_t)) {
Dries's avatar
 
Dries committed
336
    $cache = cache_get("locale:$locale");
Dries's avatar
 
Dries committed
337

338 339 340
    if ($cache == 0) {
      locale_refresh_cache();
      $cache = cache_get("locale:$locale");
Dries's avatar
 
Dries committed
341
    }
342
    $locale_t = unserialize($cache->data);
Dries's avatar
 
Dries committed
343
  }
Dries's avatar
 
Dries committed
344

Dries's avatar
 
Dries committed
345
  if (isset($locale_t[$string])) {
Dries's avatar
 
Dries committed
346
    $string = $locale_t[$string];
Dries's avatar
 
Dries committed
347 348
  }
  else {
Dries's avatar
 
Dries committed
349
    $result = db_query("SELECT lid, $locale FROM {locales} WHERE string = '%s'", $string);
Dries's avatar
 
Dries committed
350 351 352 353 354 355 356
    if ($trans = db_fetch_object($result)) {
      if (!empty($trans->$locale)) {
        $locale_t[$string] = $trans->$locale;
        $string = $trans->$locale;
      }
    }
    else {
Dries's avatar
 
Dries committed
357
      db_query("INSERT INTO {locales} (string, location) VALUES ('%s', '%s')", $string, request_uri());
358
      cache_clear_all("locale:$locale");
Dries's avatar
 
Dries committed
359
    }
Dries's avatar
Dries committed
360
  }
Dries's avatar
 
Dries committed
361

Dries's avatar
 
Dries committed
362 363
  return $string;
}
Dries's avatar
 
Dries committed
364

Dries's avatar
Dries committed
365
?>