summaryrefslogtreecommitdiff
path: root/modules/locale.module
blob: 41197e51e7362429ebeb1e08e68b0cb8e5382076 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
// $Id$

function locale_help() {
 ?>
  <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>
  <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>

  <h3>How to translate texts</h3>

  <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>

  <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>
  <pre>
    $languages = array("nl" => "Dutch / Nederlands", "fr" => "French / Francais");
  </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>
    $languages = array("en" => "English", "nl" => "Dutch / Nederlands", "fr" => "French / Francais");
  </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>
    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;
  </pre>
 <?php
}

function locale_system($field){
  $system["description"] = t("Enables the translation of the user interface to languages other than English.");
  return $system[$field];
}

function locale_perm() {
  return array("administer locales");
}

function locale_link($type) {
  global $languages;

  if ($type == "admin" && user_access("administer locales")) {
    $help["general"] = "The locale module handles translations into new languages.  It also enables you to add jargon, slang or other special language as fits the website.  For each language you want to support, a line needs to be added to your configuration file.";

    menu("admin/locale", "localization", NULL, $help["general"], 5);
    menu("admin/locale/search", "search string", "locale_admin", NULL, 8);
    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

    foreach ($languages as $key => $value) {
      menu("admin/locale/$key", "$value", NULL, $help["general"]);
      menu("admin/locale/$key/translated", "translated strings", "locale_admin");
      menu("admin/locale/$key/untranslated", "untranslated strings", "locale_admin");
    }
  }
}

function locale_delete($lid) {
  db_query("DELETE FROM locales WHERE lid = %d", $lid);
  locale_refresh_cache();

  return t("deleted string");
}

function locale_save($lid) {
  $edit = $_POST["edit"];
  foreach ($edit as $key=>$value) {
    db_query("UPDATE locales SET $key = '%s' WHERE lid = %d", $value, $lid);
  }
  locale_refresh_cache();
  // delete form data so it will remember where it came from
  $edit = '';

  return t("saved string");
}

function locale_refresh_cache() {
  global $languages;

  foreach (array_keys($languages) as $locale) {
    $result = db_query("SELECT string, %s FROM locales", $locale);
    while ($data = db_fetch_object($result)) {
      if (empty($data->$locale)) {
        $t[$data->string] = $data->string;
      }
      else {
        $t[$data->string] = $data->$locale;
      }
    }
    cache_set("locale:$locale", serialize($t));
  }
}

function locale_edit($lid) {
  global $languages;

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

    return form($form);
  }
}

function locale_languages($translation) {
  global $languages;

  foreach ($languages as $key => $value) {
    $output .= ($translation->$key) ? "<a href=\"#\" title=\"". $translation->$key ."\">$key</a> " : "<strike>$key</strike> ";
  }

  return $output;
}

function locale_seek() {
  global $id, $languages, $locale_settings;
  $op = $_POST["op"];
  $edit = $_POST["edit"];

  if ($op != 'overview' && !$edit && session_is_registered("locale_settings")) {
    $edit = $locale_settings;
  }
  else {
    $locale_settings = $edit;
    session_register("locale_settings");
  }

  if (is_array($edit)) {

    if ($edit["status"]) {
      switch ($edit["language"]) {
        case "all":
          foreach ($languages as $key=>$value) {
            $tmp[] = $key . (check_query($edit["status"]) == 1 ? " !=" : " =") ." ''";
          }
          $query[] = implode(" AND ", $tmp);
          break;
        case "any":
          foreach ($languages as $key=>$value) {
            $tmp[] = $key . (check_query($edit["status"]) == 1 ? " !=" : " =") ." ''";
          }
          $query[] = "(". implode(" || ", $tmp) .")";
          break;
        default:
          $query[] = check_query($edit["language"]) . (check_query($edit["status"]) == 1 ? " !=" : " =") ." ''";
      }
    }

    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) {
          $string_query[] = check_query($edit["language"]) ." LIKE '%". check_query($edit["string"]) ."%'";
        }
        else {
          foreach ($languages as $key=>$value) {
            $string_query[] = check_query($key) ." LIKE '%". check_query($edit["string"]) ."%'";
          }
        }
      }
      $query[] = "(". implode(" || ", $string_query) .")";
    }

    $result = db_query("SELECT * FROM locales". (count($query) ? " WHERE ". implode(" && ", $query) : "") ." ORDER BY string");

    $header = array(t("string"), (($edit["status"] != 2 && strlen($edit["language"]) == 2) ? t("translated string") : t("languages")), array("data" => t("operations"), "colspan" => "2"));
    while ($locale = db_fetch_object($result)) {
      $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"));
    }
    $output .= table($header, $rows);
  }

  reset($languages);

  $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"));

  $output .= form($form);

  return $output;
}

function locale_admin() {
  $op = $_POST["op"];
  $edit = $_POST["edit"];

  if (user_access("administer locales")) {
    locale_admin_initialize();

    if (empty($op)) {
      $op = arg(2);
    }

    switch ($op) {
      case "delete":
        print status(locale_delete(check_query(arg(3))));
        print locale_seek();
        break;
      case "edit":
        print locale_edit(check_query(arg(3)));
        break;
      case "search":
      case t("Search"):
        print locale_seek();
        break;
      case t("Save translations"):
        print status(locale_save(check_query(arg(3))));
        print locale_seek();
        break;
      default:
        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();
        }
    }
  }
  else {
    print message_access();
  }
}

function locale_admin_initialize() {
  /*
  ** 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.
  */

  $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));
    }
  }
}

function locale($string) {
  global $locale;
  static $locale_t;

  if (!isset($locale_t)) {
    $cache = cache_get("locale:$locale");
    if ($cache == 0) {
      locale_refresh_cache();
      $cache = cache_get("locale:$locale");
    }
    $locale_t = unserialize($cache->data);
  }

  if ($locale_t[$string] != "") {
    $string = $locale_t[$string];
  }
  else {
    $result = db_query("SELECT lid, $locale FROM locales WHERE string = '%s'", $string);
    if (!db_fetch_object($result)) {
      db_query("INSERT INTO locales (string, location) VALUES ('%s', '%s')", $string, request_uri());
      cache_clear_all("locale:$locale");
    }
  }

  return $string;
}

?>