      1 // The Antville Project
      2 // http://code.google.com/p/antville
      3 //
      4 // Copyright 2007-2011 by Tobi Schäfer.
      5 //
      6 // Copyright 2001–2007 Robert Gaggl, Hannes Wallnöfer, Tobi Schäfer,
      7 // Matthias & Michael Platzer, Christoph Lincke.
      8 //
      9 // Licensed under the Apache License, Version 2.0 (the ``License'');
     10 // you may not use this file except in compliance with the License.
     11 // You may obtain a copy of the License at
     12 //
     13 //    http://www.apache.org/licenses/LICENSE-2.0
     14 //
     15 // Unless required by applicable law or agreed to in writing, software
     16 // distributed under the License is distributed on an ``AS IS'' BASIS,
     17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     18 // See the License for the specific language governing permissions and
     19 // limitations under the License.
     20 //
     21 // $Revision$
     22 // $LastChangedBy$
     23 // $LastChangedDate$
     24 // $URL$
     25
     26 /**
     27  * @fileOverview Contains redefined or additional methods for
     28  * internationalization and localization.
     29  */
     30
     31 /**
     32  * This method is called from the build script to extract gettext call strings from scripts and skins.
     33  * @param {String} script The filename of the script containing the MessageParser code.
     34  * @param {String} scanDirs The list of directory names to be scanned for i18n.
     35  * @param {String} potFile The filename for the output POT file.
     36  */
     37 Root.prototype.extractMessages = function(script, scanDirs, potFile) {
     38    var temp = {print: global.print, readFile: global.readFile};
     39    global.print = function(str) {app.log(str);}
     40    global.readFile = function(fpath, encoding) {
     41       res.push();
     42       var file = new helma.File(fpath);
     43       file.open({charset: encoding || "UTF-8"});
     44       var str;
     45       while ((str = file.readln()) !== null) {
     46          res.writeln(str);
     47       }
     48       file.close();
     49       return res.pop();
     50    }
     51    var args = ["-o", potFile, "-e", "utf-8"];
     52    for each (var dir in scanDirs.split(" ")) {
     53       args.push(app.dir + "/../" + dir);
     54    }
     55    var file = new helma.File(script);
     56    var MessageParser = new Function(file.readAll());
     57    MessageParser.apply(global, args);
     58    global.print = temp.print;
     59    global.readFile = temp.readFile;
     60    return;
     61 }
     62
     63 /**
     64  * This method is useful for disambiguation of messages (single words most of the time) that have different meanings depending on the context. Example: comment – the verb "to comment" vs the noun "a comment".
     65  * @param {Object} key The message ID.
     66  * @param {Object} context The context of the message.
     67  * @example cgettext('comment', 'verb')
     68  * @returns {String}
     69  */
     70 function cgettext(key, context) {
     71    var msgId = cgettext.getKey(key, context);
     72    var text = jala.i18n.translate(msgId);
     73    return text === msgId ? key : text;
     74 }
     75
     76 /**
     77  * Helper method to define the message ID depending on the context.
     78  * @param {Object} key The message ID.
     79  * @param {Object} context The context of the message.
     80  * @returns {String} The message ID, probably suffixed with '//' plus the context
     81  * @example cgettext.getKey('comment', 'verb') ===> 'comment // verb'
     82  */
     83 cgettext.getKey = function(key, context) {
     84    return context ? key + " // " + context : key;
     85 }
     86
     87 /**
     88  * Helma macro wrapper for the gettext() method.
     89  * @param {Object} param The default Helma macro parameter object.
     90  * @param {String} text The text used as message ID.
     91  * @returns {String} Either the translated or the original text.
     92  * @see jala.i18n.gettext
     93  */
     94 function gettext_macro(param, text /*, value1, value2, ...*/) {
     95    if (!text) {
     96       return;
     97    }
     98    var re = gettext_macro.REGEX;
     99    var args = [text.toString().replace(re, String.SPACE)];
    100    for (var i=2; i<arguments.length; i+=1) {
    101       args.push(arguments[i]);
    102    }
    103    if (param.context) {
    104       return cgettext.call(this, args[0], param.context);
    105    }
    106    return gettext.apply(this, args);
    107 }
    108
    109 /**
    110  * The regular expression used to reduce multiple whitespace characters.
    111  * @constant
    112  */
    113 gettext_macro.REGEX = /\s+/g;
    114
    115 /**
    116  * Helma macro wrapper for the ngettext() method.
    117  * @param {Object} param The default Helma macro parameter object.
    118  * @param {String} singular The text used as the singular message ID.
    119  * @param {String} plural The text used as the plural message ID.
    120  * @returns String Either the translated or the original string.
    121  * @see jala.i18n.ngettext
    122  */
    123 function ngettext_macro(param, singular, plural /*, value1, value2, ...*/) {
    124    if (!singular || !plural) {
    125       return;
    126    }
    127    var re = gettext_macro.REGEX;
    128    var args = [singular.toString().replace(re, String.SPACE), plural.replace(re, String.SPACE)];
    129    for (var i=3; i<arguments.length; i+=1) {
    130       args.push(arguments[i]);
    131    }
    132    return ngettext.apply(this, args);
    133 }
    134
    135 /**
    136  * Helma macro wrapper for the markgettext() method.
    137  * @param {Object} param The default Helma macro parameter object.
    138  * @param {Object} singular The text used as the singular message ID.
    139  * @param {Object} plural The text used as the plural message ID.
    140  * @see jala.i18n.markgettext
    141  */
    142 function markgettext_macro(param, singular, plural) {
    143    return markgettext.call(this, singular, plural);
    144 }
    145