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 
 33  * from scripts and skins.
 34  * @param {String} script
 35  * @param {String} scanDirs
 36  * @param {String} potFile
 37  */
 38 Root.prototype.extractMessages = function(script, scanDirs, potFile) {
 39    var temp = {print: global.print, readFile: global.readFile};
 40    global.print = function(str) {app.log(str);}
 41    global.readFile = function(fpath, encoding) {
 42       res.push();
 43       var file = new helma.File(fpath);
 44       file.open({charset: encoding || "UTF-8"});
 45       var str;
 46       while ((str = file.readln()) !== null) {
 47          res.writeln(str);
 48       }
 49       file.close();
 50       return res.pop();
 51    }
 52    var args = ["-o", potFile, "-e", "utf-8"];
 53    for each (var dir in scanDirs.split(" ")) {
 54       args.push(app.dir + "/../" + dir);
 55    }
 56    var file = new helma.File(script);
 57    var MessageParser = new Function(file.readAll());
 58    MessageParser.apply(global, args);
 59    global.print = temp.print;
 60    global.readFile = temp.readFile;
 61    return;
 62 }
 63 
 64 /**
 65  * This method is useful for disambiguation of messages (single words most of
 66  * the time) that have different meanings depending on the context.
 67  * Example: comment (the verb "to comment" vs the noun "a comment")
 68  * @param {Object} key The message ID
 69  * @param {Object} context The context of the message
 70  * @returns String
 71  */
 72 function cgettext(key, context) {
 73    var msgId = cgettext.getKey(key, context);
 74    var text = jala.i18n.translate(msgId);
 75    return text === msgId ? key : text;
 76 }
 77 
 78 /**
 79  * Helper method to define the message ID depending on the context.
 80  * @param {Object} key
 81  * @param {Object} context
 82  * @returns String
 83  */
 84 cgettext.getKey = function(key, context) {
 85    return context ? key + " // " + context : key;
 86 }
 87 
 88 /**
 89  * 
 90  * @param {Object} param
 91  * @param {String} text
 92  * @returns String
 93  * @see jala.i18n.gettext
 94  */
 95 function gettext_macro(param, text /*, value1, value2, ...*/) {
 96    if (!text) {
 97       return;
 98    }
 99    var re = gettext_macro.REGEX;
100    var args = [text.replace(re, String.SPACE)];
101    for (var i=2; i<arguments.length; i+=1) {
102       args.push(arguments[i]);
103    }
104    if (param.context) {
105       return cgettext.call(this, args[0], param.context);
106    }
107    return gettext.apply(this, args);
108 }
109 
110 /**
111  * The regular expression used to reduce multiple whitespace characters.
112  * @constant
113  */
114 gettext_macro.REGEX = /\s+/g;
115 
116 /**
117  * 
118  * @param {Object} param
119  * @param {String} singular
120  * @param {String} plural
121  * @returns String
122  * @see jala.i18n#ngettext
123  */
124 function ngettext_macro(param, singular, plural /*, value1, value2, ...*/) {
125    if (!singular || !plural) {
126       return;
127    }
128    var re = gettext_macro.REGEX;
129    var args = [singular.replace(re, String.SPACE), plural.replace(re, String.SPACE)];
130    for (var i=3; i<arguments.length; i+=1) {
131       args.push(arguments[i]);
132    }
133    return ngettext.apply(this, args);
134 }
135 
136 /**
137  * 
138  * @param {Object} param
139  * @param {Object} singular
140  * @param {Object} plural
141  * @see jala.i18n#markgettext
142  */
143 function markgettext_macro(param, singular, plural) {
144    return markgettext.call(this, singular, plural);
145 }
146