locale.module 13.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() {
Dries's avatar
   
Dries committed
5
6
7
8
9
10
11
12
13
14
15
16
17
  $output .= "<p>Normally programs are written and documented in English, and use English to interact with users.  This is true for a great deal of web sites.  However, most people are less comfortable with English than with their own 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 of 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 texts in English.  We explored the various alternatives to support internationalization and decided to design the framework in such a way that the impact of internationalization on drupal's sources is minimized, modular and that it 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><p>The actual translation starts at the ". l("overview","admin/locale") ." of the locale section of the administration pages. In the \"localization\" section 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>";
  $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 URI where this text shows up one your site.  Changes 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><p>Adding a new language requires you to edit your configuration file and to edit 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>";
  $output .= "<pre>
    \$languages = array(\"nl\" => \"Dutch / Nederlands\", \"fr\" => \"French / Francais\");
  </pre>";
Dries's avatar
   
Dries committed
18

Dries's avatar
   
Dries committed
19
20
21
22
23
24
  $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>
    \$languages = array(\"en\" => \"English\", \"nl\" => \"Dutch / Nederlands\", \"fr\" => \"French / Francais\");
  </pre>";
  $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
25
26
27
    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
28
29
  </pre>";
  return t($output);
Dries's avatar
   
Dries committed
30
31
}

32
function locale_system($field){
Kjartan's avatar
Kjartan committed
33
  $system["description"] = t("Enables the translation of the user interface to languages other than English.");
34
35
36
  return $system[$field];
}

Dries's avatar
   
Dries committed
37
function locale_perm() {
Dries's avatar
   
Dries committed
38
39
40
41
  return array("administer locales");
}

function locale_link($type) {
Dries's avatar
   
Dries committed
42
43
  global $languages;

Dries's avatar
   
Dries committed
44
  if ($type == "admin" && user_access("administer locales")) {
Dries's avatar
   
Dries committed
45
46
47
48
    $help["general"] = 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.");
    $help["search"] = t("Search the localization database. ('*' can be used as a wildcard)");
    $help["translated"] = t("Start by searching the translated strings.");
    $help["untranslated"] = t("Start by searching the untranslated strings.");
Dries's avatar
   
Dries committed
49
50

    menu("admin/locale", "localization", NULL, $help["general"], 5);
Dries's avatar
   
Dries committed
51
    menu("admin/locale/search", "search string", "locale_admin", $help["search"], 8);
Dries's avatar
   
Dries committed
52
53
54
    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
55

Dries's avatar
   
Dries committed
56
    foreach ($languages as $key => $value) {
Dries's avatar
   
Dries committed
57
      menu("admin/locale/$key", "$value", NULL, $help["general"]);
Dries's avatar
   
Dries committed
58
59
      menu("admin/locale/$key/translated", "translated strings", "locale_admin", $help["translated"]);
      menu("admin/locale/$key/untranslated", "untranslated strings", "locale_admin", $help["untranslated"]);
Dries's avatar
   
Dries committed
60
61
    }
  }
Dries's avatar
   
Dries committed
62
63
}

Dries's avatar
   
Dries committed
64
function locale_delete($lid) {
Dries's avatar
   
Dries committed
65
  db_query("DELETE FROM locales WHERE lid = %d", $lid);
Dries's avatar
   
Dries committed
66
  locale_refresh_cache();
Dries's avatar
   
Dries committed
67
68

  return t("deleted string");
Dries's avatar
   
Dries committed
69
70
}

Dries's avatar
   
Dries committed
71
function locale_save($lid) {
Dries's avatar
   
Dries committed
72
  $edit = $_POST["edit"];
Dries's avatar
   
Dries committed
73
  foreach ($edit as $key=>$value) {
Dries's avatar
   
Dries committed
74
    db_query("UPDATE locales SET $key = '%s' WHERE lid = %d", $value, $lid);
Dries's avatar
   
Dries committed
75
76
  }
  locale_refresh_cache();
Dries's avatar
   
Dries committed
77
78
  // delete form data so it will remember where it came from
  $edit = '';
Dries's avatar
   
Dries committed
79
80

  return t("saved string");
Dries's avatar
   
Dries committed
81
82
83
84
85
86
}

function locale_refresh_cache() {
  global $languages;

  foreach (array_keys($languages) as $locale) {
Dries's avatar
   
Dries committed
87
    $result = db_query("SELECT string, %s FROM locales", $locale);
Dries's avatar
   
Dries committed
88
    while ($data = db_fetch_object($result)) {
89
90
91
92
93
94
      if (empty($data->$locale)) {
        $t[$data->string] = $data->string;
      }
      else {
        $t[$data->string] = $data->$locale;
      }
Dries's avatar
   
Dries committed
95
96
    }
    cache_set("locale:$locale", serialize($t));
Dries's avatar
   
Dries committed
97
98
99
  }
}

Dries's avatar
   
Dries committed
100
function locale_edit($lid) {
Dries's avatar
   
Dries committed
101
  global $languages;
Dries's avatar
   
Dries committed
102

Dries's avatar
   
Dries committed
103
  $result = db_query("SELECT * FROM locales WHERE lid = '$lid'");
Dries's avatar
   
Dries committed
104
  if ($translation = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
105

Dries's avatar
   
Dries committed
106
    $form .= form_item(t("Original text"), wordwrap(drupal_specialchars($translation->string, 0)));
Dries's avatar
   
Dries committed
107
108
109
110
111

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

Dries's avatar
   
Dries committed
114
    return form($form);
Dries's avatar
   
Dries committed
115
116
117
118
  }
}

function locale_languages($translation) {
Dries's avatar
   
Dries committed
119
120
  global $languages;

Dries's avatar
   
Dries committed
121
122
  foreach ($languages as $key => $value) {
    $output .= ($translation->$key) ? "<a href=\"#\" title=\"". $translation->$key ."\">$key</a> " : "<strike>$key</strike> ";
Dries's avatar
   
Dries committed
123
124
125
126
127
  }

  return $output;
}

Kjartan's avatar
Kjartan committed
128
function locale_seek() {
Dries's avatar
   
Dries committed
129
  global $id, $languages;
Dries's avatar
   
Dries committed
130
131
  $op = $_POST["op"];
  $edit = $_POST["edit"];
Kjartan's avatar
Kjartan committed
132

Dries's avatar
   
Dries committed
133
134
  if ($op != 'overview' && !$edit && isset($_SESSION["locale_settings"])) {
    $edit = $_SESSION["locale_settings"];
Kjartan's avatar
Kjartan committed
135
  }
Dries's avatar
   
Dries committed
136
  else {
Dries's avatar
   
Dries committed
137
    $_SESSION["locale_settings"] = $edit;
Dries's avatar
   
Dries committed
138
139
  }

140
  if (is_array($edit)) {
Dries's avatar
   
Dries committed
141
142
143

    if ($edit["status"]) {
      switch ($edit["language"]) {
Kjartan's avatar
Kjartan committed
144
145
        case "all":
          foreach ($languages as $key=>$value) {
Dries's avatar
   
Dries committed
146
            $tmp[] = $key . (check_query($edit["status"]) == 1 ? " !=" : " =") ." ''";
Kjartan's avatar
Kjartan committed
147
          }
Dries's avatar
   
Dries committed
148
          $query[] = implode(" AND ", $tmp);
Kjartan's avatar
Kjartan committed
149
150
151
          break;
        case "any":
          foreach ($languages as $key=>$value) {
Dries's avatar
   
Dries committed
152
            $tmp[] = $key . (check_query($edit["status"]) == 1 ? " !=" : " =") ." ''";
Kjartan's avatar
Kjartan committed
153
154
155
156
          }
          $query[] = "(". implode(" || ", $tmp) .")";
          break;
        default:
Dries's avatar
   
Dries committed
157
          $query[] = check_query($edit["language"]) . (check_query($edit["status"]) == 1 ? " !=" : " =") ." ''";
Kjartan's avatar
Kjartan committed
158
159
      }
    }
Dries's avatar
   
Dries committed
160

Dries's avatar
   
Dries committed
161
162
163
164
    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
165
          $string_query[] = check_query($edit["language"]) ." LIKE '%". check_query($edit["string"]) ."%'";
Dries's avatar
   
Dries committed
166
167
168
        }
        else {
          foreach ($languages as $key=>$value) {
Kjartan's avatar
Kjartan committed
169
            $string_query[] = check_query($key) ." LIKE '%". check_query($edit["string"]) ."%'";
Dries's avatar
   
Dries committed
170
171
172
          }
        }
      }
Kjartan's avatar
Kjartan committed
173
      $query[] = "(". implode(" || ", $string_query) .")";
Dries's avatar
   
Dries committed
174
175
    }

Kjartan's avatar
Kjartan committed
176
    $result = db_query("SELECT * FROM locales". (count($query) ? " WHERE ". implode(" && ", $query) : "") ." ORDER BY string");
Dries's avatar
   
Dries committed
177

Dries's avatar
   
Dries committed
178
    $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
179
    while ($locale = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
180
      $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
181
    }
Dries's avatar
   
Dries committed
182
    $output .= table($header, $rows);
Kjartan's avatar
Kjartan committed
183
  }
Kjartan's avatar
Kjartan committed
184

Dries's avatar
   
Dries committed
185
186
  reset($languages);

Dries's avatar
   
Dries committed
187
  $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
188
189
190
  $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
191

Dries's avatar
   
Dries committed
192
  $output .= form($form);
Kjartan's avatar
Kjartan committed
193
194
195
196

  return $output;
}

Dries's avatar
   
Dries committed
197
function locale_admin() {
Dries's avatar
   
Dries committed
198
199
  $op = $_POST["op"];
  $edit = $_POST["edit"];
Dries's avatar
   
Dries committed
200

Dries's avatar
   
Dries committed
201
  if (user_access("administer locales")) {
Steven Wittens's avatar
Steven Wittens committed
202
203
    locale_admin_initialize();

Dries's avatar
   
Dries committed
204
205
206
    if (empty($op)) {
      $op = arg(2);
    }
Dries's avatar
   
Dries committed
207
208
209

    switch ($op) {
      case "delete":
Dries's avatar
   
Dries committed
210
        print status(locale_delete(check_query(arg(3))));
Dries's avatar
   
Dries committed
211
        print locale_seek();
Dries's avatar
   
Dries committed
212
213
        break;
      case "edit":
Dries's avatar
   
Dries committed
214
        print locale_edit(check_query(arg(3)));
Dries's avatar
   
Dries committed
215
        break;
216
      case "search":
Dries's avatar
   
Dries committed
217
      case t("Search"):
Dries's avatar
   
Dries committed
218
219
        print locale_seek();
        break;
Dries's avatar
   
Dries committed
220
221
      case t("Save translations"):
        print status(locale_save(check_query(arg(3))));
Dries's avatar
   
Dries committed
222
        print locale_seek();
Kjartan's avatar
Kjartan committed
223
        break;
Dries's avatar
   
Dries committed
224
      default:
Dries's avatar
   
Dries committed
225
226
227
228
229
230
231
232
233
234
        if (arg(3) == "translated") {
          $edit["status"] = 1;
          $edit["language"] = arg(2);
          print locale_seek();
        }
        else {
          $edit["status"] = 2;
          $edit["language"] = arg(2);
          print locale_seek();
        }
Dries's avatar
   
Dries committed
235
236
237
238
    }
  }
  else {
    print message_access();
Dries's avatar
   
Dries committed
239
240
241
  }
}

Steven Wittens's avatar
Steven Wittens committed
242
function locale_admin_initialize() {
Dries's avatar
   
Dries committed
243
244
245
246
247
  /*
  ** 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266

  $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
267
268
function locale($string) {
  global $locale;
Dries's avatar
   
Dries committed
269
270
  static $locale_t;

Dries's avatar
   
Dries committed
271
  if (!isset($locale_t)) {
Dries's avatar
   
Dries committed
272
    $cache = cache_get("locale:$locale");
273
274
275
    if ($cache == 0) {
      locale_refresh_cache();
      $cache = cache_get("locale:$locale");
Dries's avatar
   
Dries committed
276
    }
277
    $locale_t = unserialize($cache->data);
Dries's avatar
   
Dries committed
278
  }
Dries's avatar
   
Dries committed
279

Dries's avatar
   
Dries committed
280
  if ($locale_t[$string] != "") {
Dries's avatar
   
Dries committed
281
    $string = $locale_t[$string];
Dries's avatar
   
Dries committed
282
283
284
285
  }
  else {
    $result = db_query("SELECT lid, $locale FROM locales WHERE string = '%s'", $string);
    if (!db_fetch_object($result)) {
286
      db_query("INSERT INTO locales (string, location) VALUES ('%s', '%s')", $string, request_uri());
287
      cache_clear_all("locale:$locale");
Dries's avatar
   
Dries committed
288
    }
Dries's avatar
Dries committed
289
  }
Dries's avatar
   
Dries committed
290

Dries's avatar
   
Dries committed
291
292
  return $string;
}
Dries's avatar
   
Dries committed
293

Dries's avatar
Dries committed
294
?>