locale.module 14.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
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
  if ($type == "admin" && user_access("administer locales")) {
Dries's avatar
   
Dries committed
73

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

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

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

  return t("deleted string");
Dries's avatar
   
Dries committed
93
94
}

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

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

function locale_refresh_cache() {
  global $languages;

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

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

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

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

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

Dries's avatar
   
Dries committed
138
    return form($form);
Dries's avatar
   
Dries committed
139
140
141
142
  }
}

function locale_languages($translation) {
Dries's avatar
   
Dries committed
143
144
  global $languages;

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

  return $output;
}

Kjartan's avatar
Kjartan committed
152
function locale_seek() {
Dries's avatar
   
Dries committed
153
  global $id, $languages;
Dries's avatar
   
Dries committed
154
  $op = $_POST["op"];
Dries's avatar
   
Dries committed
155
  $edit =& $_POST["edit"];
Kjartan's avatar
Kjartan committed
156

Dries's avatar
   
Dries committed
157
158
  if ($op != 'overview' && !$edit && isset($_SESSION["locale_settings"])) {
    $edit = $_SESSION["locale_settings"];
Kjartan's avatar
Kjartan committed
159
  }
Dries's avatar
   
Dries committed
160
  else {
Dries's avatar
   
Dries committed
161
    $_SESSION["locale_settings"] = $edit;
Dries's avatar
   
Dries committed
162
163
  }

164
  if (is_array($edit)) {
Dries's avatar
   
Dries committed
165
166
167

    if ($edit["status"]) {
      switch ($edit["language"]) {
Kjartan's avatar
Kjartan committed
168
169
        case "all":
          foreach ($languages as $key=>$value) {
Dries's avatar
   
Dries committed
170
            $tmp[] = $key . (check_query($edit["status"]) == 1 ? " !=" : " =") ." ''";
Kjartan's avatar
Kjartan committed
171
          }
Dries's avatar
   
Dries committed
172
          $query[] = implode(" AND ", $tmp);
Kjartan's avatar
Kjartan committed
173
174
175
          break;
        case "any":
          foreach ($languages as $key=>$value) {
Dries's avatar
   
Dries committed
176
            $tmp[] = $key . (check_query($edit["status"]) == 1 ? " !=" : " =") ." ''";
Kjartan's avatar
Kjartan committed
177
178
179
180
          }
          $query[] = "(". implode(" || ", $tmp) .")";
          break;
        default:
Dries's avatar
   
Dries committed
181
          $query[] = check_query($edit["language"]) . (check_query($edit["status"]) == 1 ? " !=" : " =") ." ''";
Kjartan's avatar
Kjartan committed
182
183
      }
    }
Dries's avatar
   
Dries committed
184

Dries's avatar
   
Dries committed
185
186
187
188
    if ($edit["string"]) {
      $string_query[] = "string LIKE '%". check_query($edit["string"]) ."%'";
      if ($edit["status"] != 2) {
        if (strlen($edit["language"]) == 2) {
Kjartan's avatar
Kjartan committed
189
          $string_query[] = check_query($edit["language"]) ." LIKE '%". check_query($edit["string"]) ."%'";
Dries's avatar
   
Dries committed
190
191
192
        }
        else {
          foreach ($languages as $key=>$value) {
Kjartan's avatar
Kjartan committed
193
            $string_query[] = check_query($key) ." LIKE '%". check_query($edit["string"]) ."%'";
Dries's avatar
   
Dries committed
194
195
196
          }
        }
      }
Kjartan's avatar
Kjartan committed
197
      $query[] = "(". implode(" || ", $string_query) .")";
Dries's avatar
   
Dries committed
198
199
    }

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

Dries's avatar
   
Dries committed
202
    $header = array(t("string"), (($edit["status"] != 2 && strlen($edit["language"]) == 2) ? t("translated string") : t("languages")), array("data" => t("operations"), "colspan" => "2"));
Kjartan's avatar
Kjartan committed
203
    while ($locale = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
204
      $rows[] = array("$locale->string<br /><small><i>$locale->location</i></small>", array("data" => (($edit["status"] != 2 && strlen($edit["language"]) == 2) ? $locale->$edit["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
205
    }
206
207
208

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

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

Kjartan's avatar
Kjartan committed
213
  }
Kjartan's avatar
Kjartan committed
214

Dries's avatar
   
Dries committed
215
216
  return $output;
}
Dries's avatar
   
Dries committed
217

Dries's avatar
   
Dries committed
218
219
220
function locale_seek_form() {
  global $languages;
  $edit =& $_POST["edit"];
Dries's avatar
   
Dries committed
221
  $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
222
223
224
  $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
225

Dries's avatar
   
Dries committed
226
  $output .= form($form);
Kjartan's avatar
Kjartan committed
227
228
229
230

  return $output;
}

Dries's avatar
   
Dries committed
231
function locale_admin() {
Dries's avatar
   
Dries committed
232
  $op = $_POST["op"];
Dries's avatar
   
Dries committed
233
  $edit =& $_POST["edit"];
Dries's avatar
   
Dries committed
234

Dries's avatar
   
Dries committed
235
  if (user_access("administer locales")) {
Steven Wittens's avatar
Steven Wittens committed
236
237
    locale_admin_initialize();

Dries's avatar
   
Dries committed
238
239
240
    if (empty($op)) {
      $op = arg(2);
    }
Dries's avatar
   
Dries committed
241
242
243

    switch ($op) {
      case "delete":
Dries's avatar
   
Dries committed
244
245
        $output = status(locale_delete(check_query(arg(3))));
        $output .= locale_seek();
Dries's avatar
   
Dries committed
246
247
        break;
      case "edit":
Dries's avatar
   
Dries committed
248
        $output = locale_edit(check_query(arg(3)));
Dries's avatar
   
Dries committed
249
        break;
250
      case "search":
Dries's avatar
   
Dries committed
251
        $output = locale_seek_form();
Dries's avatar
   
Dries committed
252
        break;
Dries's avatar
   
Dries committed
253
      case t("Search"):
Dries's avatar
   
Dries committed
254
255
        $output = locale_seek();
        $output .= locale_seek_form();
Dries's avatar
   
Dries committed
256
        break;
Dries's avatar
   
Dries committed
257
      case t("Save translations"):
Dries's avatar
   
Dries committed
258
259
        $output = status(locale_save(check_query(arg(3))));
        $output .= locale_seek();
Kjartan's avatar
Kjartan committed
260
        break;
Dries's avatar
   
Dries committed
261
      default:
Dries's avatar
   
Dries committed
262
263
264
265
266
267
268
269
        if (arg(3) == "translated") {
          $edit["status"] = 1;
          $edit["language"] = arg(2);
        }
        else {
          $edit["status"] = 2;
          $edit["language"] = arg(2);
        }
Dries's avatar
   
Dries committed
270
271
        $output = locale_seek();
        $output .= locale_seek_form();
Dries's avatar
   
Dries committed
272
    }
Dries's avatar
   
Dries committed
273
    return $output;
Dries's avatar
   
Dries committed
274
275
  }
  else {
Dries's avatar
   
Dries committed
276
    return message_access();
Dries's avatar
   
Dries committed
277
278
279
  }
}

Steven Wittens's avatar
Steven Wittens committed
280
function locale_admin_initialize() {
Dries's avatar
   
Dries committed
281
282
283
284
285
  /*
  ** 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304

  $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
305
306
function locale($string) {
  global $locale;
Dries's avatar
   
Dries committed
307
308
  static $locale_t;

Dries's avatar
   
Dries committed
309
  if (!isset($locale_t)) {
Dries's avatar
   
Dries committed
310
    $cache = cache_get("locale:$locale");
311
312
313
    if ($cache == 0) {
      locale_refresh_cache();
      $cache = cache_get("locale:$locale");
Dries's avatar
   
Dries committed
314
    }
315
    $locale_t = unserialize($cache->data);
Dries's avatar
   
Dries committed
316
  }
Dries's avatar
   
Dries committed
317

318
  if (array_key_exists($string, $locale_t)) {
Dries's avatar
   
Dries committed
319
    $string = $locale_t[$string];
Dries's avatar
   
Dries committed
320
321
  }
  else {
Dries's avatar
   
Dries committed
322
    $result = db_query("SELECT lid, $locale FROM {locales} WHERE string = '%s'", $string);
Dries's avatar
   
Dries committed
323
    if (!db_fetch_object($result)) {
Dries's avatar
   
Dries committed
324
      db_query("INSERT INTO {locales} (string, location) VALUES ('%s', '%s')", $string, request_uri());
325
      cache_clear_all("locale:$locale");
Dries's avatar
   
Dries committed
326
    }
Dries's avatar
Dries committed
327
  }
Dries's avatar
   
Dries committed
328

Dries's avatar
   
Dries committed
329
330
  return $string;
}
Dries's avatar
   
Dries committed
331

Dries's avatar
Dries committed
332
?>