1if(typeof(ss) == 'undefined') ss = {}; 2 3/* 4 * Lightweight clientside i18n implementation. 5 * Caution: Only available after DOM loaded because we need to detect the language 6 * 7 * For non-i18n stub implementation, see sapphire/javascript/i18nx.js 8 * 9 * Based on jQuery i18n plugin: 1.0.0 Feb-10-2008 10 * 11 * Dual licensed under the MIT and GPL licenses: 12 * http://www.opensource.org/licenses/mit-license.php 13 * http://www.gnu.org/licenses/gpl.html 14 * 15 * Based on 'javascript i18n that almost doesn't suck' by markos 16 * http://markos.gaivo.net/blog/?p=100 17 */ 18ss.i18n = { 19 20 currentLocale: null, 21 22 defaultLocale: 'en_US', 23 24 lang: {}, 25 26 init: function() { 27 this.currentLocale = this.detectLocale(); 28 }, 29 30 /** 31 * set_locale() 32 * Set locale in long format, e.g. "de_AT" for Austrian German. 33 * @param string locale 34 */ 35 setLocale: function(locale) { 36 this.currentLocale = locale; 37 }, 38 39 /** 40 * getLocale() 41 * Get locale in long format. Falls back to i18n.defaut_locale. 42 * @return string 43 */ 44 getLocale: function() { 45 return (this.currentLocale) ? this.currentLocale : this.defaultLocale; 46 }, 47 48 /** 49 * _() 50 * The actual translation function. Looks the given string up in the 51 * dictionary and returns the translation if one exists. If a translation 52 * is not found, returns the original word 53 * 54 * @param string entity A "long" locale format, e.g. "de_DE" (Required) 55 * @param string fallbackString (Required) 56 * @param int priority (not used) 57 * @param string context Give translators context for the string 58 * @return string : Translated word 59 * 60 */ 61 _t: function (entity, fallbackString, priority, context) { 62 if (this.lang && this.lang[this.getLocale()] && this.lang[this.getLocale()][entity]) { 63 return this.lang[this.getLocale()][entity]; 64 } else if (this.lang && this.lang[this.defaultLocale] && this.lang[this.defaultLocale][entity]) { 65 return this.lang[this.defaultLocale][entity]; 66 } else if(fallbackString) { 67 return fallbackString; 68 } else { 69 return ''; 70 } 71 }, 72 73 /** 74 * Add entities to a dictionary. If a dictionary doesn't 75 * exist for this locale, its automatically created. 76 * Existing entities are overwritten. 77 * 78 * @param string locale 79 * @param Object dict 80 */ 81 addDictionary: function(locale, dict) { 82 if(!this.lang[locale]) this.lang[locale] = {}; 83 for(entity in dict) { 84 this.lang[locale][entity] = dict[entity]; 85 } 86 }, 87 88 /** 89 * Get dictionary for a specific locale. 90 * 91 * @param string locale 92 */ 93 getDictionary: function(locale) { 94 return this.lang[locale]; 95 }, 96 97 /** 98 * stripStr() 99 * 100 * @param string str : The string to strip 101 * @return string result : Stripped string 102 * 103 */ 104 stripStr: function(str) { 105 return str.replace(/^\s*/, "").replace(/\s*$/, ""); 106 }, 107 108 /** 109 * stripStrML() 110 * 111 * @param string str : The multi-line string to strip 112 * @return string result : Stripped string 113 * 114 */ 115 stripStrML: function(str) { 116 // Split because m flag doesn't exist before JS1.5 and we need to 117 // strip newlines anyway 118 var parts = str.split('\n'); 119 for (var i=0; i<parts.length; i++) 120 parts[i] = stripStr(parts[i]); 121 122 // Don't join with empty strings, because it "concats" words 123 // And strip again 124 return stripStr(parts.join(" ")); 125 }, 126 127 /* 128 * printf() 129 * C-printf like function, which substitutes %s with parameters 130 * given in list. %%s is used to escape %s. 131 * 132 * Doesn't work in IE5.0 (splice) 133 * 134 * @param string S : string to perform printf on. 135 * @param string L : Array of arguments for printf() 136 */ 137 sprintf: function(S) { 138 if (arguments.length == 1) return S; 139 140 var nS = ""; 141 var tS = S.split("%s"); 142 143 var args = []; 144 for (var i=1, len = arguments.length; i <len; ++i) { 145 args.push(arguments[i]); 146 }; 147 148 for(var i=0; i<args.length; i++) { 149 if (tS[i].lastIndexOf('%') == tS[i].length-1 && i != args.length-1) 150 tS[i] += "s"+tS.splice(i+1,1)[0]; 151 nS += tS[i] + args[i]; 152 } 153 return nS + tS[tS.length-1]; 154 }, 155 156 /** 157 * Detect document language settings by looking at <meta> tags. 158 * If no match is found, returns this.defaultLocale. 159 * 160 * @todo get by <html lang=''> - needs modification of SSViewer 161 * 162 * @return string Locale in mixed lowercase/uppercase format suitable 163 * for usage in ss.i18n.lang arrays (e.g. 'en_US'). 164 */ 165 detectLocale: function() { 166 var rawLocale; 167 var detectedLocale; 168 169 // get by meta 170 var metas = document.getElementsByTagName('meta'); 171 for(var i=0; i<metas.length; i++) { 172 if(metas[i].attributes['http-equiv'] && metas[i].attributes['http-equiv'].nodeValue.toLowerCase() == 'content-language') { 173 rawLocale = metas[i].attributes['content'].nodeValue; 174 } 175 } 176 177 // fallback to default locale 178 if(!rawLocale) rawLocale = this.defaultLocale; 179 180 var rawLocaleParts = rawLocale.match(/([^-|_]*)[-|_](.*)/); 181 // get locale (e.g. 'en_US') from common name (e.g. 'en') 182 // by looking at ss.i18n.lang tables 183 if(rawLocale.length == 2) { 184 for(compareLocale in ss.i18n.lang) { 185 if(compareLocale.substr(0,2).toLowerCase() == rawLocale.toLowerCase()) { 186 detectedLocale = compareLocale; 187 break; 188 } 189 } 190 } else if(rawLocaleParts) { 191 detectedLocale = rawLocaleParts[1].toLowerCase() + '_' + rawLocaleParts[2].toUpperCase(); 192 } 193 194 return detectedLocale; 195 }, 196 197 /** 198 * Attach an event listener to the given object. 199 * Modeled after behaviour.js, but externalized 200 * to keep the i18n library standalone for now. 201 */ 202 addEvent: function(obj, evType, fn, useCapture){ 203 if (obj.addEventListener){ 204 obj.addEventListener(evType, fn, useCapture); 205 return true; 206 } else if (obj.attachEvent){
207 var r = obj.attachEvent("on"+evType, fn); 208 return r; 209 } else { 210 alert("Handler could not be attached"); 211 } 212 } 213}; 214 215 216 217ss.i18n.addEvent(window, "load", function() { 218 ss.i18n.init(); 219});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.