Labs/JS Modules: Difference between revisions

Jump to navigation Jump to search
describe the new StringBundle module
(describe the new StringBundle module)
Line 114: Line 114:


  testBranch.set({ foo: 1, bar: "awesome", baz: true });
  testBranch.set({ foo: 1, bar: "awesome", baz: true });
== StringBundle ==
The [http://hg.mozdev.org/jsmodules/file/tip/StringBundle.js StringBundle module] makes it easier to access string bundles from JS modules and XPCOM components.  To use the module, import it, create a new instance of StringBundle, and then use the instance's <code>get</code> and <code>getAll</code> methods to retrieve strings (you can get both plain and formatted strings with <code>get</code>):
let strings = new StringBundle("chrome://example/locale/strings.properties");
let foo = strings.get("foo");
let barFormatted = strings.get("bar", [arg1, arg2]);
for each (let string in strings.getAll())
  dump (string.key + " = " + string.value + "\n");
Note: in addition to the API described above, the StringBundle object supports the API for the <code>stringbundle</code> XBL binding to make it easier to switch from the binding to the module:
<strike>let strings = document.getElementById("myStringBundleElement");</strike>
let strings = new StringBundle("chrome://example/locale/strings.properties");
let foo = strings.getString("foo");
let barFormatted = strings.getFormattedString("bar", [arg1, arg2]);
let enumerator = strings.strings;
while (enumerator.hasMoreElements()) {
  let string = enumerator.getNext().QueryInterface(Ci.nsIPropertyElement);
  dump (string.key + " = " + string.value + "\n");
}


== URI ==
== URI ==
canmove, Confirmed users
2,056

edits

Navigation menu