locale.module 14.1 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($section = "admin/help#locale") {
Dries's avatar
   
Dries committed
5

Dries's avatar
   
Dries committed
6
  switch ($section) {
Dries's avatar
   
Dries committed
7
    case 'admin/help#locale':
Dries's avatar
   
Dries committed
8
      return t("
Dries's avatar
   
Dries committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
      <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>
      <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>
      <h3>How to translate texts</h3>
      <p>The actual translation starts at the <a href=\"%overview\">overview</a> 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>
      <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 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>
      <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 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>
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>", array("%overview" => url("admin/locale")));
Dries's avatar
   
Dries committed
35
      break;
Dries's avatar
   
Dries committed
36
    case 'admin/system/modules#description':
Dries's avatar
   
Dries committed
37
      return t('Enables the translation of the user interface to languages other than English.');
Dries's avatar
   
Dries committed
38
    case 'admin/locale':
Dries's avatar
   
Dries committed
39
      return 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
40
    case 'admin/locale/search':
Dries's avatar
   
Dries committed
41
      return t("Search the localization database. ('*' can be used as a wildcard)");
Dries's avatar
   
Dries committed
42
43
  }

Dries's avatar
   
Dries committed
44
45
}

Dries's avatar
   
Dries committed
46
function locale_help_page() {
Dries's avatar
   
Dries committed
47
  print theme('page', locale_help());
Dries's avatar
   
Dries committed
48
49
}

Dries's avatar
   
Dries committed
50
function locale_perm() {
Dries's avatar
   
Dries committed
51
  return array('administer locales');
Dries's avatar
   
Dries committed
52
53
54
}

function locale_link($type) {
Dries's avatar
   
Dries committed
55
56
  global $languages;

Dries's avatar
   
Dries committed
57
  if ($type == "system") {
Dries's avatar
   
Dries committed
58
    if (user_access('administer locales')) {
Dries's avatar
   
Dries committed
59

Dries's avatar
   
Dries committed
60
      menu("admin/locale", t("localization"), "locale_admin", 5);
Dries's avatar
   
Dries committed
61
      menu("admin/locale/search", t("search string"), "locale_admin", 8);
Dries's avatar
   
Dries committed
62
      menu("admin/locale/help", t("help"), "locale_help_page", 9);
63
64
      menu("admin/locale/edit", t("edit string"), "locale_admin", 0, MENU_HIDE, MENU_LOCKED);
      menu("admin/locale/delete", t("delete string"), "locale_admin", 0, MENU_HIDE, MENU_LOCKED);
Dries's avatar
   
Dries committed
65
66

      foreach ($languages as $key => $value) {
Dries's avatar
   
Dries committed
67
        menu("admin/locale/$key", "$value", "locale_admin");
Dries's avatar
   
Dries committed
68
69
        menu("admin/locale/$key/translated", t("translated strings"), "locale_admin");
        menu("admin/locale/$key/untranslated", t("untranslated strings"), "locale_admin");
Dries's avatar
   
Dries committed
70
      }
Dries's avatar
   
Dries committed
71
72
    }
  }
Dries's avatar
   
Dries committed
73
74
}

Dries's avatar
   
Dries committed
75
76
function locale_user($type, &$edit, &$user) {
  global $languages;
Dries's avatar
   
Dries committed
77
  if ($type == 'edit' && count($languages) > 1) {
78
    return array(t('Locale settings') => form_radios(t("Language"), 'language', $user->language, $languages, t("Selecting a different language will change the language of the site.")));
Dries's avatar
   
Dries committed
79
80
81
  }
}

Dries's avatar
   
Dries committed
82
function locale_delete($lid) {
Dries's avatar
   
Dries committed
83
  db_query("DELETE FROM {locales} WHERE lid = %d", $lid);
Dries's avatar
   
Dries committed
84
  locale_refresh_cache();
Dries's avatar
   
Dries committed
85

Dries's avatar
   
Dries committed
86
  drupal_set_message(t("deleted string"));
Dries's avatar
   
Dries committed
87
88
}

Dries's avatar
   
Dries committed
89
function locale_save($lid) {
Dries's avatar
   
Dries committed
90
  $edit =& $_POST["edit"];
Dries's avatar
   
Dries committed
91
  foreach ($edit as $key => $value) {
Dries's avatar
   
Dries committed
92
    db_query("UPDATE {locales} SET $key = '%s' WHERE lid = %d", $value, $lid);
Dries's avatar
   
Dries committed
93
94
  }
  locale_refresh_cache();
Dries's avatar
   
Dries committed
95
96
  // delete form data so it will remember where it came from
  $edit = '';
Dries's avatar
   
Dries committed
97

Dries's avatar
   
Dries committed
98
  drupal_set_message(t("saved string"));
Dries's avatar
   
Dries committed
99
100
101
102
103
104
}

function locale_refresh_cache() {
  global $languages;

  foreach (array_keys($languages) as $locale) {
Dries's avatar
   
Dries committed
105
106
107
108
109
    /*
    ** We only load short strings into the cache to improve both performance
    ** and memory usages.
    */
    $result = db_query("SELECT string, %s FROM {locales} WHERE LENGTH(string) < 75", $locale);
Dries's avatar
   
Dries committed
110
    while ($data = db_fetch_object($result)) {
111
112
113
114
115
116
      if (empty($data->$locale)) {
        $t[$data->string] = $data->string;
      }
      else {
        $t[$data->string] = $data->$locale;
      }
Dries's avatar
   
Dries committed
117
118
    }
    cache_set("locale:$locale", serialize($t));
Dries's avatar
   
Dries committed
119
120
121
  }
}

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

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

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

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

Dries's avatar
   
Dries committed
136
    return form($form);
Dries's avatar
   
Dries committed
137
138
139
140
  }
}

function locale_languages($translation) {
Dries's avatar
   
Dries committed
141
142
  global $languages;

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

  return $output;
}

150
function locale_seek_query() {
Dries's avatar
   
Dries committed
151
  $fields = array("string", 'language', 'status');
152
153
154
155
156
157
  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
158
  }
Dries's avatar
   
Dries committed
159
  else {
160
161
162
163
164
    foreach ($_REQUEST as $key => $value) {
      if (!empty($value) && in_array($key, $fields)) {
        $query->$key = strpos(",", $value) ? explode(",", $value) : $value;
      }
    }
Dries's avatar
   
Dries committed
165
  }
166
167
  return $query;
}
Dries's avatar
   
Dries committed
168

169
170
171
172
function locale_seek() {
  global $id, $languages;
  $op = $_POST["op"];
  $query = locale_seek_query();
Dries's avatar
   
Dries committed
173

174
175
176
177
  if ($query) {

    if ($query->status) {
      switch ($query->language) {
Kjartan's avatar
Kjartan committed
178
179
        case "all":
          foreach ($languages as $key=>$value) {
180
            $tmp[] = $key . (check_query($query->status) == 1 ? " !=" : " =") ." ''";
Kjartan's avatar
Kjartan committed
181
          }
182
          $sql[] = implode(" AND ", $tmp);
Kjartan's avatar
Kjartan committed
183
184
185
          break;
        case "any":
          foreach ($languages as $key=>$value) {
186
            $tmp[] = $key . (check_query($query->status) == 1 ? " !=" : " =") ." ''";
Kjartan's avatar
Kjartan committed
187
          }
Dries's avatar
   
Dries committed
188
          $sql[] = "(". implode(" OR ", $tmp) .")";
Kjartan's avatar
Kjartan committed
189
190
          break;
        default:
191
          $sql[] = check_query($query->language) . (check_query($query->status) == 1 ? " !=" : " =") ." ''";
Kjartan's avatar
Kjartan committed
192
193
      }
    }
Dries's avatar
   
Dries committed
194

195
196
197
198
199
    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
200
201
202
        }
        else {
          foreach ($languages as $key=>$value) {
203
            $string_query[] = check_query($key) ." LIKE '%". check_query($query->string) ."%'";
Dries's avatar
   
Dries committed
204
205
206
          }
        }
      }
Dries's avatar
   
Dries committed
207
      $sql[] = "(". implode(" OR ", $string_query) .")";
Dries's avatar
   
Dries committed
208
209
    }

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

212
    $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
213
    while ($locale = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
214
      $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
215
    }
216

217
218
219
220
221
222
223
    $request = array();
    if (count($query)) {
      foreach ($query as $key => $value) {
        $request[$key] = (is_array($value)) ? implode(",", $value) : $value;
      }
    }

Dries's avatar
Dries committed
224
    if ($pager = theme("pager", NULL, 50, 0, $request)) {
225
      $rows[] = array(array("data" => "$pager", "colspan" => "5"));
Dries's avatar
   
Dries committed
226
227
    }

Dries's avatar
   
Dries committed
228
    $output .= theme("table", $header, $rows);
229

Kjartan's avatar
Kjartan committed
230
  }
Kjartan's avatar
Kjartan committed
231

Dries's avatar
   
Dries committed
232
233
  return $output;
}
Dries's avatar
   
Dries committed
234

Dries's avatar
   
Dries committed
235
236
237
function locale_seek_form() {
  global $languages;
  $edit =& $_POST["edit"];
Dries's avatar
   
Dries committed
238
  $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
239
  if (count($languages) > 1) {
Dries's avatar
   
Dries committed
240
    $form .= form_radios(t("Language"), 'language', ($edit['language'] ? $edit['language'] : "all"), array_merge(array("any" => t("Any language"), "all" => t("All languages")), $languages), t("In which language must the string be translated/untranslated (see status)?"));
Dries's avatar
   
Dries committed
241
242
243
  }
  else {
    foreach ($languages as $key => $value) {
Dries's avatar
   
Dries committed
244
      $form .= form_hidden('language', $key);
Dries's avatar
   
Dries committed
245
246
    }
  }
Dries's avatar
   
Dries committed
247
248
  $form .= form_radios(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
249

Dries's avatar
   
Dries committed
250
  $output .= form($form);
Kjartan's avatar
Kjartan committed
251
252
253
254

  return $output;
}

Dries's avatar
   
Dries committed
255
function locale_admin() {
Dries's avatar
   
Dries committed
256
  $op = $_POST["op"];
Dries's avatar
   
Dries committed
257
  $edit =& $_POST["edit"];
Dries's avatar
   
Dries committed
258

Dries's avatar
   
Dries committed
259
260
261
  if (empty($op)) {
    $op = arg(2);
  }
Dries's avatar
   
Dries committed
262

Dries's avatar
   
Dries committed
263
264
265
266
267
268
269
270
271
272
273
  switch ($op) {
    case 'delete':
      $output .= locale_delete(check_query(arg(3)));
      $output .= locale_seek();
      break;
    case 'edit':
      $output .= locale_edit(check_query(arg(3)));
      $output .= locale_seek();
      break;
    case 'search':
      if (locale_seek_query()) {
Dries's avatar
   
Dries committed
274
        $output = locale_seek();
Dries's avatar
   
Dries committed
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
      }
      $output .= locale_seek_form();
      break;
    case t('Search'):
      $output = locale_seek();
      $output .= locale_seek_form();
      break;
    case t('Save translations'):
      $output .= locale_save(check_query(arg(3)));
      $output .= locale_seek_form();
      break;
    default:
      if (arg(3) == 'translated') {
        $edit['status'] = 1;
        $edit['language'] = arg(2);
      }
      else {
        $edit['status'] = 2;
        $edit['language'] = arg(2);
      }
      $output = locale_seek();
      $output .= locale_seek_form();
Dries's avatar
   
Dries committed
297
  }
Dries's avatar
   
Dries committed
298
  print theme('page', $output);
Dries's avatar
   
Dries committed
299
300
301
302
}

function locale($string) {
  global $locale;
Dries's avatar
   
Dries committed
303
304
  static $locale_t;

Dries's avatar
   
Dries committed
305
  if (!isset($locale_t)) {
Dries's avatar
   
Dries committed
306
    $cache = cache_get("locale:$locale");
Dries's avatar
   
Dries committed
307

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

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

Dries's avatar
   
Dries committed
332
333
  return $string;
}
Dries's avatar
   
Dries committed
334

Dries's avatar
Dries committed
335
?>