lang-tag
Version:
A professional solution for managing translations in modern JavaScript/TypeScript projects, especially those using component-based architectures. `lang-tag` simplifies internationalization by allowing you to define translation keys directly within the com
101 lines (100 loc) • 2.02 kB
JavaScript
function F(e, o, t, n, f) {
const l = {};
for (const [s, r] of Object.entries(t)) {
const c = `${n}${s}`, u = f ? `${f}.${s}` : s;
if (typeof r == "object" && r !== null)
l[s] = F(
e,
o,
r,
`${c}.`,
u
);
else if (typeof r == "string") {
const a = (p, i, y, h) => (b) => o.transform({
config: e,
parentPath: n,
path: p,
unprefixedPath: i,
key: y,
value: h,
params: b
});
if (o.processKey) {
const p = {
config: e,
parentPath: n,
path: c,
unprefixedPath: u,
key: s,
value: r
};
o.processKey(
p,
(i, y) => {
const h = n + i, b = f ? `${f}.${i}` : i;
l[i] = a(
h,
b,
i,
y
);
}
), l.hasOwnProperty(s) || (l[s] = a(
c,
u,
s,
r
));
} else
l[s] = a(
c,
u,
s,
r
);
}
}
return l;
}
function j(e, o, t) {
let n = o?.path || "";
return n && !n.endsWith(".") && (n += "."), F(
o,
t,
e,
n,
""
);
}
function $(e) {
const o = {};
for (const t in e)
if (Object.prototype.hasOwnProperty.call(e, t)) {
const n = e[t];
if (n == null)
continue;
typeof n == "object" && !Array.isArray(n) && !(n instanceof Function) ? o[t] = $(
n
) : typeof n == "string" ? o[t] = ((f) => n) : typeof n == "function" && (o[t] = n);
}
return o;
}
function k(e, o) {
let t = e;
for (const n of o)
if (t && typeof t == "object" && n in t)
t = t[n];
else
return null;
return typeof t == "function" ? t : null;
}
function m(e, o) {
const t = o.split(".");
return k(e, t);
}
export {
j as createCallableTranslations,
m as lookupTranslation,
$ as normalizeTranslations
};