drupal/modules/locale.module

308 lines
14 KiB
Plaintext
Raw Normal View History

<?php
// $Id$
function locale_help() {
$output .= "<p>Normally programs are written and documented in English, and use English to interact with users. This is true for a great deal of web sites. 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 web site showing a lot less of English, and far more of their own language.</p>";
$output .= "<p>Therefore Drupal provides a framework to setup a multi-lingual web site, 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>";
$output .= "<h3>How to translate texts</h3><p>The actual translation starts at the ". l("overview","admin/locale") ." of the locale section of the administration pages. In the \"localization\" section 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>";
$output .= "<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>";
$output .= "<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>";
$output .= "<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>";
$output .= "<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>";
$output .= "<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>";
$output .= "<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>";
$output .= "<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>";
$output .= "<pre>
\$languages = array(\"nl\" => \"Dutch / Nederlands\", \"fr\" => \"French / Francais\");
</pre>";
$output .= "<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>";
$output .= "<pre>
\$languages = array(\"en\" => \"English\", \"nl\" => \"Dutch / Nederlands\", \"fr\" => \"French / Francais\");
</pre>";
$output .= "<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>";
$output .= "<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>";
return t($output);
}
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) {
2002-12-24 15:40:32 +00:00
global $languages;
if ($type == "admin" && user_access("administer locales")) {
$help["general"] = 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.");
$help["search"] = t("Search the localization database. ('*' can be used as a wildcard)");
$help["translated"] = t("Start by searching the translated strings.");
$help["untranslated"] = t("Start by searching the untranslated strings.");
menu("admin/locale", "localization", NULL, $help["general"], 5);
menu("admin/locale/search", "search string", "locale_admin", $help["search"], 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
2002-12-24 15:40:32 +00:00
foreach ($languages as $key => $value) {
menu("admin/locale/$key", "$value", NULL, $help["general"]);
menu("admin/locale/$key/translated", "translated strings", "locale_admin", $help["translated"]);
menu("admin/locale/$key/untranslated", "untranslated strings", "locale_admin", $help["untranslated"]);
2002-12-24 15:40:32 +00:00
}
}
}
function locale_delete($lid) {
db_query("DELETE FROM {locales} WHERE lid = %d", $lid);
2002-01-05 17:07:48 +00:00
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);
2002-01-05 17:07:48 +00:00
}
locale_refresh_cache();
// delete form data so it will remember where it came from
$edit = '';
return t("saved string");
2002-01-05 17:07:48 +00:00
}
function locale_refresh_cache() {
global $languages;
foreach (array_keys($languages) as $locale) {
$result = db_query("SELECT string, %s FROM {locales} ", $locale);
2002-01-05 17:07:48 +00:00
while ($data = db_fetch_object($result)) {
if (empty($data->$locale)) {
$t[$data->string] = $data->string;
}
else {
$t[$data->string] = $data->$locale;
}
2002-01-05 17:07:48 +00:00
}
cache_set("locale:$locale", serialize($t));
}
}
function locale_edit($lid) {
global $languages;
2002-01-05 17:07:48 +00:00
$result = db_query("SELECT * FROM {locales} WHERE lid = '$lid'");
if ($translation = db_fetch_object($result)) {
$form .= form_item(t("Original text"), wordwrap(drupal_specialchars($translation->string, 0)));
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"));
Welp. Large commit ahead. CHANGES: - Added "read" and "write" permissions into drupal but removed it again because - when finished after 3 hours of work - it was considered nothing but added complexity that didn't buy us anything. :I (I'll explain this in detail on the mailing list, I guess.) - Added a very simple help.module to group all available documentation on a single page. - Fixed bug in node_control(), book.module: UnConeD forgot to global $user when updating the combobox code. - Removed static wishlist.module: in future, the wishlist can be maintained as a page in our collaborative book. - Revised most of settings.module: tidied up the code and the descriptions to accompany the settings and introduced a new "default maximum number of nodes to display on the main page" variable. - Revised most of comment.module: the administration interface looks better now, integrated node permissions, and -finally- made it possible to delete comments. - Polished on: + account.module + structure.module + locale.module + module.module + forum.module - Form-ified: + account.php + account.module + setting.module + cvs.module + submit.php + comment.module + forum.module + book.module + page.module + locale.module - Updated CHANGELOG INFO: - Designed a "generic tracker system with optional backends" on paper. The idea is to allow registered users to hot-list certain topics, individual nodes or threads (comments) and to "plug-in" output backends like - for instance - an e-mail digest. The design requires "intelligent blocks" though. TODO: - I want to tidy up the headline.module and backend.class as well as merge in headlineRSS10.module. Julian spent quite some time working on headline.module but I'm not sure what he changed and whether he'd contribute it back?
2001-04-30 17:13:08 +00:00
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() {
- Bugfix: renamed the SQL field 'types' to 'nodes' because 'types' is a reserved keyword in MySQL 4. This fixes critical bug #1618. Patch by Marco. ==> This fix requires to run update.php! - Bugfix: made sessions work without warnings when register_globals is turned off. The solution is to use $_SESSION instead of session_register(). This fixes critical bug #1797. Patch by Marco. - Bugfix: sometimes error messages where being discarded when previewing a node. Patch by Craig Courtney. - Bugfix: fixed charset problems. This fixes critical bug #1549. Patch '0023.charset.patch' by Al. - Code improvements: removed some dead code from the comment module. Patch by Marco. - Documentation improvements: polished the node module help texts and form descriptions. Patch '0019.node.module.help.patch' by Al. - CSS improvements all over the map! Patch '0021.more.css.patch' by Al. - GUI improvements: improved the position of Druplicon in the admin menu. Patch '0020.admin.logo.patch' by Al. - GUI improvements: new logos for theme Marvin and theme UnConeD. Logos by Kristjan Jansen. - GUI improvements: small changes to the output emitted by the profile module. Suggestions by Steven Wittens. - GUI improvements: small fixes to Xtemplate. Patch '0022.xtemplate.css.patch' by Al. TODO: - Some modules such as the buddy list module and the annotation module in the contributions repository are also using session_register(). They should be updated. We should setup a task on Drupal. - There is code emitting '<div align="right">' which doesn't validate. - Does our XML feeds validate with the charset changes? - The forum module's SQL doesn't work properly on PostgreSQL.
2003-06-04 18:24:39 +00:00
global $id, $languages;
$op = $_POST["op"];
$edit =& $_POST["edit"];
- Bugfix: renamed the SQL field 'types' to 'nodes' because 'types' is a reserved keyword in MySQL 4. This fixes critical bug #1618. Patch by Marco. ==> This fix requires to run update.php! - Bugfix: made sessions work without warnings when register_globals is turned off. The solution is to use $_SESSION instead of session_register(). This fixes critical bug #1797. Patch by Marco. - Bugfix: sometimes error messages where being discarded when previewing a node. Patch by Craig Courtney. - Bugfix: fixed charset problems. This fixes critical bug #1549. Patch '0023.charset.patch' by Al. - Code improvements: removed some dead code from the comment module. Patch by Marco. - Documentation improvements: polished the node module help texts and form descriptions. Patch '0019.node.module.help.patch' by Al. - CSS improvements all over the map! Patch '0021.more.css.patch' by Al. - GUI improvements: improved the position of Druplicon in the admin menu. Patch '0020.admin.logo.patch' by Al. - GUI improvements: new logos for theme Marvin and theme UnConeD. Logos by Kristjan Jansen. - GUI improvements: small changes to the output emitted by the profile module. Suggestions by Steven Wittens. - GUI improvements: small fixes to Xtemplate. Patch '0022.xtemplate.css.patch' by Al. TODO: - Some modules such as the buddy list module and the annotation module in the contributions repository are also using session_register(). They should be updated. We should setup a task on Drupal. - There is code emitting '<div align="right">' which doesn't validate. - Does our XML feeds validate with the charset changes? - The forum module's SQL doesn't work properly on PostgreSQL.
2003-06-04 18:24:39 +00:00
if ($op != 'overview' && !$edit && isset($_SESSION["locale_settings"])) {
$edit = $_SESSION["locale_settings"];
}
else {
- Bugfix: renamed the SQL field 'types' to 'nodes' because 'types' is a reserved keyword in MySQL 4. This fixes critical bug #1618. Patch by Marco. ==> This fix requires to run update.php! - Bugfix: made sessions work without warnings when register_globals is turned off. The solution is to use $_SESSION instead of session_register(). This fixes critical bug #1797. Patch by Marco. - Bugfix: sometimes error messages where being discarded when previewing a node. Patch by Craig Courtney. - Bugfix: fixed charset problems. This fixes critical bug #1549. Patch '0023.charset.patch' by Al. - Code improvements: removed some dead code from the comment module. Patch by Marco. - Documentation improvements: polished the node module help texts and form descriptions. Patch '0019.node.module.help.patch' by Al. - CSS improvements all over the map! Patch '0021.more.css.patch' by Al. - GUI improvements: improved the position of Druplicon in the admin menu. Patch '0020.admin.logo.patch' by Al. - GUI improvements: new logos for theme Marvin and theme UnConeD. Logos by Kristjan Jansen. - GUI improvements: small changes to the output emitted by the profile module. Suggestions by Steven Wittens. - GUI improvements: small fixes to Xtemplate. Patch '0022.xtemplate.css.patch' by Al. TODO: - Some modules such as the buddy list module and the annotation module in the contributions repository are also using session_register(). They should be updated. We should setup a task on Drupal. - There is code emitting '<div align="right">' which doesn't validate. - Does our XML feeds validate with the charset changes? - The forum module's SQL doesn't work properly on PostgreSQL.
2003-06-04 18:24:39 +00:00
$_SESSION["locale_settings"] = $edit;
}
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["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 = pager_query("SELECT * FROM {locales} ". (count($query) ? " WHERE ". implode(" && ", $query) : "") ." ORDER BY string", 50);
$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)) {
2003-01-06 19:51:01 +00:00
$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"));
}
if ($pager = pager_display(NULL, 50, 0, "admin")) {
$rows[] = array(array("data" => "$pager", "colspan" => "5"));
}
$output .= table($header, $rows);
}
return $output;
}
function locale_seek_form() {
global $languages;
$edit =& $_POST["edit"];
$form .= form_textfield(t("Strings to search for"), "string", $edit["string"], 30, 30, t("Leave blank to show all strings."));
$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_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();
2003-01-06 19:51:01 +00:00
if (empty($op)) {
$op = arg(2);
}
switch ($op) {
case "delete":
print status(locale_delete(check_query(arg(3))));
print locale_seek();
break;
case "edit":
2003-01-06 19:51:01 +00:00
print locale_edit(check_query(arg(3)));
break;
case "search":
print locale_seek_form();
break;
case t("Search"):
print locale_seek();
print locale_seek_form();
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);
}
else {
$edit["status"] = 2;
$edit["language"] = arg(2);
}
print locale_seek();
print locale_seek_form();
}
}
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;
2002-01-05 17:07:48 +00:00
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);
}
2002-01-05 17:07:48 +00:00
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");
2002-01-05 17:07:48 +00:00
}
2001-02-17 16:26:48 +00:00
}
2002-01-05 17:07:48 +00:00
return $string;
}
2003-01-21 22:35:54 +00:00
?>