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

Dries's avatar
   
Dries committed
4
function locale_help($section = "admin/help#locale") {
Dries's avatar
   
Dries committed
5
6
  $output = "";

Dries's avatar
   
Dries committed
7
  switch ($section) {
Dries's avatar
   
Dries committed
8
    case 'admin/help#locale':
Dries's avatar
   
Dries committed
9
10
11
      $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>";
Dries's avatar
   
Dries committed
12
      $output .= "<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>";
Dries's avatar
   
Dries committed
13
14
15
16
17
18
19
20
      $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
21
22
23
  $output .= "<pre>
    \$languages = array(\"nl\" => \"Dutch / Nederlands\", \"fr\" => \"French / Francais\");
  </pre>";
Dries's avatar
   
Dries committed
24

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

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

Dries's avatar
   
Dries committed
57
function locale_help_page() {
Dries's avatar
   
Dries committed
58
  print theme("page", locale_help());
Dries's avatar
   
Dries committed
59
60
}

Dries's avatar
   
Dries committed
61
function locale_perm() {
Dries's avatar
   
Dries committed
62
63
64
65
  return array("administer locales");
}

function locale_link($type) {
Dries's avatar
   
Dries committed
66
67
  global $languages;

Dries's avatar
   
Dries committed
68
69
70
  if ($type == "system") {
    if (user_access("administer locales")) {

Dries's avatar
   
Dries committed
71
72
      menu("admin/locale", t("localization"), NULL, 5);
      menu("admin/locale/search", t("search string"), "locale_admin", 8);
Dries's avatar
   
Dries committed
73
      menu("admin/locale/help", t("help"), "locale_help_page", 9);
Dries's avatar
   
Dries committed
74
75
      menu("admin/locale/edit", t("edit string"), "locale_admin", 0, MENU_HIDE);
      menu("admin/locale/delete", t("delete string"), "locale_admin", 0, MENU_HIDE);
Dries's avatar
   
Dries committed
76
77

      foreach ($languages as $key => $value) {
Dries's avatar
   
Dries committed
78
79
80
        menu("admin/locale/$key", "$value", NULL);
        menu("admin/locale/$key/translated", t("translated strings"), "locale_admin");
        menu("admin/locale/$key/untranslated", t("untranslated strings"), "locale_admin");
Dries's avatar
   
Dries committed
81
      }
Dries's avatar
   
Dries committed
82
83
    }
  }
Dries's avatar
   
Dries committed
84
85
}

Dries's avatar
   
Dries committed
86
87
88
function locale_user($type, &$edit, &$user) {
  global $languages;
  if ($type == "edit_form" && count($languages) > 1) {
Dries's avatar
   
Dries committed
89
    $output = form_radios(t("Language"), "language", $user->language, $languages, t("Selecting a different language will change the language of the site."));
Dries's avatar
   
Dries committed
90
91
92
93
  }
  return $output;
}

Dries's avatar
   
Dries committed
94
function locale_delete($lid) {
Dries's avatar
   
Dries committed
95
  db_query("DELETE FROM {locales} WHERE lid = %d", $lid);
Dries's avatar
   
Dries committed
96
  locale_refresh_cache();
Dries's avatar
   
Dries committed
97
98

  return t("deleted string");
Dries's avatar
   
Dries committed
99
100
}

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

  return t("saved string");
Dries's avatar
   
Dries committed
111
112
113
114
115
116
}

function locale_refresh_cache() {
  global $languages;

  foreach (array_keys($languages) as $locale) {
Dries's avatar
   
Dries committed
117
118
119
120
121
    /*
    ** We only load short strings into the cache to improve both performance
    ** and memory usages.
    */
    $result = db_query("SELECT string, %s FROM {locales} WHERE LENGTH(string) < 75", $locale);
Dries's avatar
   
Dries committed
122
    while ($data = db_fetch_object($result)) {
123
124
125
126
127
128
      if (empty($data->$locale)) {
        $t[$data->string] = $data->string;
      }
      else {
        $t[$data->string] = $data->$locale;
      }
Dries's avatar
   
Dries committed
129
130
    }
    cache_set("locale:$locale", serialize($t));
Dries's avatar
   
Dries committed
131
132
133
  }
}

Dries's avatar
   
Dries committed
134
function locale_edit($lid) {
Dries's avatar
   
Dries committed
135
  global $languages;
Dries's avatar
   
Dries committed
136

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

Dries's avatar
   
Dries committed
140
    $form .= form_item(t("Original text"), wordwrap(drupal_specialchars($translation->string, 0)));
Dries's avatar
   
Dries committed
141
142
143
144
145

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

Dries's avatar
   
Dries committed
148
    return form($form);
Dries's avatar
   
Dries committed
149
150
151
152
  }
}

function locale_languages($translation) {
Dries's avatar
   
Dries committed
153
154
  global $languages;

Dries's avatar
   
Dries committed
155
156
  foreach ($languages as $key => $value) {
    $output .= ($translation->$key) ? "<a href=\"#\" title=\"". $translation->$key ."\">$key</a> " : "<strike>$key</strike> ";
Dries's avatar
   
Dries committed
157
158
159
160
161
  }

  return $output;
}

162
163
164
165
166
167
168
169
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
170
  }
Dries's avatar
   
Dries committed
171
  else {
172
173
174
175
176
    foreach ($_REQUEST as $key => $value) {
      if (!empty($value) && in_array($key, $fields)) {
        $query->$key = strpos(",", $value) ? explode(",", $value) : $value;
      }
    }
Dries's avatar
   
Dries committed
177
  }
178
179
  return $query;
}
Dries's avatar
   
Dries committed
180

181
182
183
184
function locale_seek() {
  global $id, $languages;
  $op = $_POST["op"];
  $query = locale_seek_query();
Dries's avatar
   
Dries committed
185

186
187
188
189
  if ($query) {

    if ($query->status) {
      switch ($query->language) {
Kjartan's avatar
Kjartan committed
190
191
        case "all":
          foreach ($languages as $key=>$value) {
192
            $tmp[] = $key . (check_query($query->status) == 1 ? " !=" : " =") ." ''";
Kjartan's avatar
Kjartan committed
193
          }
194
          $sql[] = implode(" AND ", $tmp);
Kjartan's avatar
Kjartan committed
195
196
197
          break;
        case "any":
          foreach ($languages as $key=>$value) {
198
            $tmp[] = $key . (check_query($query->status) == 1 ? " !=" : " =") ." ''";
Kjartan's avatar
Kjartan committed
199
          }
Dries's avatar
   
Dries committed
200
          $sql[] = "(". implode(" OR ", $tmp) .")";
Kjartan's avatar
Kjartan committed
201
202
          break;
        default:
203
          $sql[] = check_query($query->language) . (check_query($query->status) == 1 ? " !=" : " =") ." ''";
Kjartan's avatar
Kjartan committed
204
205
      }
    }
Dries's avatar
   
Dries committed
206

207
208
209
210
211
    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
212
213
214
        }
        else {
          foreach ($languages as $key=>$value) {
215
            $string_query[] = check_query($key) ." LIKE '%". check_query($query->string) ."%'";
Dries's avatar
   
Dries committed
216
217
218
          }
        }
      }
Dries's avatar
   
Dries committed
219
      $sql[] = "(". implode(" OR ", $string_query) .")";
Dries's avatar
   
Dries committed
220
221
    }

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

224
    $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
225
    while ($locale = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
226
      $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
227
    }
228

229
230
231
232
233
234
235
    $request = array();
    if (count($query)) {
      foreach ($query as $key => $value) {
        $request[$key] = (is_array($value)) ? implode(",", $value) : $value;
      }
    }

Dries's avatar
Dries committed
236
    if ($pager = theme("pager", NULL, 50, 0, $request)) {
237
      $rows[] = array(array("data" => "$pager", "colspan" => "5"));
Dries's avatar
   
Dries committed
238
239
    }

Dries's avatar
   
Dries committed
240
    $output .= theme("table", $header, $rows);
241

Kjartan's avatar
Kjartan committed
242
  }
Kjartan's avatar
Kjartan committed
243

Dries's avatar
   
Dries committed
244
245
  return $output;
}
Dries's avatar
   
Dries committed
246

Dries's avatar
   
Dries committed
247
248
249
function locale_seek_form() {
  global $languages;
  $edit =& $_POST["edit"];
Dries's avatar
   
Dries committed
250
  $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
251
252
  $form .= form_radios(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_radios(t("Status"), "status", $edit["status"], array(2 => t("Untranslated"), 1 => t("Translated"), 0 => t("All")));
Dries's avatar
   
Dries committed
253
  $form .= form_submit(t("Search"));
Dries's avatar
   
Dries committed
254

Dries's avatar
   
Dries committed
255
  $output .= form($form);
Kjartan's avatar
Kjartan committed
256
257
258
259

  return $output;
}

Dries's avatar
   
Dries committed
260
function locale_admin() {
Dries's avatar
   
Dries committed
261
  $op = $_POST["op"];
Dries's avatar
   
Dries committed
262
  $edit =& $_POST["edit"];
Dries's avatar
   
Dries committed
263

Dries's avatar
   
Dries committed
264
  if (user_access("administer locales")) {
Steven Wittens's avatar
Steven Wittens committed
265
266
    locale_admin_initialize();

Dries's avatar
   
Dries committed
267
268
269
    if (empty($op)) {
      $op = arg(2);
    }
Dries's avatar
   
Dries committed
270
271
272

    switch ($op) {
      case "delete":
Dries's avatar
   
Dries committed
273
        drupal_set_message(locale_delete(check_query(arg(3))));
Dries's avatar
   
Dries committed
274
        $output .= locale_seek();
Dries's avatar
   
Dries committed
275
276
        break;
      case "edit":
Dries's avatar
   
Dries committed
277
        $output = locale_edit(check_query(arg(3)));
Dries's avatar
   
Dries committed
278
        break;
279
      case "search":
280
281
282
283
        if (locale_seek_query()) {
          $output = locale_seek();
        }
        $output .= locale_seek_form();
Dries's avatar
   
Dries committed
284
        break;
Dries's avatar
   
Dries committed
285
      case t("Search"):
Dries's avatar
   
Dries committed
286
287
        $output = locale_seek();
        $output .= locale_seek_form();
Dries's avatar
   
Dries committed
288
        break;
Dries's avatar
   
Dries committed
289
      case t("Save translations"):
Dries's avatar
   
Dries committed
290
        drupal_set_message(locale_save(check_query(arg(3))));
Dries's avatar
   
Dries committed
291
        $output .= locale_seek();
Kjartan's avatar
Kjartan committed
292
        break;
Dries's avatar
   
Dries committed
293
      default:
Dries's avatar
   
Dries committed
294
295
296
297
298
299
300
301
        if (arg(3) == "translated") {
          $edit["status"] = 1;
          $edit["language"] = arg(2);
        }
        else {
          $edit["status"] = 2;
          $edit["language"] = arg(2);
        }
Dries's avatar
   
Dries committed
302
303
        $output = locale_seek();
        $output .= locale_seek_form();
Dries's avatar
   
Dries committed
304
    }
Dries's avatar
   
Dries committed
305
    print theme("page", $output);
Dries's avatar
   
Dries committed
306
307
  }
  else {
Dries's avatar
   
Dries committed
308
    print theme("page", message_access());
Dries's avatar
   
Dries committed
309
310
311
  }
}

Steven Wittens's avatar
Steven Wittens committed
312
function locale_admin_initialize() {
Dries's avatar
   
Dries committed
313
314
315
316
317
  /*
  ** 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336

  $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
337
338
function locale($string) {
  global $locale;
Dries's avatar
   
Dries committed
339
340
  static $locale_t;

Dries's avatar
   
Dries committed
341
  if (!isset($locale_t)) {
Dries's avatar
   
Dries committed
342
    $cache = cache_get("locale:$locale");
Dries's avatar
   
Dries committed
343

344
345
346
    if ($cache == 0) {
      locale_refresh_cache();
      $cache = cache_get("locale:$locale");
Dries's avatar
   
Dries committed
347
    }
348
    $locale_t = unserialize($cache->data);
Dries's avatar
   
Dries committed
349
  }
Dries's avatar
   
Dries committed
350

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

Dries's avatar
   
Dries committed
368
369
  return $string;
}
Dries's avatar
   
Dries committed
370

Dries's avatar
Dries committed
371
?>