1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
/* See license.txt for terms of usage */
// ********************************************************************************************* //
// Constants
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
const DEFAULT_LOCALE = "en-US";
var EXPORTED_SYMBOLS = [];
// ********************************************************************************************* //
// Services
Cu.import("resource://firebug/fbtrace.js");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://firebug/prefLoader.js");
Cu.import("resource://gre/modules/PluralForm.jsm");
// ********************************************************************************************* //
// Firebug UI Localization
var stringBundleService = Services.strings;
var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
// This module
var Locale = {};
/*
* $STR - intended for localization of a static string.
* $STRF - intended for localization of a string with dynamically inserted values.
* $STRP - intended for localization of a string with dynamically plural forms.
*
* Notes:
* 1) Name with _ in place of spaces is the key in the firebug.properties file.
* 2) If the specified key isn't localized for particular language, both methods use
* the part after the last dot (in the specified name) as the return value.
*
* Examples:
* $STR("Label"); - search for key "Label" within the firebug.properties file
* and returns its value. If the key doesn't exist returns "Label".
*
* $STR("Button Label"); - search for key "Button_Label" withing the firebug.properties
* file. If the key doesn't exist returns "Button Label".
*
* $STR("net.Response Header"); - search for key "net.Response_Header". If the key doesn't
* exist returns "Response Header".
*
* firebug.properties:
* net.timing.Request_Time=Request Time: %S [%S]
*
* var param1 = 10;
* var param2 = "ms";
* $STRF("net.timing.Request Time", param1, param2); -> "Request Time: 10 [ms]"
*
* - search for key "net.timing.Request_Time" within the firebug.properties file. Parameters
* are inserted at specified places (%S) in the same order as they are passed. If the
* key doesn't exist the method returns "Request Time".
*/
Locale.$STR = function(name, bundle)
{
// The empty string localizes to the empty string.
if (!name)
return "";
var strKey = name.replace(" ", "_", "g");
if (!PrefLoader.getPref("useDefaultLocale"))
{
try
{
if (bundle)
return bundle.getString(strKey);
else
return Locale.getStringBundle().GetStringFromName(strKey);
}
catch (err)
{
if (FBTrace.DBG_LOCALE)
FBTrace.sysout("Locale.$STR FAILS, missing localized string for '" + name + "'", err);
}
}
try
{
// The en-US string should be always available.
var defaultBundle = Locale.getDefaultStringBundle();
if (defaultBundle)
return defaultBundle.GetStringFromName(strKey);
}
catch (err)
{
if (FBTrace.DBG_LOCALE || FBTrace.DBG_ERRORS)
FBTrace.sysout("Locale.$STR FAILS, missing default string for '" + name + "'", err);
}
// Don't panic now and use only the label after last dot.
var index = name.lastIndexOf(".");
if (index > 0 && name.charAt(index-1) != "\\")
name = name.substr(index + 1);
name = name.replace("_", " ", "g");
return name;
};
Locale.$STRF = function(name, args, bundle)
{
var strKey = name.replace(" ", "_", "g");
if (!PrefLoader.getPref("useDefaultLocale"))
{
try
{
if (bundle)
return bundle.getFormattedString(strKey, args);
else
return Locale.getStringBundle().formatStringFromName(strKey, args, args.length);
}
catch (err)
{
if (FBTrace.DBG_LOCALE)
FBTrace.sysout("Locale.$STRF FAILS, missing localized string for '" + name + "'", err);
}
}
try
{
// The en-US string should be always available.
var defaultBundle = Locale.getDefaultStringBundle();
if (defaultBundle)
return defaultBundle.formatStringFromName(strKey, args, args.length);
}
catch (err)
{
if (FBTrace.DBG_LOCALE || FBTrace.DBG_ERRORS)
FBTrace.sysout("Locale.$STRF FAILS, missing default string for '" + name + "'", err);
}
// Don't panic now and use only the label after last dot.
var index = name.lastIndexOf(".");
if (index > 0)
name = name.substr(index + 1);
return name;
};
Locale.$STRP = function(name, args, index, bundle)
{
// xxxHonza:
// pluralRule from chrome://global/locale/intl.properties for Chinese is 1,
// which is wrong, it should be 0.
var getPluralForm = PluralForm.get;
var getNumForms = PluralForm.numForms;
// Get custom plural rule; otherwise the rule from chrome://global/locale/intl.properties
// (depends on the current locale) is used.
var pluralRule = Locale.getPluralRule();
if (!isNaN(parseInt(pluralRule, 10)))
[getPluralForm, getNumForms] = PluralForm.makeGetter(pluralRule);
// Index of the argument with plural form (there must be only one arg that needs plural form).
if (!index)
index = 0;
// Get proper plural form from the string (depends on the current Firefox locale).
var translatedString = Locale.$STRF(name, args, bundle);
if (translatedString.search(";") > 0)
return getPluralForm(args[index], translatedString);
// translatedString contains no ";", either rule 0 or getString fails
return translatedString;
};
/*
* Use the current value of the attribute as a key to look up the localized value.
*/
Locale.internationalize = function(element, attr, args)
{
if (element)
{
var xulString = element.getAttribute(attr);
if (xulString)
{
var localized = args ? Locale.$STRF(xulString, args) : Locale.$STR(xulString);
// Set localized value of the attribute only if it exists.
if (localized)
element.setAttribute(attr, localized);
}
}
else
{
if (FBTrace.DBG_LOCALE)
FBTrace.sysout("Failed to internationalize element with attr "+attr+" args:"+args);
}
};
Locale.internationalizeElements = function(doc, elements, attributes)
{
for (var i=0; i<elements.length; i++)
{
var element = elements[i];
if (typeof(elements) == "string")
element = doc.getElementById(elements[i]);
if (!element)
continue;
// Remove fbInternational class, so that the label is not translated again later.
element.classList.remove("fbInternational");
for (var j=0; j<attributes.length; j++)
{
if (element.hasAttribute(attributes[j]))
Locale.internationalize(element, attributes[j]);
}
}
};
Locale.registerStringBundle = function(bundleURI)
{
// Notice that this category entry must not be persistent in Fx 4.0
categoryManager.addCategoryEntry("strings_firebug", bundleURI, "", false, true);
this.stringBundle = null;
bundleURI = getDefaultStringBundleURI(bundleURI);
categoryManager.addCategoryEntry("default_strings_firebug", bundleURI, "", false, true);
this.defaultStringBundle = null;
};
Locale.getStringBundle = function()
{
if (!this.stringBundle)
this.stringBundle = stringBundleService.createExtensibleBundle("strings_firebug");
return this.stringBundle;
};
Locale.getDefaultStringBundle = function()
{
if (!this.defaultStringBundle)
this.defaultStringBundle = stringBundleService.createExtensibleBundle("default_strings_firebug");
return this.defaultStringBundle;
};
Locale.getPluralRule = function()
{
try
{
return this.getStringBundle().GetStringFromName("pluralRule");
}
catch (err)
{
}
};
Locale.getFormattedKey = function(win, modifiers, key, keyConstant)
{
platformKeys = {};
platformKeys.shift = Locale.$STR("VK_SHIFT");
platformKeys.meta = Locale.$STR("VK_META");
platformKeys.alt = Locale.$STR("VK_ALT");
platformKeys.ctrl = Locale.$STR("VK_CONTROL");
platformKeys.sep = Locale.$STR("MODIFIER_SEPARATOR");
switch (Services.prefs.getIntPref("ui.key.accelKey"))
{
case win.KeyEvent.DOM_VK_CONTROL:
platformKeys.accel = platformKeys.ctrl;
break;
case win.KeyEvent.DOM_VK_ALT:
platformKeys.accel = platformKeys.alt;
break;
case win.KeyEvent.DOM_VK_META:
platformKeys.accel = platformKeys.meta;
break;
default:
platformKeys.accel = (win.navigator.platform.search("Mac") != -1 ? platformKeys.meta :
platformKeys.ctrl);
}
if ((modifiers == "shift,alt,control,accel" && keyConstant == "VK_SCROLL_LOCK") ||
(key == "" || (!key && keyConstant == "")))
{
return "";
}
var val = "";
if (modifiers)
{
val = modifiers.replace(/^[\s,]+|[\s,]+$/g, "").split(/[\s,]+/g).join(platformKeys.sep).
replace("alt", platformKeys.alt).replace("shift", platformKeys.shift).
replace("control", platformKeys.ctrl).replace("meta", platformKeys.meta).
replace("accel", platformKeys.accel) +
platformKeys.sep;
}
if (key)
return val += key;
if (keyConstant)
{
var localizedKey = Locale.$STR(keyConstant);
// Create human friendly alternative ourself, if there is no translation
// for the key constant
if (localizedKey.lastIndexOf("VK ", 0) === 0)
localizedKey = capitalize(localizedKey.replace("VK ", ""), true);
val += localizedKey;
}
return val;
}
// ********************************************************************************************* //
// Helpers
// This module needs to be independent of any other modules, so this is mainly a copy of
// Str.capitalize().
function capitalize(string)
{
function capitalizeFirstLetter(string)
{
var rest = string.slice(1).toLowerCase();
return string.charAt(0).toUpperCase() + rest;
}
return string.split(" ").map(capitalizeFirstLetter).join(" ");
}
function getDefaultStringBundleURI(bundleURI)
{
var chromeRegistry = Cc["@mozilla.org/chrome/chrome-registry;1"].
getService(Ci.nsIChromeRegistry);
var uri = Services.io.newURI(bundleURI, "UTF-8", null);
var fileURI = chromeRegistry.convertChromeURL(uri).spec;
var parts = fileURI.split("/");
parts[parts.length - 2] = DEFAULT_LOCALE;
return parts.join("/");
}
// ********************************************************************************************* //
|