locale.module 12.6 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
 ?>
  <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
10
11
12
13
14
15
16
17
18
19
  <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>
Dries's avatar
   
Dries committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

  <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>
Dries's avatar
   
Dries committed
35
 <?php
Dries's avatar
   
Dries committed
36
37
}

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

function locale_link($type) {
  if ($type == "admin" && user_access("administer locales")) {
    $links[] = "<a href=\"admin.php?mod=locale\">locales</a>";
  }

  return $links ? $links : array();
Dries's avatar
   
Dries committed
48
49
}

Dries's avatar
CHANGES    
Dries committed
50
function locale_conf_options() {
Dries's avatar
   
Dries committed
51
  return form_select("Locale support", "locale", variable_get("locale", 0), array("Disabled", "Enabled"), "Disable locale support if your site does not require translation or internationalization support.");
Dries's avatar
CHANGES    
Dries committed
52
53
}

Dries's avatar
   
Dries committed
54
55
function locale_delete($lid) {
  db_query("DELETE FROM locales WHERE lid = '$lid'");
Dries's avatar
   
Dries committed
56
  locale_refresh_cache();
Dries's avatar
   
Dries committed
57
58
}

Dries's avatar
   
Dries committed
59
function locale_save($lid, $edit) {
Dries's avatar
   
Dries committed
60
  foreach ($edit as $key=>$value) {
Dries's avatar
   
Dries committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    db_query("UPDATE locales SET $key = '". check_query($value) ."' WHERE lid = '$lid'");
  }
  locale_refresh_cache();
}

function locale_refresh_cache() {
  global $languages;

  foreach (array_keys($languages) as $locale) {
    $result = db_query("SELECT string, ". check_query($locale) ." FROM locales");
    while ($data = db_fetch_object($result)) {
      $t[$data->string] = $data->$locale;
    }
    cache_set("locale:$locale", serialize($t));
Dries's avatar
   
Dries committed
75
76
77
  }
}

Dries's avatar
   
Dries committed
78
function locale_edit($lid) {
Dries's avatar
   
Dries committed
79
  global $languages;
Dries's avatar
   
Dries committed
80

Dries's avatar
   
Dries committed
81
  $result = db_query("SELECT * FROM locales WHERE lid = '$lid'");
Dries's avatar
   
Dries committed
82
  if ($translation = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
83
84
85
86
    $form .= form_item(t("Original text"), "<PRE>". wordwrap(check_output($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("Save translations");

Dries's avatar
   
Dries committed
87
    return form($form);
Dries's avatar
   
Dries committed
88
89
90
91
  }
}

function locale_languages($translation) {
Dries's avatar
   
Dries committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  global $languages;

  foreach ($languages as $key=>$value) {
    $output .= ($translation->$key) ? "$key " : "<STRIKE>$key</STRIKE> ";
  }

  return $output;
}

function locale_links($translation) {
  global $languages;

  foreach ($languages as $key=>$value) {
    if ($translation) {
Dries's avatar
   
Dries committed
106
      $output .= "<a href=\"admin.php?mod=locale&op=translated&language=$key\">translated '$key' strings</a> | ";
Dries's avatar
   
Dries committed
107
108
    }
    else {
Dries's avatar
   
Dries committed
109
      $output .= "<a href=\"admin.php?mod=locale&op=untranslated&language=$key\">untranslated '$key' strings</a> | ";
110
    }
Dries's avatar
   
Dries committed
111
  }
Dries's avatar
   
Dries committed
112
113

  return $output;
Dries's avatar
   
Dries committed
114
115
}

Dries's avatar
   
Dries committed
116
function locale_overview() {
Dries's avatar
   
Dries committed
117
  $result = db_query("SELECT * FROM locales ORDER BY string");
Dries's avatar
   
Dries committed
118

Dries's avatar
   
Dries committed
119
  $output .= "<TABLE BORDER=\"1\" CELLPADDING=\"2\" CELLSPACING=\"2\">\n";
Kjartan's avatar
Kjartan committed
120
  $output .= " <TR><TH>string</TH><TH>languages</TH><TH COLSPAN=\"2\">operations</TH></TR>\n";
Dries's avatar
   
Dries committed
121
  while ($locale = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
122
    $output .= " <TR><TD>". check_output($locale->string) ."<BR><SMALL><I>". check_output($locale->location) ."</I></SMALL></TD><TD ALIGN=\"center\">". check_output(locale_languages($locale)) ."</TD><TD NOWRAP><A HREF=\"admin.php?mod=locale&op=edit&id=$locale->lid\">edit locale</A></TD><TD NOWRAP><A HREF=\"admin.php?mod=locale&op=delete&id=$locale->lid\">delete locale</A></TD></TR>";
Dries's avatar
   
Dries committed
123
124
125
126
127
128
  }
  $output .= "</TABLE>\n";

  return $output;
}

Dries's avatar
   
Dries committed
129
130
function locale_translated($language) {
  $result = db_query("SELECT * FROM locales WHERE $language != '' ORDER BY string");
Dries's avatar
   
Dries committed
131

Dries's avatar
   
Dries committed
132
  $output .= "<TABLE BORDER=\"1\" CELLPADDING=\"2\" CELLSPACING=\"2\">\n";
Kjartan's avatar
Kjartan committed
133
  $output .= " <TR><TH>original string</TH><TH>translated string</TH><TH COLSPAN=\"2\">operations</TH></TR>\n";
Dries's avatar
   
Dries committed
134
  while ($locale = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
135
    $output .= " <TR><TD>". check_output($locale->string) ."</TD><TD>". check_output($locale->$language) ."</TD><TD NOWRAP><A HREF=\"admin.php?mod=locale&op=edit&id=$locale->lid\"> edit locale</A></TD><TD NOWRAP><A HREF=\"admin.php?mod=locale&op=delete&id=$locale->lid\">delete locale</A></TD></TR>";
Dries's avatar
 
Dries committed
136
  }
Dries's avatar
   
Dries committed
137
138
139
140
141
  $output .= "</TABLE>\n";

  return $output;
}

Dries's avatar
   
Dries committed
142
143
function locale_untranslated($language) {
  $result = db_query("SELECT * FROM locales WHERE $language = '' ORDER BY string");
Dries's avatar
   
Dries committed
144
145

  $output .= "<TABLE BORDER=\"1\" CELLPADDING=\"2\" CELLSPACING=\"2\">\n";
Kjartan's avatar
Kjartan committed
146
  $output .= " <TR><TH>string</TH><TH COLSPAN=\"2\">operations</TH></TR>\n";
Dries's avatar
   
Dries committed
147
  while ($locale = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
148
    $output .= " <TR><TD>". check_output($locale->string) ."<BR><SMALL><I>$locale->location</I></SMALL></TD><TD NOWRAP><A HREF=\"admin.php?mod=locale&op=edit&id=$locale->lid\"> edit locale</A></TD><TD NOWRAP><A HREF=\"admin.php?mod=locale&op=delete&id=$locale->lid\">delete locale</A></TD></TR>";
Dries's avatar
   
Dries committed
149
  }
Dries's avatar
   
Dries committed
150
151
152
  $output .= "</TABLE>\n";

  return $output;
Dries's avatar
 
Dries committed
153
154
}

Kjartan's avatar
Kjartan committed
155
function locale_seek() {
Dries's avatar
   
Dries committed
156
  global $id, $edit, $languages, $op, $locale_settings;
Kjartan's avatar
Kjartan committed
157

Dries's avatar
   
Dries committed
158
  if (session_is_registered("locale_settings")) {
Kjartan's avatar
Kjartan committed
159
160
161
162
163
164
    $edit = $locale_settings;
  }

  if ($op != "search" && is_array($edit)) {
    $locale_settings = $edit;
    session_register("locale_settings");
Kjartan's avatar
Kjartan committed
165
166
167
168
    if ($edit[status]) {
      switch ($edit[language]) {
        case "all":
          foreach ($languages as $key=>$value) {
Dries's avatar
   
Dries committed
169
            $tmp[] = $key . (check_query($edit[status]) == 1 ? " !=" : " =") ." ''";
Kjartan's avatar
Kjartan committed
170
171
172
173
174
          }
          $query[] = implode(" && ", $tmp);
          break;
        case "any":
          foreach ($languages as $key=>$value) {
Dries's avatar
   
Dries committed
175
            $tmp[] = $key . (check_query($edit[status]) == 1 ? " !=" : " =") ." ''";
Kjartan's avatar
Kjartan committed
176
177
178
179
          }
          $query[] = "(". implode(" || ", $tmp) .")";
          break;
        default:
Dries's avatar
   
Dries committed
180
          $query[] = check_query($edit[language]) . (check_query($edit[status]) == 1 ? " !=" : " =") ." ''";
Kjartan's avatar
Kjartan committed
181
182
      }
    }
Dries's avatar
   
Dries committed
183
184
    if ($edit[module]) $query[] = "location LIKE '%mod=". (check_query($edit[module]) != "all" ? check_query($edit[module]) : "") ."%'";
    if ($edit[string]) $query[] = "string RLIKE '". check_query($edit[string]) ."'";
Dries's avatar
   
Dries committed
185

Kjartan's avatar
Kjartan committed
186
    $result = db_query("SELECT * FROM locales". (count($query) ? " WHERE ". implode(" && ", $query) : ""));
Kjartan's avatar
Kjartan committed
187
188
189
190

    $output .= "<TABLE BORDER=\"1\" CELLPADDING=\"2\" CELLSPACING=\"2\">\n";
    $output .= " <TR><TH>string</TH><TH>languages</TH><TH COLSPAN=\"2\">operations</TH></TR>\n";
    while ($locale = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
191
      $output .= " <TR><TD>". check_output($locale->string) ."<BR><SMALL><I>". check_output($locale->location) ."</I></SMALL></TD><TD ALIGN=\"center\">". check_output(locale_languages($locale)) ."</TD><TD NOWRAP><A HREF=\"admin.php?mod=locale&op=edit&id=$locale->lid\">edit locale</A></TD><TD NOWRAP><A HREF=\"admin.php?mod=locale&op=delete&id=$locale->lid\">delete locale</A></TD></TR>";
Kjartan's avatar
Kjartan committed
192
193
194
    }
    $output .= "</TABLE>\n";
  }
Kjartan's avatar
Kjartan committed
195
196
197
198
199

  $form .= form_select("Language", "language", $edit[language], array_merge(array("all" => "All", "any" => "Any"), $languages));
  $form .= form_select("Status", "status", $edit[status], array("All", "Translated", "Untranslated"));
  $form .= form_select("Module", "module", $edit[module], array_merge(array("0" => "All modules + pages", "all" => "All modules"), module_list()));
  $form .= form_textfield("String", "string", $edit[string], 30, 30, "Leave blank to show all strings. This is treated as a regular expression.");
Kjartan's avatar
Kjartan committed
200
  $form .= form_submit("Search");
Dries's avatar
   
Dries committed
201

Dries's avatar
   
Dries committed
202
  $output .= form($form);
Kjartan's avatar
Kjartan committed
203
204
205
206

  return $output;
}

Dries's avatar
   
Dries committed
207
function locale_admin() {
Dries's avatar
   
Dries committed
208
  global $id, $edit, $op, $language;
Dries's avatar
   
Dries committed
209

Dries's avatar
   
Dries committed
210
  if (!variable_get("locale", 0)) {
Dries's avatar
   
Dries committed
211
    print status("locale disabled.");
Dries's avatar
   
Dries committed
212
213
  }
  else if (user_access("administer locales")) {
Kjartan's avatar
Kjartan committed
214
    print "<SMALL>". locale_links(1) . locale_links(0) ."<A HREF=\"admin.php?mod=locale&op=search\">search</A> | <A HREF=\"admin.php?mod=locale&op=overview\">overview</A> | <A HREF=\"admin.php?mod=locale&op=help\">help</A></SMALL><HR>\n";
Dries's avatar
   
Dries committed
215
216
217

    switch ($op) {
      case "delete":
Dries's avatar
   
Dries committed
218
        print status(locale_delete(check_query($id)));
Dries's avatar
   
Dries committed
219
220
221
222
223
224
        print locale_overview();
        break;
      case "help":
        print locale_help();
        break;
      case "edit":
Dries's avatar
   
Dries committed
225
        print locale_edit(check_query($id));
Dries's avatar
   
Dries committed
226
        break;
Dries's avatar
   
Dries committed
227
      case "translated":
Dries's avatar
   
Dries committed
228
229
        print locale_translated($language);
        break;
Dries's avatar
   
Dries committed
230
      case "untranslated":
Dries's avatar
   
Dries committed
231
        print locale_untranslated($language);
Dries's avatar
   
Dries committed
232
        break;
Kjartan's avatar
Kjartan committed
233
234
      case "overview":
        print locale_overview();
Kjartan's avatar
Kjartan committed
235
236
        break;
      case "Save translations":
Dries's avatar
   
Dries committed
237
        print locale_save(check_query($id), $edit);
Dries's avatar
   
Dries committed
238
      default:
Kjartan's avatar
Kjartan committed
239
        print locale_seek();
Dries's avatar
   
Dries committed
240
241
242
243
    }
  }
  else {
    print message_access();
Dries's avatar
   
Dries committed
244
245
246
247
248
  }
}

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

Dries's avatar
   
Dries committed
251
  if (variable_get("locale", 0)) {
Dries's avatar
   
Dries committed
252
253
254
255
256
257
258
259
260
261
262
263
264
    if (!isset($locale_t)) {
      $locale_t = unserialize(cache_get("locale:$locale"));
    }

    if ($locale_t[$string] != "") {
      $string = check_output($locale_t[$string]);
    }
    else {
      $result = db_query("SELECT lid, $locale FROM locales WHERE STRCMP(string, '". addslashes($string) ."') = 0");
      if (!db_fetch_object($result)) {
        db_query("INSERT INTO locales (string, location) VALUES ('". check_query($string) ."', '". check_query(getenv("REQUEST_URI")) ."')");
      }
    }
Dries's avatar
Dries committed
265
  }
Dries's avatar
   
Dries committed
266

Dries's avatar
   
Dries committed
267
268
  return $string;
}
Dries's avatar
   
Dries committed
269

Dries's avatar
   
Dries committed
270
?>