locale.module 15.2 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
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>";
Dries's avatar
   
Dries committed
13
      $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
14
15
16
17
18
19
20
21
      $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
      $output = t($output,  array("%overview" => l(t("overview"), "admin/locale") ));
Dries's avatar
   
Dries committed
37
38
      break;
    case 'admin/system/modules':
Dries's avatar
   
Dries committed
39
      $output = t("Enables the translation of the user interface to languages other than English.");
Dries's avatar
   
Dries committed
40
41
      break;
    case 'admin/locale':
Dries's avatar
   
Dries committed
42
      $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
43
44
      break;
    case 'admin/locale/search':
Dries's avatar
   
Dries committed
45
      $output = t("Search the localization database. ('*' can be used as a wildcard)");
Dries's avatar
   
Dries committed
46
47
      break;
    case 'admin/locale/translated':
Dries's avatar
   
Dries committed
48
      $output = t("Start by searching the translated strings.");
Dries's avatar
   
Dries committed
49
50
      break;
    case 'admin/locale/untranslated':
Dries's avatar
   
Dries committed
51
      $output = t("Start by searching the untranslated strings.");
Dries's avatar
   
Dries committed
52
53
54
      break;
  }

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

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

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

  return $output;
64
65
}

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

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

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

Dries's avatar
   
Dries committed
76
77
78
79
80
      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
81
82
83

      foreach ($languages as $key => $value) {
        menu("admin/locale/$key", "$value", NULL, locale_help("admin/locale"));
Dries's avatar
   
Dries committed
84
85
        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
86
      }
Dries's avatar
   
Dries committed
87
88
    }
  }
Dries's avatar
   
Dries committed
89
90
}

Dries's avatar
   
Dries committed
91
92
93
94
95
96
97
98
function locale_user($type, &$edit, &$user) {
  global $languages;
  if ($type == "edit_form" && count($languages) > 1) {
    $output = form_select(t("Language"), "language", $user->language, $languages, t("Selecting a different language will change the language of the site."));
  }
  return $output;
}

Dries's avatar
   
Dries committed
99
function locale_delete($lid) {
Dries's avatar
   
Dries committed
100
  db_query("DELETE FROM {locales} WHERE lid = %d", $lid);
Dries's avatar
   
Dries committed
101
  locale_refresh_cache();
Dries's avatar
   
Dries committed
102
103

  return t("deleted string");
Dries's avatar
   
Dries committed
104
105
}

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

  return t("saved string");
Dries's avatar
   
Dries committed
116
117
118
119
120
121
}

function locale_refresh_cache() {
  global $languages;

  foreach (array_keys($languages) as $locale) {
Dries's avatar
   
Dries committed
122
    $result = db_query("SELECT string, %s FROM {locales} ", $locale);
Dries's avatar
   
Dries committed
123
    while ($data = db_fetch_object($result)) {
124
125
126
127
128
129
      if (empty($data->$locale)) {
        $t[$data->string] = $data->string;
      }
      else {
        $t[$data->string] = $data->$locale;
      }
Dries's avatar
   
Dries committed
130
131
    }
    cache_set("locale:$locale", serialize($t));
Dries's avatar
   
Dries committed
132
133
134
  }
}

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

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

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

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

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

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

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

  return $output;
}

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

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

187
188
189
190
  if ($query) {

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

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

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

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

230
231
232
233
234
235
236
237
    $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)) {
238
      $rows[] = array(array("data" => "$pager", "colspan" => "5"));
Dries's avatar
   
Dries committed
239
240
    }

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

Kjartan's avatar
Kjartan committed
243
  }
Kjartan's avatar
Kjartan committed
244

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

Dries's avatar
   
Dries committed
248
249
250
function locale_seek_form() {
  global $languages;
  $edit =& $_POST["edit"];
Dries's avatar
   
Dries committed
251
  $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
252
253
254
  $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
255

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

  return $output;
}

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

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

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

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

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

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

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

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

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

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

Dries's avatar
Dries committed
366
?>