locale.module 14.9 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 8 9 10 11 12 13 14 15 16 17 18 19 20 21
function locale_help($section = "admin/locale/help") {
  $output = "";

  switch($section) {
    case 'admin/help':
    case 'admin/locale/help':
      $output .= "<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>";
      $output .= "<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>";
      $output .= "<h3>How to translate texts</h3>";
      $output .= strtr("<p>The actual translation starts at the %overview 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>", array("%overview" => l(t("overview"), "admin/locale") ));
      $output .= "<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>";
      $output .= "<p>Below the text you can see an example URI where this text shows up one 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>";
      $output .= "<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>";
      $output .= "<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>";
      $output .= "<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>";
      $output .= "<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>";
      $output .= "<h3>How to add new languages</h3>";
      $output .= "<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>";
Dries's avatar
 
Dries committed
22 23 24
  $output .= "<pre>
    \$languages = array(\"nl\" => \"Dutch / Nederlands\", \"fr\" => \"French / Francais\");
  </pre>";
Dries's avatar
 
Dries committed
25

Dries's avatar
 
Dries committed
26 27
      $output .= "<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>";
      $output .= "<pre>
Dries's avatar
 
Dries committed
28 29
    \$languages = array(\"en\" => \"English\", \"nl\" => \"Dutch / Nederlands\", \"fr\" => \"French / Francais\");
  </pre>";
Dries's avatar
 
Dries committed
30 31
      $output .= "<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>";
      $output .= "<pre>
Dries's avatar
 
Dries committed
32 33 34
    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
35
  </pre>";
Dries's avatar
 
Dries committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
      break;
    case 'admin/system/modules':
      $output = "Enables the translation of the user interface to languages other than English.";
      break;
    case 'admin/locale':
      $output = "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.";
      break;
    case 'admin/locale/search':
      $output = "Search the localization database. ('*' can be used as a wildcard)";
      break;
    case 'admin/locale/translated':
      $output = "Start by searching the translated strings.";
      break;
    case 'admin/locale/untranslated':
      $output = "Start by searching the untranslated strings.";
      break;
  }

Dries's avatar
 
Dries committed
54
  return t($output);
Dries's avatar
 
Dries committed
55 56
}

57
function locale_system($field){
Dries's avatar
 
Dries committed
58
  $output = "";
Dries's avatar
 
Dries committed
59

Dries's avatar
 
Dries committed
60
  if ($field == "description") { $output = locale_help("admin/system/modules"); }
Dries's avatar
 
Dries committed
61 62

  return $output;
63 64
}

Dries's avatar
 
Dries committed
65
function locale_perm() {
Dries's avatar
 
Dries committed
66 67 68 69
  return array("administer locales");
}

function locale_link($type) {
Dries's avatar
 
Dries committed
70 71
  global $languages;

Dries's avatar
 
Dries committed
72 73 74
  if ($type == "system") {
    if (user_access("administer locales")) {

Dries's avatar
 
Dries committed
75 76 77 78 79
      menu("admin/locale", t("localization"), NULL, locale_help("admin/locale"), 5);
      menu("admin/locale/search", t("search string"), "locale_admin", locale_help("admin/locale/search"), 8);
      menu("admin/locale/help", t("help"), "locale_help", NULL, 9);
      menu("admin/locale/edit", t("edit string"), "locale_admin", NULL, 0, 1); // hidden menu
      menu("admin/locale/delete", t("delete string"), "locale_admin", NULL, 0, 1); // hidden menu
Dries's avatar
 
Dries committed
80 81 82

      foreach ($languages as $key => $value) {
        menu("admin/locale/$key", "$value", NULL, locale_help("admin/locale"));
Dries's avatar
 
Dries committed
83 84
        menu("admin/locale/$key/translated", t("translated strings"), "locale_admin", locale_help("admin/locale/translated"));
        menu("admin/locale/$key/untranslated", t("untranslated strings"), "locale_admin", locale_help("admin/locale/untranslated"));
Dries's avatar
 
Dries committed
85
      }
Dries's avatar
 
Dries committed
86 87
    }
  }
Dries's avatar
 
Dries committed
88 89
}

Dries's avatar
 
Dries committed
90
function locale_delete($lid) {
Dries's avatar
 
Dries committed
91
  db_query("DELETE FROM {locales} WHERE lid = %d", $lid);
Dries's avatar
 
Dries committed
92
  locale_refresh_cache();
Dries's avatar
 
Dries committed
93 94

  return t("deleted string");
Dries's avatar
 
Dries committed
95 96
}

Dries's avatar
 
Dries committed
97
function locale_save($lid) {
Dries's avatar
 
Dries committed
98
  $edit =& $_POST["edit"];
Dries's avatar
 
Dries committed
99
  foreach ($edit as $key=>$value) {
Dries's avatar
 
Dries committed
100
    db_query("UPDATE {locales} SET $key = '%s' WHERE lid = %d", $value, $lid);
Dries's avatar
 
Dries committed
101 102
  }
  locale_refresh_cache();
Dries's avatar
 
Dries committed
103 104
  // delete form data so it will remember where it came from
  $edit = '';
Dries's avatar
 
Dries committed
105 106

  return t("saved string");
Dries's avatar
 
Dries committed
107 108 109 110 111 112
}

function locale_refresh_cache() {
  global $languages;

  foreach (array_keys($languages) as $locale) {
Dries's avatar
 
Dries committed
113
    $result = db_query("SELECT string, %s FROM {locales} ", $locale);
Dries's avatar
 
Dries committed
114
    while ($data = db_fetch_object($result)) {
115 116 117 118 119 120
      if (empty($data->$locale)) {
        $t[$data->string] = $data->string;
      }
      else {
        $t[$data->string] = $data->$locale;
      }
Dries's avatar
 
Dries committed
121 122
    }
    cache_set("locale:$locale", serialize($t));
Dries's avatar
 
Dries committed
123 124 125
  }
}

Dries's avatar
 
Dries committed
126
function locale_edit($lid) {
Dries's avatar
 
Dries committed
127
  global $languages;
Dries's avatar
 
Dries committed
128

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

Dries's avatar
 
Dries committed
132
    $form .= form_item(t("Original text"), wordwrap(drupal_specialchars($translation->string, 0)));
Dries's avatar
 
Dries committed
133 134 135 136 137

    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
138
    $form .= form_submit(t("Save translations"));
Dries's avatar
 
Dries committed
139

Dries's avatar
 
Dries committed
140
    return form($form);
Dries's avatar
 
Dries committed
141 142 143 144
  }
}

function locale_languages($translation) {
Dries's avatar
 
Dries committed
145 146
  global $languages;

Dries's avatar
 
Dries committed
147 148
  foreach ($languages as $key => $value) {
    $output .= ($translation->$key) ? "<a href=\"#\" title=\"". $translation->$key ."\">$key</a> " : "<strike>$key</strike> ";
Dries's avatar
 
Dries committed
149 150 151 152 153
  }

  return $output;
}

154 155 156 157 158 159 160 161
function locale_seek_query() {
  $fields = array("string", "language", "status");
  if (is_array($_REQUEST["edit"])) {
    foreach ($_REQUEST["edit"] as $key => $value) {
      if (!empty($value) && in_array($key, $fields)) {
        $query->$key = $value;
      }
    }
Kjartan's avatar
Kjartan committed
162
  }
Dries's avatar
 
Dries committed
163
  else {
164 165 166 167 168
    foreach ($_REQUEST as $key => $value) {
      if (!empty($value) && in_array($key, $fields)) {
        $query->$key = strpos(",", $value) ? explode(",", $value) : $value;
      }
    }
Dries's avatar
 
Dries committed
169
  }
170 171
  return $query;
}
Dries's avatar
 
Dries committed
172

173 174 175 176
function locale_seek() {
  global $id, $languages;
  $op = $_POST["op"];
  $query = locale_seek_query();
Dries's avatar
 
Dries committed
177

178 179 180 181
  if ($query) {

    if ($query->status) {
      switch ($query->language) {
Kjartan's avatar
Kjartan committed
182 183
        case "all":
          foreach ($languages as $key=>$value) {
184
            $tmp[] = $key . (check_query($query->status) == 1 ? " !=" : " =") ." ''";
Kjartan's avatar
Kjartan committed
185
          }
186
          $sql[] = implode(" AND ", $tmp);
Kjartan's avatar
Kjartan committed
187 188 189
          break;
        case "any":
          foreach ($languages as $key=>$value) {
190
            $tmp[] = $key . (check_query($query->status) == 1 ? " !=" : " =") ." ''";
Kjartan's avatar
Kjartan committed
191
          }
192
          $sql[] = "(". implode(" || ", $tmp) .")";
Kjartan's avatar
Kjartan committed
193 194
          break;
        default:
195
          $sql[] = check_query($query->language) . (check_query($query->status) == 1 ? " !=" : " =") ." ''";
Kjartan's avatar
Kjartan committed
196 197
      }
    }
Dries's avatar
 
Dries committed
198

199 200 201 202 203
    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
204 205 206
        }
        else {
          foreach ($languages as $key=>$value) {
207
            $string_query[] = check_query($key) ." LIKE '%". check_query($query->string) ."%'";
Dries's avatar
 
Dries committed
208 209 210
          }
        }
      }
211
      $sql[] = "(". implode(" || ", $string_query) .")";
Dries's avatar
 
Dries committed
212 213
    }

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

216
    $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
217
    while ($locale = db_fetch_object($result)) {
218
      $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
219
    }
220

221 222 223 224 225 226 227 228
    $request = array();
    if (count($query)) {
      foreach ($query as $key => $value) {
        $request[$key] = (is_array($value)) ? implode(",", $value) : $value;
      }
    }

    if ($pager = pager_display(NULL, 50, 0, "admin", $request)) {
229
      $rows[] = array(array("data" => "$pager", "colspan" => "5"));
Dries's avatar
 
Dries committed
230 231
    }

Dries's avatar
 
Dries committed
232
    $output .= table($header, $rows);
233

Kjartan's avatar
Kjartan committed
234
  }
Kjartan's avatar
Kjartan committed
235

Dries's avatar
 
Dries committed
236 237
  return $output;
}
Dries's avatar
 
Dries committed
238

Dries's avatar
 
Dries committed
239 240 241
function locale_seek_form() {
  global $languages;
  $edit =& $_POST["edit"];
Dries's avatar
 
Dries committed
242
  $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
243 244 245
  $form .= form_select(t("Language"), "language", ($edit["language"] ? $edit["language"] : key($languages)), array_merge(array("any" => t("Any language"), "all" => t("All languages")), $languages), t("In which language must the string be translated/untranslated (see status)?"));
  $form .= form_select(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
246

Dries's avatar
 
Dries committed
247
  $output .= form($form);
Kjartan's avatar
Kjartan committed
248 249 250 251

  return $output;
}

Dries's avatar
 
Dries committed
252
function locale_admin() {
Dries's avatar
 
Dries committed
253
  $op = $_POST["op"];
Dries's avatar
 
Dries committed
254
  $edit =& $_POST["edit"];
Dries's avatar
 
Dries committed
255

Dries's avatar
 
Dries committed
256
  if (user_access("administer locales")) {
Steven Wittens's avatar
Steven Wittens committed
257 258
    locale_admin_initialize();

Dries's avatar
 
Dries committed
259 260 261
    if (empty($op)) {
      $op = arg(2);
    }
Dries's avatar
 
Dries committed
262 263 264

    switch ($op) {
      case "delete":
Dries's avatar
 
Dries committed
265 266
        $output = status(locale_delete(check_query(arg(3))));
        $output .= locale_seek();
Dries's avatar
 
Dries committed
267 268
        break;
      case "edit":
Dries's avatar
 
Dries committed
269
        $output = locale_edit(check_query(arg(3)));
Dries's avatar
 
Dries committed
270
        break;
271
      case "search":
272 273 274 275
        if (locale_seek_query()) {
          $output = locale_seek();
        }
        $output .= locale_seek_form();
Dries's avatar
 
Dries committed
276
        break;
Dries's avatar
 
Dries committed
277
      case t("Search"):
Dries's avatar
 
Dries committed
278 279
        $output = locale_seek();
        $output .= locale_seek_form();
Dries's avatar
 
Dries committed
280
        break;
Dries's avatar
 
Dries committed
281
      case t("Save translations"):
Dries's avatar
 
Dries committed
282 283
        $output = status(locale_save(check_query(arg(3))));
        $output .= locale_seek();
Kjartan's avatar
Kjartan committed
284
        break;
Dries's avatar
 
Dries committed
285
      default:
Dries's avatar
 
Dries committed
286 287 288 289 290 291 292 293
        if (arg(3) == "translated") {
          $edit["status"] = 1;
          $edit["language"] = arg(2);
        }
        else {
          $edit["status"] = 2;
          $edit["language"] = arg(2);
        }
Dries's avatar
 
Dries committed
294 295
        $output = locale_seek();
        $output .= locale_seek_form();
Dries's avatar
 
Dries committed
296
    }
Dries's avatar
 
Dries committed
297
    return $output;
Dries's avatar
 
Dries committed
298 299
  }
  else {
Dries's avatar
 
Dries committed
300
    return message_access();
Dries's avatar
 
Dries committed
301 302 303
  }
}

Steven Wittens's avatar
Steven Wittens committed
304
function locale_admin_initialize() {
Dries's avatar
 
Dries committed
305 306 307 308 309
  /*
  ** This function inserts common strings into the locale table (eg.
  ** names of months and days).  We use $revision and a stored variable
  ** to track if the locale table is up-to-date.
  */
Steven Wittens's avatar
Steven Wittens committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328

  $revision = 1;

  if (variable_get("locale_initialize_revision", 0) < $revision) {
    variable_set("locale_initialize_revision", $revision);
    for ($i = 1; $i <= 12; $i++) {
      $stamp = mktime(0, 0, 0, $i, 1, 1971);
      t(date("F", $stamp));
      t(date("M", $stamp));
    }

    for ($i = 0; $i <= 7; $i++) {
      $stamp = $i * 86400;
      t(date("D", $stamp));
      t(date("l", $stamp));
    }
  }
}

Dries's avatar
 
Dries committed
329 330
function locale($string) {
  global $locale;
Dries's avatar
 
Dries committed
331 332
  static $locale_t;

Dries's avatar
 
Dries committed
333
  if (!isset($locale_t)) {
Dries's avatar
 
Dries committed
334
    $cache = cache_get("locale:$locale");
335 336 337
    if ($cache == 0) {
      locale_refresh_cache();
      $cache = cache_get("locale:$locale");
Dries's avatar
 
Dries committed
338
    }
339
    $locale_t = unserialize($cache->data);
Dries's avatar
 
Dries committed
340
  }
Dries's avatar
 
Dries committed
341

342
  if (array_key_exists($string, $locale_t)) {
Dries's avatar
 
Dries committed
343
    $string = $locale_t[$string];
Dries's avatar
 
Dries committed
344 345
  }
  else {
Dries's avatar
 
Dries committed
346
    $result = db_query("SELECT lid, $locale FROM {locales} WHERE string = '%s'", $string);
Dries's avatar
 
Dries committed
347
    if (!db_fetch_object($result)) {
Dries's avatar
 
Dries committed
348
      db_query("INSERT INTO {locales} (string, location) VALUES ('%s', '%s')", $string, request_uri());
349
      cache_clear_all("locale:$locale");
Dries's avatar
 
Dries committed
350
    }
Dries's avatar
Dries committed
351
  }
Dries's avatar
 
Dries committed
352

Dries's avatar
 
Dries committed
353 354
  return $string;
}
Dries's avatar
 
Dries committed
355

Dries's avatar
Dries committed
356
?>