locale.module 5.5 KB
Newer Older
Dries's avatar
   
Dries committed
1
<?php
Dries's avatar
 
Dries committed
2

Dries's avatar
   
Dries committed
3
function locale_help() {
Dries's avatar
   
Dries committed
4
5
 ?>
  <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
CHANGES    
Dries committed
6
  <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.  A side effect is that translation support adds significant overhead to the dynamic generation of your website.  If you don't need translation support, consider to turning it off from the "conf" section.</P>
Dries's avatar
   
Dries committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

  <H3>Adding a new language</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>
Dries's avatar
   
Dries committed
24
 <?php
Dries's avatar
   
Dries committed
25
26
}

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

Dries's avatar
   
Dries committed
31
32
33
34
35
36
37
38
39
40
41
function locale_delete($id) {
  db_query("DELETE FROM locales WHERE id = '$id'");
}

function locale_save($id, $edit) {
  foreach ($edit as $key=>$value) {
    db_query("UPDATE locales SET $key = '". check_input($value) ."' WHERE id = '$id'");
  }
}

function locale_edit($id) {
Dries's avatar
   
Dries committed
42
43
  global $languages, $REQUEST_URI;

Dries's avatar
   
Dries committed
44
45
  $result = db_query("SELECT * FROM locales WHERE id = '$id'");
  if ($translation = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
46
47
48
49
50
51
    $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_hidden("id", $id);
    $form .= form_submit("Save translations");

    return form($REQUEST_URI, $form);
Dries's avatar
   
Dries committed
52
53
54
55
  }
}

function locale_languages($translation) {
56
57
58
59
60
61
  global $languages, $na;
  if (count($languages)) {
    foreach ($languages as $key=>$value) {
      $output .= ($translation->$key) ? "$key " : "<STRIKE>$key</STRIKE> ";
    }
    return $output;
Dries's avatar
   
Dries committed
62
  }
63
  return $na;
Dries's avatar
   
Dries committed
64
65
}

Dries's avatar
   
Dries committed
66
function locale_overview() {
67
68
  if (variable_get("locale", 0)) {
    $result = db_query("SELECT * FROM locales ORDER BY string");
Dries's avatar
   
Dries committed
69

70
71
72
73
74
75
76
    $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)) {
      $languages = locale_languages($locale);
      $output .= " <TR><TD>". check_output($locale->string) ."<BR><SMALL><I>$locale->location</I></SMALL></TD><TD ALIGN=\"center\">$languages</TD><TD><A HREF=\"admin.php?mod=locale&op=edit&id=$locale->id\">edit</A></TD><TD><A HREF=\"admin.php?mod=locale&op=delete&id=$locale->id\">delete</A></TD></TR>";
    }
    $output .= "</TABLE>\n";
Dries's avatar
   
Dries committed
77

78
    return $output;
Dries's avatar
 
Dries committed
79
  }
Dries's avatar
   
Dries committed
80
81
82
  else {
    return status("locale disabled.");
  }
Dries's avatar
 
Dries committed
83
84
}

Dries's avatar
   
Dries committed
85
86
87
88
89
90
91
function locale_admin() {
  global $id, $edit, $op;

  print "<SMALL><A HREF=\"admin.php?mod=locale\">overview</A> | <A HREF=\"admin.php?mod=locale&op=help\">help</A></SMALL><HR>\n";

  switch ($op) {
    case "delete":
Dries's avatar
   
Dries committed
92
93
      print status(locale_delete(check_input($id)));
      print locale_overview();
Dries's avatar
   
Dries committed
94
95
      break;
    case "help":
Dries's avatar
   
Dries committed
96
      print locale_help();
Dries's avatar
   
Dries committed
97
98
      break;
    case "edit":
Dries's avatar
   
Dries committed
99
      print locale_edit(check_input($id));
Dries's avatar
   
Dries committed
100
101
      break;
    case "Save translations":
Dries's avatar
   
Dries committed
102
      print locale_save(check_input($id), $edit);
Dries's avatar
   
Dries committed
103
104
      // fall through
    default:
Dries's avatar
   
Dries committed
105
      print locale_overview();
Dries's avatar
   
Dries committed
106
107
108
109
110
  }
}

function locale($string) {
  global $locale;
Dries's avatar
   
Dries committed
111
  if (variable_get("locale", 0)) {
Dries's avatar
Dries committed
112
113
114
115
    $result = db_query("SELECT id, $locale FROM locales WHERE STRCMP(string, '". addslashes($string) ."') = 0");
    if ($translation = db_fetch_object($result)) $string = ($translation->$locale) ? check_output($translation->$locale) : $string;
    else db_query("INSERT INTO locales (string, location) VALUES ('". addslashes($string) ."', '". check_input(getenv("REQUEST_URI")) ."')");
  }
Dries's avatar
   
Dries committed
116
117
118
119
  return $string;
}

?>