1 //
  2 // The Antville Project
  3 // http://code.google.com/p/antville
  4 //
  5 // Copyright 2001-2007 by The Antville People
  6 //
  7 // Licensed under the Apache License, Version 2.0 (the ``License'');
  8 // you may not use this file except in compliance with the License.
  9 // You may obtain a copy of the License at
 10 //
 11 //    http://www.apache.org/licenses/LICENSE-2.0
 12 //
 13 // Unless required by applicable law or agreed to in writing, software
 14 // distributed under the License is distributed on an ``AS IS'' BASIS,
 15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 16 // See the License for the specific language governing permissions and
 17 // limitations under the License.
 18 //
 19 // $Revision$
 20 // $LastChangedBy$
 21 // $LastChangedDate$
 22 // $URL$
 23 //
 24 
 25 /**
 26  * @fileOverview Contains redefined or additional methods for 
 27  * internationalization and localization.
 28  */
 29 
 30 /**
 31  * This method is called from the build script to extract gettext call strings 
 32  * from scripts and skins.
 33  * @param {String} script
 34  * @param {String} scanDirs
 35  * @param {String} potFile
 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
 65  * the time) that have different meanings depending on the context.
 66  * Example: comment (the verb "to comment" vs the noun "a comment")
 67  * @param {Object} key The message ID
 68  * @param {Object} context The context of the message
 69  * @returns String
 70  */
 71 function cgettext(key, context) {
 72    var msgId = cgettext.getKey(key, context);
 73    var text = jala.i18n.translate(msgId);
 74    return text === msgId ? key : text;
 75 }
 76 
 77 /**
 78  * Helper method to define the message ID depending on the context.
 79  * @param {Object} key
 80  * @param {Object} context
 81  * @returns String
 82  */
 83 cgettext.getKey = function(key, context) {
 84    return context ? key + " // " + context : key;
 85 }
 86 
 87 /**
 88  * 
 89  * @param {Object} param
 90  * @param {String} text
 91  * @returns String
 92  * @see jala.i18n.gettext
 93  */
 94 function gettext_macro(param, text /*, value1, value2, ...*/) {
 95    if (!text) {
 96       return;
 97    }
 98    var re = /(\s*)(?:\r|\n)\s*/g;
 99    var args = [text.replace(re, "$1")];
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  * 
111  * @param {Object} param
112  * @param {String} singular
113  * @param {String} plural
114  * @returns String
115  * @see jala.i18n#ngettext
116  */
117 function ngettext_macro(param, singular, plural /*, value1, value2, ...*/) {
118    if (!singular || !plural) {
119       return;
120    }
121    var re = /(\s*)(?:\r|\n)\s*/g;
122    var args = [singular.replace(re, "$1"), plural.replace(re, "$1")];
123    for (var i=3; i<arguments.length; i+=1) {
124       args.push(arguments[i]);
125    }
126    return ngettext.apply(this, args);
127 }
128 
129 /**
130  * 
131  * @param {Object} param
132  * @param {Object} singular
133  * @param {Object} plural
134  * @see jala.i18n#markgettext
135  */
136 function markgettext_macro(param, singular, plural) {
137    return markgettext.call(this, singular, plural);
138 }
139