locale.module 11.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
function locale_help() {
Dries's avatar
   
Dries committed
5
 ?>
Dries's avatar
   
Dries committed
6
  <p>Normally programs are written and documented in English, and use English to interact with users.  This is true for a great deal of websites.  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 website showing a lot less of English, and far more of their own language.</p>
Dries's avatar
   
Dries committed
7
  <p>Therefore Drupal provides a framework to setup a multi-lingual website, 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>
Dries's avatar
   
Dries committed
8

Dries's avatar
   
Dries committed
9
  <h3>How to translate texts</h3>
Dries's avatar
   
Dries committed
10

Dries's avatar
   
Dries committed
11
12
13
14
15
16
17
  <p>The actual translation starts at the "overview" of the locale page of the administration pages.  To allow a user to maintain the translations, he obviously needs access to the locale module.  See the account documentation for more information on roles and permissions.</p>
  <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>
  <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>
  <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>
  <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>
  <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>
  <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>
Dries's avatar
   
Dries committed
18

Dries's avatar
   
Dries committed
19
  <h3>How to add new languages</h3>
Dries's avatar
   
Dries committed
20

Dries's avatar
   
Dries committed
21
22
  <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>
  <pre>
Dries's avatar
   
Dries committed
23
    $languages = array("nl" => "Dutch / Nederlands", "fr" => "French / Francais");
Dries's avatar
   
Dries committed
24
25
26
  </pre>
  <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>
  <pre>
Dries's avatar
   
Dries committed
27
    $languages = array("en" => "English", "nl" => "Dutch / Nederlands", "fr" => "French / Francais");
Dries's avatar
   
Dries committed
28
29
30
  </pre>
  <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>
  <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
 <?php
Dries's avatar
   
Dries committed
36
37
}

38
function locale_system($field){
Kjartan's avatar
Kjartan committed
39
  $system["description"] = t("Enables the translation of the user interface to languages other than English.");
40
41
42
  return $system[$field];
}

Dries's avatar
   
Dries committed
43
function locale_perm() {
Dries's avatar
   
Dries committed
44
45
46
47
  return array("administer locales");
}

function locale_link($type) {
Dries's avatar
   
Dries committed
48
49
  global $languages;

Dries's avatar
   
Dries committed
50
  if ($type == "admin" && user_access("administer locales")) {
Dries's avatar
   
Dries committed
51
52
53
    menu_add("locale", url("admin/locale"), "Translate this site.", NULL, NULL, 3);
    menu_add("search string", url("admin/locale/search"), "Search a string.", NULL, "locale", 8);
    menu_add("help", url("admin/locale/help"), "More information about the locale system.", NULL, "locale", 9);
Dries's avatar
   
Dries committed
54

Dries's avatar
   
Dries committed
55
    foreach ($languages as $key => $value) {
Dries's avatar
   
Dries committed
56
57
      menu_add("translated '$key' strings", url("admin/locale/translated/$key"), "Display translated '$key' strings.", NULL, "locale");
      menu_add("untranslated '$key' strings", url("admin/locale/untranslated/$key"), "Display untranslated '$key' strings.", NULL, "locale");
Dries's avatar
   
Dries committed
58
59
    }
  }
Dries's avatar
   
Dries committed
60
61
}

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

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

function locale_refresh_cache() {
  global $languages;

  foreach (array_keys($languages) as $locale) {
Dries's avatar
   
Dries committed
81
    $result = db_query("SELECT string, %s FROM locales", $locale);
Dries's avatar
   
Dries committed
82
83
84
85
    while ($data = db_fetch_object($result)) {
      $t[$data->string] = $data->$locale;
    }
    cache_set("locale:$locale", serialize($t));
Dries's avatar
   
Dries committed
86
87
88
  }
}

Dries's avatar
   
Dries committed
89
function locale_edit($lid) {
Dries's avatar
   
Dries committed
90
  global $languages;
Dries's avatar
   
Dries committed
91

Dries's avatar
   
Dries committed
92
  $result = db_query("SELECT * FROM locales WHERE lid = '$lid'");
Dries's avatar
   
Dries committed
93
  if ($translation = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
94
    $form .= form_item(t("Original text"), "<pre>". wordwrap($translation->string) ."</pre>");
Dries's avatar
   
Dries committed
95
    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
96
    $form .= form_submit(t("Save translations"));
Dries's avatar
   
Dries committed
97

Dries's avatar
   
Dries committed
98
    return form($form);
Dries's avatar
   
Dries committed
99
100
101
102
  }
}

function locale_languages($translation) {
Dries's avatar
   
Dries committed
103
104
  global $languages;

Dries's avatar
   
Dries committed
105
106
  foreach ($languages as $key => $value) {
    $output .= ($translation->$key) ? "<a href=\"#\" title=\"". $translation->$key ."\">$key</a> " : "<strike>$key</strike> ";
Dries's avatar
   
Dries committed
107
108
109
110
111
  }

  return $output;
}

Kjartan's avatar
Kjartan committed
112
function locale_seek() {
Dries's avatar
   
Dries committed
113
  global $id, $edit, $languages, $op, $locale_settings;
Kjartan's avatar
Kjartan committed
114

Dries's avatar
   
Dries committed
115
  if ($op != 'overview' && !$edit && session_is_registered("locale_settings")) {
Kjartan's avatar
Kjartan committed
116
117
    $edit = $locale_settings;
  }
Dries's avatar
   
Dries committed
118
  else {
Kjartan's avatar
Kjartan committed
119
120
    $locale_settings = $edit;
    session_register("locale_settings");
Dries's avatar
   
Dries committed
121
122
  }

123
  if (is_array($edit)) {
Dries's avatar
   
Dries committed
124
125
126

    if ($edit["status"]) {
      switch ($edit["language"]) {
Kjartan's avatar
Kjartan committed
127
128
        case "all":
          foreach ($languages as $key=>$value) {
Dries's avatar
   
Dries committed
129
            $tmp[] = $key . (check_query($edit["status"]) == 1 ? " !=" : " =") ." ''";
Kjartan's avatar
Kjartan committed
130
131
132
133
134
          }
          $query[] = implode(" && ", $tmp);
          break;
        case "any":
          foreach ($languages as $key=>$value) {
Dries's avatar
   
Dries committed
135
            $tmp[] = $key . (check_query($edit["status"]) == 1 ? " !=" : " =") ." ''";
Kjartan's avatar
Kjartan committed
136
137
138
139
          }
          $query[] = "(". implode(" || ", $tmp) .")";
          break;
        default:
Dries's avatar
   
Dries committed
140
          $query[] = check_query($edit["language"]) . (check_query($edit["status"]) == 1 ? " !=" : " =") ." ''";
Kjartan's avatar
Kjartan committed
141
142
      }
    }
Dries's avatar
   
Dries committed
143

Dries's avatar
   
Dries committed
144
145
146
147
148
149
150
151
    if ($edit["module"]) {
      $query[] = "location LIKE '%mod=". (check_query($edit["module"]) != "all" ? check_query($edit["module"]) : "") ."%'";
    }

    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
152
          $string_query[] = check_query($edit["language"]) ." LIKE '%". check_query($edit["string"]) ."%'";
Dries's avatar
   
Dries committed
153
154
155
        }
        else {
          foreach ($languages as $key=>$value) {
Kjartan's avatar
Kjartan committed
156
            $string_query[] = check_query($key) ." LIKE '%". check_query($edit["string"]) ."%'";
Dries's avatar
   
Dries committed
157
158
159
          }
        }
      }
Kjartan's avatar
Kjartan committed
160
      $query[] = "(". implode(" || ", $string_query) .")";
Dries's avatar
   
Dries committed
161
162
    }

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

Dries's avatar
   
Dries committed
165
    $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
166
    while ($locale = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
167
      $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
168
    }
Dries's avatar
   
Dries committed
169
    $output .= table($header, $rows);
Kjartan's avatar
Kjartan committed
170
  }
Kjartan's avatar
Kjartan committed
171

Dries's avatar
   
Dries committed
172
173
  reset($languages);

Dries's avatar
   
Dries committed
174
175
176
177
178
  $form .= form_textfield(t("String"), "string", $edit["string"], 30, 30, t("Leave blank to show all strings. This is treated as a regular expression."));
  $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_select(t("Module"), "module", $edit["module"], array_merge(array("0" => t("All modules + pages"), "all" => t("All modules")), module_list()));
  $form .= form_submit(t("Search"));
Dries's avatar
   
Dries committed
179

Dries's avatar
   
Dries committed
180
  $output .= form($form);
Kjartan's avatar
Kjartan committed
181
182
183
184

  return $output;
}

Dries's avatar
   
Dries committed
185
function locale_admin() {
Dries's avatar
   
Dries committed
186
  global $op, $edit;
Dries's avatar
   
Dries committed
187

Dries's avatar
   
Dries committed
188
  if (user_access("administer locales")) {
Steven Wittens's avatar
Steven Wittens committed
189
190
    locale_admin_initialize();

Dries's avatar
   
Dries committed
191
192
193
    if (empty($op)) {
      $op = arg(2);
    }
Dries's avatar
   
Dries committed
194
195
196

    switch ($op) {
      case "delete":
Dries's avatar
   
Dries committed
197
        locale_delete(check_query(arg(3)));
Dries's avatar
   
Dries committed
198
        print locale_seek();
Dries's avatar
   
Dries committed
199
200
201
202
203
        break;
      case "help":
        print locale_help();
        break;
      case "edit":
Dries's avatar
   
Dries committed
204
        print locale_edit(check_query(arg(3)));
Dries's avatar
   
Dries committed
205
        break;
206
      case "search":
Dries's avatar
   
Dries committed
207
      case t("Search"):
Dries's avatar
   
Dries committed
208
209
        print locale_seek();
        break;
Dries's avatar
   
Dries committed
210
      case "translated":
Dries's avatar
   
Dries committed
211
        $edit["status"] = 1;
Dries's avatar
   
Dries committed
212
        $edit["language"] = arg(3);
Dries's avatar
   
Dries committed
213
        print locale_seek();
Dries's avatar
   
Dries committed
214
        break;
Dries's avatar
   
Dries committed
215
      case "untranslated":
Dries's avatar
   
Dries committed
216
        $edit["status"] = 2;
Dries's avatar
   
Dries committed
217
        $edit["language"] = arg(3);
Dries's avatar
   
Dries committed
218
        print locale_seek();
Kjartan's avatar
Kjartan committed
219
        break;
Dries's avatar
   
Dries committed
220
      case t("Save translations"):
Dries's avatar
   
Dries committed
221
        print locale_save(check_query(arg(3)));
Dries's avatar
   
Dries committed
222
      default:
Kjartan's avatar
Kjartan committed
223
        print locale_seek();
Dries's avatar
   
Dries committed
224
225
226
227
    }
  }
  else {
    print message_access();
Dries's avatar
   
Dries committed
228
229
230
  }
}

Steven Wittens's avatar
Steven Wittens committed
231
function locale_admin_initialize() {
Dries's avatar
   
Dries committed
232
233
234
235
236
  /*
  ** 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255

  $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
256
257
function locale($string) {
  global $locale;
Dries's avatar
   
Dries committed
258
259
  static $locale_t;

Dries's avatar
   
Dries committed
260
  if (!isset($locale_t)) {
Dries's avatar
   
Dries committed
261
262
263
264
    $cache = cache_get("locale:$locale");
    if ($cache) {
      $locale_t = unserialize($cache->data);
    }
Dries's avatar
   
Dries committed
265
  }
Dries's avatar
   
Dries committed
266

Dries's avatar
   
Dries committed
267
  if ($locale_t[$string] != "") {
Dries's avatar
   
Dries committed
268
    $string = $locale_t[$string];
Dries's avatar
   
Dries committed
269
270
271
272
  }
  else {
    $result = db_query("SELECT lid, $locale FROM locales WHERE string = '%s'", $string);
    if (!db_fetch_object($result)) {
273
      db_query("INSERT INTO locales (string, location) VALUES ('%s', '%s')", $string, request_uri());
Dries's avatar
   
Dries committed
274
    }
Dries's avatar
Dries committed
275
  }
Dries's avatar
   
Dries committed
276

Dries's avatar
   
Dries committed
277
278
  return $string;
}
Dries's avatar
   
Dries committed
279

Dries's avatar
Dries committed
280
?>