JSFiddle - React, Tailwind, and code Playground
by rajnandan1
JavaScript
const l = function (sessionLangMap, key, ...args) {
const keys = key.split(".");
let obj = sessionLangMap;
for (const key of keys) {
obj = obj?.[key];
if (!obj) {
break;
}
}
// Replace placeholders in the string if arguments are provided
if (obj && args.length > 0) {
obj = obj.replace(/%[a-z]/g, () => args.shift());
}
return obj || key;
};
console.log(l({ a: "aa" }, "a", 2, 4)); // Output: "aa 2"