[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/js/dist/ -> i18n.js (source)

   1  (function() {
   2  "use strict";
   3  var wp;
   4  (wp ||= {}).i18n = (() => {
   5    var __create = Object.create;
   6    var __defProp = Object.defineProperty;
   7    var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
   8    var __getOwnPropNames = Object.getOwnPropertyNames;
   9    var __getProtoOf = Object.getPrototypeOf;
  10    var __hasOwnProp = Object.prototype.hasOwnProperty;
  11    var __commonJS = (cb, mod) => function __require() {
  12      return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
  13    };
  14    var __export = (target, all) => {
  15      for (var name in all)
  16        __defProp(target, name, { get: all[name], enumerable: true });
  17    };
  18    var __copyProps = (to, from, except, desc) => {
  19      if (from && typeof from === "object" || typeof from === "function") {
  20        for (let key of __getOwnPropNames(from))
  21          if (!__hasOwnProp.call(to, key) && key !== except)
  22            __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  23      }
  24      return to;
  25    };
  26    var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  27      // If the importer is in node compatibility mode or this is not an ESM
  28      // file that has been converted to a CommonJS file using a Babel-
  29      // compatible transform (i.e. "__esModule" has not been set), then set
  30      // "default" to the CommonJS "module.exports" for node compatibility.
  31      isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  32      mod
  33    ));
  34    var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  35  
  36    // package-external:@wordpress/hooks
  37    var require_hooks = __commonJS({
  38      "package-external:@wordpress/hooks"(exports, module) {
  39        module.exports = window.wp.hooks;
  40      }
  41    });
  42  
  43    // packages/i18n/build-module/index.mjs
  44    var index_exports = {};
  45    __export(index_exports, {
  46      __: () => __,
  47      _n: () => _n,
  48      _nx: () => _nx,
  49      _x: () => _x,
  50      createI18n: () => createI18n,
  51      defaultI18n: () => default_i18n_default,
  52      getLocaleData: () => getLocaleData,
  53      hasTranslation: () => hasTranslation,
  54      isRTL: () => isRTL,
  55      resetLocaleData: () => resetLocaleData,
  56      setLocaleData: () => setLocaleData,
  57      sprintf: () => sprintf2,
  58      subscribe: () => subscribe
  59    });
  60  
  61    // node_modules/@tannin/sprintf/src/index.js
  62    var PATTERN = /%(((\d+)\$)|(\(([$_a-zA-Z][$_a-zA-Z0-9]*)\)))?[ +0#-]*\d*(\.(\d+|\*))?(ll|[lhqL])?([cduxXefgsp%])/g;
  63    function sprintf(string, ...args) {
  64      var i = 0;
  65      if (Array.isArray(args[0])) {
  66        args = /** @type {import('../types').SprintfArgs<T>[]} */
  67        /** @type {unknown} */
  68        args[0];
  69      }
  70      return string.replace(PATTERN, function() {
  71        var index, name, precision, type, value;
  72        index = arguments[3];
  73        name = arguments[5];
  74        precision = arguments[7];
  75        type = arguments[9];
  76        if (type === "%") {
  77          return "%";
  78        }
  79        if (precision === "*") {
  80          precision = args[i];
  81          i++;
  82        }
  83        if (name === void 0) {
  84          if (index === void 0) {
  85            index = i + 1;
  86          }
  87          i++;
  88          value = args[index - 1];
  89        } else if (args[0] && typeof args[0] === "object" && args[0].hasOwnProperty(name)) {
  90          value = args[0][name];
  91        }
  92        if (type === "f") {
  93          value = parseFloat(value) || 0;
  94        } else if (type === "d") {
  95          value = parseInt(value) || 0;
  96        }
  97        if (precision !== void 0) {
  98          if (type === "f") {
  99            value = value.toFixed(precision);
 100          } else if (type === "s") {
 101            value = value.substr(0, precision);
 102          }
 103        }
 104        return value !== void 0 && value !== null ? value : "";
 105      });
 106    }
 107  
 108    // packages/i18n/build-module/sprintf.mjs
 109    function sprintf2(format, ...args) {
 110      return sprintf(format, ...args);
 111    }
 112  
 113    // node_modules/@tannin/postfix/index.js
 114    var PRECEDENCE;
 115    var OPENERS;
 116    var TERMINATORS;
 117    var PATTERN2;
 118    PRECEDENCE = {
 119      "(": 9,
 120      "!": 8,
 121      "*": 7,
 122      "/": 7,
 123      "%": 7,
 124      "+": 6,
 125      "-": 6,
 126      "<": 5,
 127      "<=": 5,
 128      ">": 5,
 129      ">=": 5,
 130      "==": 4,
 131      "!=": 4,
 132      "&&": 3,
 133      "||": 2,
 134      "?": 1,
 135      "?:": 1
 136    };
 137    OPENERS = ["(", "?"];
 138    TERMINATORS = {
 139      ")": ["("],
 140      ":": ["?", "?:"]
 141    };
 142    PATTERN2 = /<=|>=|==|!=|&&|\|\||\?:|\(|!|\*|\/|%|\+|-|<|>|\?|\)|:/;
 143    function postfix(expression) {
 144      var terms = [], stack = [], match, operator, term, element;
 145      while (match = expression.match(PATTERN2)) {
 146        operator = match[0];
 147        term = expression.substr(0, match.index).trim();
 148        if (term) {
 149          terms.push(term);
 150        }
 151        while (element = stack.pop()) {
 152          if (TERMINATORS[operator]) {
 153            if (TERMINATORS[operator][0] === element) {
 154              operator = TERMINATORS[operator][1] || operator;
 155              break;
 156            }
 157          } else if (OPENERS.indexOf(element) >= 0 || PRECEDENCE[element] < PRECEDENCE[operator]) {
 158            stack.push(element);
 159            break;
 160          }
 161          terms.push(element);
 162        }
 163        if (!TERMINATORS[operator]) {
 164          stack.push(operator);
 165        }
 166        expression = expression.substr(match.index + operator.length);
 167      }
 168      expression = expression.trim();
 169      if (expression) {
 170        terms.push(expression);
 171      }
 172      return terms.concat(stack.reverse());
 173    }
 174  
 175    // node_modules/@tannin/evaluate/index.js
 176    var OPERATORS = {
 177      "!": function(a) {
 178        return !a;
 179      },
 180      "*": function(a, b) {
 181        return a * b;
 182      },
 183      "/": function(a, b) {
 184        return a / b;
 185      },
 186      "%": function(a, b) {
 187        return a % b;
 188      },
 189      "+": function(a, b) {
 190        return a + b;
 191      },
 192      "-": function(a, b) {
 193        return a - b;
 194      },
 195      "<": function(a, b) {
 196        return a < b;
 197      },
 198      "<=": function(a, b) {
 199        return a <= b;
 200      },
 201      ">": function(a, b) {
 202        return a > b;
 203      },
 204      ">=": function(a, b) {
 205        return a >= b;
 206      },
 207      "==": function(a, b) {
 208        return a === b;
 209      },
 210      "!=": function(a, b) {
 211        return a !== b;
 212      },
 213      "&&": function(a, b) {
 214        return a && b;
 215      },
 216      "||": function(a, b) {
 217        return a || b;
 218      },
 219      "?:": function(a, b, c) {
 220        if (a) {
 221          throw b;
 222        }
 223        return c;
 224      }
 225    };
 226    function evaluate(postfix2, variables) {
 227      var stack = [], i, j, args, getOperatorResult, term, value;
 228      for (i = 0; i < postfix2.length; i++) {
 229        term = postfix2[i];
 230        getOperatorResult = OPERATORS[term];
 231        if (getOperatorResult) {
 232          j = getOperatorResult.length;
 233          args = Array(j);
 234          while (j--) {
 235            args[j] = stack.pop();
 236          }
 237          try {
 238            value = getOperatorResult.apply(null, args);
 239          } catch (earlyReturn) {
 240            return earlyReturn;
 241          }
 242        } else if (variables.hasOwnProperty(term)) {
 243          value = variables[term];
 244        } else {
 245          value = +term;
 246        }
 247        stack.push(value);
 248      }
 249      return stack[0];
 250    }
 251  
 252    // node_modules/@tannin/compile/index.js
 253    function compile(expression) {
 254      var terms = postfix(expression);
 255      return function(variables) {
 256        return evaluate(terms, variables);
 257      };
 258    }
 259  
 260    // node_modules/@tannin/plural-forms/index.js
 261    function pluralForms(expression) {
 262      var evaluate2 = compile(expression);
 263      return function(n) {
 264        return +evaluate2({ n });
 265      };
 266    }
 267  
 268    // node_modules/tannin/index.js
 269    var DEFAULT_OPTIONS = {
 270      contextDelimiter: "",
 271      onMissingKey: null
 272    };
 273    function getPluralExpression(pf) {
 274      var parts, i, part;
 275      parts = pf.split(";");
 276      for (i = 0; i < parts.length; i++) {
 277        part = parts[i].trim();
 278        if (part.indexOf("plural=") === 0) {
 279          return part.substr(7);
 280        }
 281      }
 282    }
 283    function Tannin(data, options) {
 284      var key;
 285      this.data = data;
 286      this.pluralForms = {};
 287      this.options = {};
 288      for (key in DEFAULT_OPTIONS) {
 289        this.options[key] = options !== void 0 && key in options ? options[key] : DEFAULT_OPTIONS[key];
 290      }
 291    }
 292    Tannin.prototype.getPluralForm = function(domain, n) {
 293      var getPluralForm = this.pluralForms[domain], config, plural, pf;
 294      if (!getPluralForm) {
 295        config = this.data[domain][""];
 296        pf = config["Plural-Forms"] || config["plural-forms"] || // Ignore reason: As known, there's no way to document the empty
 297        // string property on a key to guarantee this as metadata.
 298        // @ts-ignore
 299        config.plural_forms;
 300        if (typeof pf !== "function") {
 301          plural = getPluralExpression(
 302            config["Plural-Forms"] || config["plural-forms"] || // Ignore reason: As known, there's no way to document the empty
 303            // string property on a key to guarantee this as metadata.
 304            // @ts-ignore
 305            config.plural_forms
 306          );
 307          pf = pluralForms(plural);
 308        }
 309        getPluralForm = this.pluralForms[domain] = pf;
 310      }
 311      return getPluralForm(n);
 312    };
 313    Tannin.prototype.dcnpgettext = function(domain, context, singular, plural, n) {
 314      var index, key, entry;
 315      if (n === void 0) {
 316        index = 0;
 317      } else {
 318        index = this.getPluralForm(domain, n);
 319      }
 320      key = singular;
 321      if (context) {
 322        key = context + this.options.contextDelimiter + singular;
 323      }
 324      entry = this.data[domain][key];
 325      if (entry && entry[index]) {
 326        return entry[index];
 327      }
 328      if (this.options.onMissingKey) {
 329        this.options.onMissingKey(singular, domain);
 330      }
 331      return index === 0 ? singular : plural;
 332    };
 333  
 334    // packages/i18n/build-module/create-i18n.mjs
 335    var DEFAULT_LOCALE_DATA = {
 336      "": {
 337        plural_forms(n) {
 338          return n === 1 ? 0 : 1;
 339        }
 340      }
 341    };
 342    var I18N_HOOK_REGEXP = /^i18n\.(n?gettext|has_translation)(_|$)/;
 343    var createI18n = (initialData, initialDomain, hooks) => {
 344      const tannin = new Tannin({});
 345      const listeners = /* @__PURE__ */ new Set();
 346      const notifyListeners = () => {
 347        listeners.forEach((listener) => listener());
 348      };
 349      const subscribe2 = (callback) => {
 350        listeners.add(callback);
 351        return () => listeners.delete(callback);
 352      };
 353      const getLocaleData2 = (domain = "default") => tannin.data[domain];
 354      const doSetLocaleData = (data, domain = "default") => {
 355        tannin.data[domain] = {
 356          ...tannin.data[domain],
 357          ...data
 358        };
 359        tannin.data[domain][""] = {
 360          ...DEFAULT_LOCALE_DATA[""],
 361          ...tannin.data[domain]?.[""]
 362        };
 363        delete tannin.pluralForms[domain];
 364      };
 365      const setLocaleData2 = (data, domain) => {
 366        doSetLocaleData(data, domain);
 367        notifyListeners();
 368      };
 369      const addLocaleData = (data, domain = "default") => {
 370        tannin.data[domain] = {
 371          ...tannin.data[domain],
 372          ...data,
 373          // Populate default domain configuration (supported locale date which omits
 374          // a plural forms expression).
 375          "": {
 376            ...DEFAULT_LOCALE_DATA[""],
 377            ...tannin.data[domain]?.[""],
 378            ...data?.[""]
 379          }
 380        };
 381        delete tannin.pluralForms[domain];
 382        notifyListeners();
 383      };
 384      const resetLocaleData2 = (data, domain) => {
 385        tannin.data = {};
 386        tannin.pluralForms = {};
 387        setLocaleData2(data, domain);
 388      };
 389      const dcnpgettext = (domain = "default", context, single, plural, number) => {
 390        if (!tannin.data[domain]) {
 391          doSetLocaleData(void 0, domain);
 392        }
 393        return tannin.dcnpgettext(domain, context, single, plural, number);
 394      };
 395      const getFilterDomain = (domain) => domain || "default";
 396      const __2 = (text, domain) => {
 397        let translation = dcnpgettext(domain, void 0, text);
 398        if (!hooks) {
 399          return translation;
 400        }
 401        translation = hooks.applyFilters(
 402          "i18n.gettext",
 403          translation,
 404          text,
 405          domain
 406        );
 407        return hooks.applyFilters(
 408          "i18n.gettext_" + getFilterDomain(domain),
 409          translation,
 410          text,
 411          domain
 412        );
 413      };
 414      const _x2 = (text, context, domain) => {
 415        let translation = dcnpgettext(domain, context, text);
 416        if (!hooks) {
 417          return translation;
 418        }
 419        translation = hooks.applyFilters(
 420          "i18n.gettext_with_context",
 421          translation,
 422          text,
 423          context,
 424          domain
 425        );
 426        return hooks.applyFilters(
 427          "i18n.gettext_with_context_" + getFilterDomain(domain),
 428          translation,
 429          text,
 430          context,
 431          domain
 432        );
 433      };
 434      const _n2 = (single, plural, number, domain) => {
 435        let translation = dcnpgettext(
 436          domain,
 437          void 0,
 438          single,
 439          plural,
 440          number
 441        );
 442        if (!hooks) {
 443          return translation;
 444        }
 445        translation = hooks.applyFilters(
 446          "i18n.ngettext",
 447          translation,
 448          single,
 449          plural,
 450          number,
 451          domain
 452        );
 453        return hooks.applyFilters(
 454          "i18n.ngettext_" + getFilterDomain(domain),
 455          translation,
 456          single,
 457          plural,
 458          number,
 459          domain
 460        );
 461      };
 462      const _nx2 = (single, plural, number, context, domain) => {
 463        let translation = dcnpgettext(
 464          domain,
 465          context,
 466          single,
 467          plural,
 468          number
 469        );
 470        if (!hooks) {
 471          return translation;
 472        }
 473        translation = hooks.applyFilters(
 474          "i18n.ngettext_with_context",
 475          translation,
 476          single,
 477          plural,
 478          number,
 479          context,
 480          domain
 481        );
 482        return hooks.applyFilters(
 483          "i18n.ngettext_with_context_" + getFilterDomain(domain),
 484          translation,
 485          single,
 486          plural,
 487          number,
 488          context,
 489          domain
 490        );
 491      };
 492      const isRTL2 = () => {
 493        return "rtl" === _x2("ltr", "text direction");
 494      };
 495      const hasTranslation2 = (single, context, domain) => {
 496        const key = context ? context + "" + single : single;
 497        let result = !!tannin.data?.[domain ?? "default"]?.[key];
 498        if (hooks) {
 499          result = hooks.applyFilters(
 500            "i18n.has_translation",
 501            result,
 502            single,
 503            context,
 504            domain
 505          );
 506          result = hooks.applyFilters(
 507            "i18n.has_translation_" + getFilterDomain(domain),
 508            result,
 509            single,
 510            context,
 511            domain
 512          );
 513        }
 514        return result;
 515      };
 516      if (initialData) {
 517        setLocaleData2(initialData, initialDomain);
 518      }
 519      if (hooks) {
 520        const onHookAddedOrRemoved = (hookName) => {
 521          if (I18N_HOOK_REGEXP.test(hookName)) {
 522            notifyListeners();
 523          }
 524        };
 525        hooks.addAction("hookAdded", "core/i18n", onHookAddedOrRemoved);
 526        hooks.addAction("hookRemoved", "core/i18n", onHookAddedOrRemoved);
 527      }
 528      return {
 529        getLocaleData: getLocaleData2,
 530        setLocaleData: setLocaleData2,
 531        addLocaleData,
 532        resetLocaleData: resetLocaleData2,
 533        subscribe: subscribe2,
 534        __: __2,
 535        _x: _x2,
 536        _n: _n2,
 537        _nx: _nx2,
 538        isRTL: isRTL2,
 539        hasTranslation: hasTranslation2
 540      };
 541    };
 542  
 543    // packages/i18n/build-module/default-i18n.mjs
 544    var import_hooks = __toESM(require_hooks(), 1);
 545    var i18n = createI18n(void 0, void 0, import_hooks.defaultHooks);
 546    var default_i18n_default = i18n;
 547    var getLocaleData = i18n.getLocaleData.bind(i18n);
 548    var setLocaleData = i18n.setLocaleData.bind(i18n);
 549    var resetLocaleData = i18n.resetLocaleData.bind(i18n);
 550    var subscribe = i18n.subscribe.bind(i18n);
 551    var __ = i18n.__.bind(i18n);
 552    var _x = i18n._x.bind(i18n);
 553    var _n = i18n._n.bind(i18n);
 554    var _nx = i18n._nx.bind(i18n);
 555    var isRTL = i18n.isRTL.bind(i18n);
 556    var hasTranslation = i18n.hasTranslation.bind(i18n);
 557    return __toCommonJS(index_exports);
 558  })();
 559  (window.wp ||= {}).i18n = wp.i18n;
 560  })();


Generated : Tue Aug 18 08:20:25 2026 Cross-referenced by PHPXref