* integrate Marko Samastur's translate.js with gettext and php
 * translate the first JavaScript strings
 * add scripts/i18n.php to generate the list of i18n dictionary
This commit is contained in:
Jan Dittberner 2007-12-01 23:15:26 +00:00
parent 3b234e141d
commit 4a15c5b5b6
15 changed files with 409 additions and 12 deletions

View file

@ -138,7 +138,11 @@ if ($_SESSION["language"]) {
putenv(sprintf("LANG=%s", $_SESSION["language"]));
putenv(sprintf("LANGUAGE=%s", $_SESSION["language"]));
$_SESSION["locale"] = _setlocale(LC_ALL, $_SESSION["language"]);
bindtextdomain($project, realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR .
".." . DIRECTORY_SEPARATOR . "po"));
bindtextdomain($project,
realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR .
".." . DIRECTORY_SEPARATOR . "po"));
bindtextdomain($project . "js",
realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR .
".." . DIRECTORY_SEPARATOR . "po"));
textdomain($project);
?>

View file

@ -27,7 +27,7 @@
*/
function handleerror(xmldata) {
if ($('error', xmldata).size()) {
var msg = 'Es ist ein Fehler aufgetreten:\n';
var msg = intl.translate('An error occured:\n');
$('error', xmldata).find('errormsg').each(function(i) {
msg += $(this).text();
});
@ -44,9 +44,9 @@ function updateDirectories(xmldata) {
var filecount = $('filecount', xmldata).text();
var filesize = $('filesize', xmldata).text();
var maydelete = $('maydelete', xmldata).text();
var htmltext = '<td>' + dirname + '</td><td>' + groups + '</td><td>' + filecount + ', ' + filesize + '</td><td><a id="edit' + dirname + '" class="editlink" href="#" title="Die Gruppenzuordnungen dieses Verzeichnisses bearbeiten"><img class="actionicon" src="images/groups.png" width="16" height="16" alt="Gruppen"/></a>';
var htmltext = '<td>' + dirname + '</td><td>' + groups + '</td><td>' + filecount + ', ' + filesize + '</td><td><a id="edit' + dirname + '" class="editlink" href="#" title="' + intl.translate("Edit this directory's group assignments.") + '"><img class="actionicon" src="images/groups.png" width="16" height="16" alt="' + intl.translate("groups") + '"/></a>';
if (maydelete == '1') {
htmltext = htmltext + '<a id="delete' + dirname + '" class="deletelink" href="#" title="Dieses Verzeichnis löschen"><img class="actionicon" src="images/delete.png" width="16" height="16" alt="löschen" /></a>';
htmltext = htmltext + '<a id="delete' + dirname + '" class="deletelink" href="#" title="' + intl.translate("Delete this directory.") + '"><img class="actionicon" src="images/delete.png" width="16" height="16" alt="' + intl.translate("delete") + '" /></a>';
}
htmltext = htmltext + '</td>';
$('#dirtable').find('tr#dir' + dirname).empty().append(htmltext);

52
admin/scripts/i18n.php Normal file
View file

@ -0,0 +1,52 @@
<?php
/**
* JavaScript internationalization code for DAVAdmin.
*
* @author Jan Dittberner <jan@dittberner.info>
* @version $Id$
* @license GPL
* @package DAVAdmin
*
* Copyright (c) 2007 Jan Dittberner
*
* This file is part of DAVAdmin.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
/** Include the internationalization code. */
require_once(".." . DIRECTORY_SEPARATOR . "i18n.inc.php");
textdomain("davadminjs");
$translatable = file(realpath(implode(DIRECTORY_SEPARATOR,
array(dirname(__FILE__), "..", "..",
"po", "transjs.txt"))));
header("Content-Type: application/x-javascript;charset=UTF-8");
if (is_array($translatable)) {
print "i18nDict = ";
$transmap = array();
foreach ($translatable as $sentence) {
$sentence = stripcslashes(stripcslashes(trim($sentence)));
$transmap[$sentence] = _($sentence);
}
print json_encode($transmap);
print ";";
} else {
print "i18nDict = {};";
}
print("\n\nvar intl = new i18n(i18nDict);");
?>

View file

@ -0,0 +1,80 @@
/*
* Helper functions
*/
function stripStr(str) {
return str.replace(/^\s*/, "").replace(/\s*$/, "");
}
// Multiline strip
function stripStrML(str) {
// Split because m flag doesn't exist before JS1.5 and we need to
// strip newlines anyway
var parts = str.split('\n');
for (var i=0; i<parts.length; i++)
parts[i] = stripStr(parts[i]);
// Don't join with empty strings, because it "concats" words
// And strip again
return stripStr(parts.join(" "));
}
/*
* C-printf like function, which substitutes %s with parameters
* given in list. %%s is used to escape %s.
*
* Doesn't work in IE5.0 (splice)
*/
function printf(S, L) {
if (!L) return S;
var nS = "";
var tS = S.split("%s");
for(var i=0; i<L.length; i++) {
if (tS[i].lastIndexOf('%') == tS[i].length-1 && i != L.length-1)
tS[i] += "s"+tS.splice(i+1,1)[0];
nS += tS[i] + L[i];
}
return nS + tS[tS.length-1];
}
/*
* i18n
*/
function i18n(i18n_dict) {
this.i18nd = i18n_dict;
// Change to entity representation non-ASCII characters
this.toEntity = function (str) {
var newstr = "";
for (var i=0;i<str.length; i++) {
if (str.charCodeAt(i) > 128)
newstr += "&#"+str.charCodeAt(i)+";";
else
newstr += str.charAt(i);
}
return newstr;
}
// Return translation, if translation dictionary exists and has a translation.
this.translate = function (str, params) {
var transl = str;
if (this.i18nd && this.i18nd[str])
transl = this.i18nd[str];
return printf(transl, params);
}
this.translateNodes = function () {
var nodes = document.getElementsByTagName("*");
for (var i=0;i<nodes.length;i++)
if (nodes[i].className.match("i18n")) {
var orig = stripStrML(nodes[i].innerHTML);
var transl = this.translate(orig);
// If translation not found, try again with
// entity representation
if (transl == orig) transl = this.translate(this.toEntity(orig));
nodes[i].innerHTML = transl;
}
}
}

View file

@ -32,4 +32,6 @@
<link rel="stylesheet" type="text/css" href="format.css" />
<link rel="stylesheet" type="text/css" href="dynaform.css" />
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript" src="scripts/translate.js"></script>
<script type="text/javascript" src="scripts/i18n.php" ></script>
<body>