]> git.openstreetmap.org Git - osqa.git/blob - forum/skins/default/media/js/jquery.i18n.js
initial import
[osqa.git] / forum / skins / default / media / js / jquery.i18n.js
1 /*
2  * jQuery i18n plugin
3  * @requires jQuery v1.1 or later
4  *
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
9  *
10  * Based on 'javascript i18n that almost doesn't suck' by markos
11  * http://markos.gaivo.net/blog/?p=100
12  *
13  * Revision: $Id$
14  * Version: 1.0.0  Feb-10-2008
15  */
16  (function($) {
17 /**
18  * i18n provides a mechanism for translating strings using a jscript dictionary.
19  *
20  */
21
22
23 /*
24  * i18n property list
25  */
26 $.i18n = {
27         
28 /**
29  * setDictionary()
30  * Initialise the dictionary and translate nodes
31  *
32  * @param property_list i18n_dict : The dictionary to use for translation
33  */
34         setDictionary: function(i18n_dict) {
35                 i18n_dict = i18n_dict;
36         },
37         
38 /**
39  * _()
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
43  *
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
47  *
48  */
49         _: function (str, params) {
50                 var transl = str;
51                 if (i18n_dict&& i18n_dict[str]) {
52                         transl = i18n_dict[str];
53                 }
54                 return this.printf(transl, params);
55         },
56         
57 /**
58  * toEntity()
59  * Change non-ASCII characters to entity representation 
60  *
61  * @param string str : The string to transform
62  * @return string result : Original string with non-ASCII content converted to entities
63  *
64  */
65         toEntity: function (str) {
66                 var result = '';
67                 for (var i=0;i<str.length; i++) {
68                         if (str.charCodeAt(i) > 128)
69                                 result += "&#"+str.charCodeAt(i)+";";
70                         else
71                                 result += str.charAt(i);
72                 }
73                 return result;
74         },
75         
76 /**
77  * stripStr()
78  *
79  * @param string str : The string to strip
80  * @return string result : Stripped string
81  *
82  */
83         stripStr: function(str) {
84                 return str.replace(/^\s*/, "").replace(/\s*$/, "");
85         },
86         
87 /**
88  * stripStrML()
89  *
90  * @param string str : The multi-line string to strip
91  * @return string result : Stripped string
92  *
93  */
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]);
100         
101                 // Don't join with empty strings, because it "concats" words
102                 // And strip again
103                 return stripStr(parts.join(" "));
104         },
105
106 /*
107  * printf()
108  * C-printf like function, which substitutes %s with parameters
109  * given in list. %%s is used to escape %s.
110  *
111  * Doesn't work in IE5.0 (splice)
112  *
113  * @param string S : string to perform printf on.
114  * @param string L : Array of arguments for printf()
115  */
116         printf: function(S, L) {
117                 if (!L) return S;
118
119                 var nS = "";
120                 var tS = S.split("%s");
121
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];
125                         nS += tS[i] + L[i];
126                 }
127                 return nS + tS[tS.length-1];
128         }
129
130 };
131
132
133 })(jQuery);