summaryrefslogtreecommitdiff
path: root/src/ollama_web/static/i18n.js
blob: 985b452d496e418b8896e23c87df2d9efda15cce (plain)
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
/** Small i18n helper for the browser.
 *
 * The server injects ``window.I18N`` as a nested message object. This module
 * exposes ``t(key, fallback)`` to resolve dot-separated keys safely.
 */
(function () {
  "use strict";

  function t(key, fallback) {
    if (typeof key !== "string") {
      return fallback !== undefined ? fallback : String(key);
    }
    const parts = key.split(".");
    let current = window.I18N;
    for (const part of parts) {
      if (current == null || typeof current !== "object" || !(part in current)) {
        return fallback !== undefined ? fallback : key;
      }
      current = current[part];
    }
    if (typeof current === "string") {
      return current;
    }
    return fallback !== undefined ? fallback : key;
  }

  window.t = t;
})();