addon-sdk/source/lib/sdk/l10n.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/l10n.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,86 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +"use strict";
     1.8 +
     1.9 +module.metadata = {
    1.10 +  "stability": "stable"
    1.11 +};
    1.12 +
    1.13 +const json = require("./l10n/json/core");
    1.14 +const { get: getKey } = require("./l10n/core");
    1.15 +const properties = require("./l10n/properties/core");
    1.16 +const { getRulesForLocale } = require("./l10n/plural-rules");
    1.17 +
    1.18 +// Retrieve the plural mapping function
    1.19 +let pluralMappingFunction = getRulesForLocale(json.language()) ||
    1.20 +                            getRulesForLocale("en");
    1.21 +
    1.22 +exports.get = function get(k) {
    1.23 +  // For now, we only accept a "string" as first argument
    1.24 +  // TODO: handle plural forms in gettext pattern
    1.25 +  if (typeof k !== "string")
    1.26 +    throw new Error("First argument of localization method should be a string");
    1.27 +  let n = arguments[1];
    1.28 +
    1.29 +  // Get translation from big hashmap or default to hard coded string:
    1.30 +  let localized = getKey(k, n) || k;
    1.31 +
    1.32 +  // # Simplest usecase:
    1.33 +  //   // String hard coded in source code:
    1.34 +  //   _("Hello world")
    1.35 +  //   // Identifier of a key stored in properties file
    1.36 +  //   _("helloString")
    1.37 +  if (arguments.length <= 1)
    1.38 +    return localized;
    1.39 +
    1.40 +  let args = arguments;
    1.41 +
    1.42 +  if (typeof localized == "object" && "other" in localized) {
    1.43 +    // # Plural form:
    1.44 +    //   // Strings hard coded in source code:
    1.45 +    //   _(["One download", "%d downloads"], 10);
    1.46 +    //   // Identifier of a key stored in properties file
    1.47 +    //   _("downloadNumber", 0);
    1.48 +    let n = arguments[1];
    1.49 +
    1.50 +    // First handle simple universal forms that may not be mandatory
    1.51 +    // for each language, (i.e. not different than 'other' form,
    1.52 +    // but still usefull for better phrasing)
    1.53 +    // For example 0 in english is the same form than 'other'
    1.54 +    // but we accept 'zero' form if specified in localization file
    1.55 +    if (n === 0 && "zero" in localized)
    1.56 +      localized = localized["zero"];
    1.57 +    else if (n === 1 && "one" in localized)
    1.58 +      localized = localized["one"];
    1.59 +    else if (n === 2 && "two" in localized)
    1.60 +      localized = localized["two"];
    1.61 +    else {
    1.62 +      let pluralForm = pluralMappingFunction(n);
    1.63 +      if (pluralForm in localized)
    1.64 +        localized = localized[pluralForm];
    1.65 +      else // Fallback in case of error: missing plural form
    1.66 +        localized = localized["other"];
    1.67 +    }
    1.68 +
    1.69 +    // Simulate a string with one placeholder:
    1.70 +    args = [null, n];
    1.71 +  }
    1.72 +
    1.73 +  // # String with placeholders:
    1.74 +  //   // Strings hard coded in source code:
    1.75 +  //   _("Hello %s", username)
    1.76 +  //   // Identifier of a key stored in properties file
    1.77 +  //   _("helloString", username)
    1.78 +  // * We supports `%1s`, `%2s`, ... pattern in order to change arguments order
    1.79 +  // in translation.
    1.80 +  // * In case of plural form, we has `%d` instead of `%s`.
    1.81 +  let offset = 1;
    1.82 +  localized = localized.replace(/%(\d*)(s|d)/g, function (v, n) {
    1.83 +      let rv = args[n != "" ? n : offset];
    1.84 +      offset++;
    1.85 +      return rv;
    1.86 +    });
    1.87 +
    1.88 +  return localized;
    1.89 +}

mercurial