From b8d6310bfd7af2cece81c0b7e7fc71711a991871 Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Tue, 19 May 2015 13:49:57 -0700 Subject: Expose ngettext() to client-side JS This exposes the function set by webhelper.set_ngettext() to the client- side Javascript as a ngettext() function, defined on the global window object. This allows apps to translate messages that need to be separated into singular and plural, just like the C ngettext() function. [endlessm/eos-sdk#291] --- webhelper/webhelper2.js | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'webhelper/webhelper2.js') diff --git a/webhelper/webhelper2.js b/webhelper/webhelper2.js index bba4c63..b5f87f8 100644 --- a/webhelper/webhelper2.js +++ b/webhelper/webhelper2.js @@ -29,6 +29,12 @@ const WH2_DBUS_MAIN_PROGRAM_INTERFACE = '\ \ \ \ + \ + \ + \ + \ + \ + \ \ '; @@ -164,6 +170,7 @@ const WebHelper = new Lang.Class({ _init: function (props={}) { this._web_actions = {}; this._gettext = null; + this._ngettext = null; this._ProxyConstructor = Gio.DBusProxy.makeProxyWrapper(WH2_DBUS_EXTENSION_INTERFACE); this.parent(props); @@ -239,6 +246,10 @@ const WebHelper = new Lang.Class({ return this._gettext(string); }, + NGettext: function (singular, plural, number) { + return this._ngettext(singular, plural, number); + }, + // Public API /** @@ -289,6 +300,56 @@ const WebHelper = new Lang.Class({ return this._gettext; }, + /** + * Method: set_ngettext + * Define function which translates singular/plural text + * + * Parameters: + * ngettext_func - a function, or null + * + * When you plan to translate text in your web application, set this + * property to the translation function. + * The function must take three parameters: a string singular message, a + * string plural message, and a number for which to generate the message. + * The function must return a string. + * The canonical example is ngettext(). + * + * This function is made available directly to the browser-side Javascript + * as *ngettext()*, a property of the global object. + * + * Pass null for _ngettext_func_ to unset the translation function. + * + * If this function has not been called, or has most recently been called + * with null, then it is illegal to call *ngettext()* in the client-side + * Javascript. + * + * Example: + * > const WebHelper2 = imports.webhelper2; + * > const Gettext = imports.gettext; + * > const GETTEXT_DOMAIN = 'smoke-grinder'; + * > + * > let webhelper = new WebHelper2.WebHelper('com.example.SmokeGrinder'); + * > webhelper.set_gettext(Gettext.dngettext.bind(null, GETTEXT_DOMAIN)); + */ + set_ngettext: function (ngettext_func) { + if (ngettext_func !== null && typeof ngettext_func !== 'function') + throw new Error('The translation function must be a function, or ' + + 'null.'); + this._ngettext = ngettext_func; + }, + + /** + * Method: get_ngettext + * Retrieve the currently set singular/plural translation function + * + * Returns: + * the translation function previously set with , or null + * if none is currently set. + */ + get_ngettext: function () { + return this._ngettext; + }, + /** * Method: translate_html * Translate the HTML page in a webview asynchronously -- cgit v1.2.3