3 * @requires jQuery v1.1 or later
5 * Examples at: http://recurser.com/articles/2008/02/21/jquery-i18n-translation-plugin/
6 * Dual licensed under the MIT and GPL licenses:
7 * http://www.opensource.org/licenses/mit-license.php
8 * http://www.gnu.org/licenses/gpl.html
10 * Based on 'javascript i18n that almost doesn't suck' by markos
11 * http://markos.gaivo.net/blog/?p=100
14 * Version: 1.0.0 Feb-10-2008
18 * i18n provides a mechanism for translating strings using a jscript dictionary.
30 * Initialise the dictionary and translate nodes
32 * @param property_list i18n_dict : The dictionary to use for translation
34 setDictionary: function(i18n_dict) {
35 i18n_dict = i18n_dict;
40 * The actual translation function. Looks the given string up in the
41 * dictionary and returns the translation if one exists. If a translation
42 * is not found, returns the original word
44 * @param string str : The string to translate
45 * @param property_list params : params for using printf() on the string
46 * @return string : Translated word
49 _: function (str, params) {
51 if (i18n_dict&& i18n_dict[str]) {
52 transl = i18n_dict[str];
54 return this.printf(transl, params);
59 * Change non-ASCII characters to entity representation
61 * @param string str : The string to transform
62 * @return string result : Original string with non-ASCII content converted to entities
65 toEntity: function (str) {
67 for (var i=0;i<str.length; i++) {
68 if (str.charCodeAt(i) > 128)
69 result += "&#"+str.charCodeAt(i)+";";
71 result += str.charAt(i);
79 * @param string str : The string to strip
80 * @return string result : Stripped string
83 stripStr: function(str) {
84 return str.replace(/^\s*/, "").replace(/\s*$/, "");
90 * @param string str : The multi-line string to strip
91 * @return string result : Stripped string
94 stripStrML: function(str) {
95 // Split because m flag doesn't exist before JS1.5 and we need to
96 // strip newlines anyway
97 var parts = str.split('\n');
98 for (var i=0; i<parts.length; i++)
99 parts[i] = stripStr(parts[i]);
101 // Don't join with empty strings, because it "concats" words
103 return stripStr(parts.join(" "));
108 * C-printf like function, which substitutes %s with parameters
109 * given in list. %%s is used to escape %s.
111 * Doesn't work in IE5.0 (splice)
113 * @param string S : string to perform printf on.
114 * @param string L : Array of arguments for printf()
116 printf: function(S, L) {
120 var tS = S.split("%s");
122 for(var i=0; i<L.length; i++) {
123 if (tS[i].lastIndexOf('%') == tS[i].length-1 && i != L.length-1)
124 tS[i] += "s"+tS.splice(i+1,1)[0];
127 return nS + tS[tS.length-1];