import template from "lodash/template";
import en from "./en.js";
import zh from "./zh.js";
export const translations = {
  zh,
  en
};
const replace = (t2, data = {}) => {
  try {
    const compiled = template(t2, { interpolate: /{([\s\S]+?)}/g, escape: /{([\s\S]+?)}/g });
    return compiled(data);
  } catch (e) {
    console.error(`Error evaluating template: ${t2}`, e);
    return "";
  }
};
export const createTranslator = ({ fallbackLocale = "en" }, langs = translations) => {
  return (key, locale = fallbackLocale, data = {}) => {
    if (!langs[locale] || !langs[locale][key]) {
      if (fallbackLocale && langs[fallbackLocale]?.[key]) {
        return replace(langs[fallbackLocale]?.[key], data);
      }
      return key;
    }
    return replace(langs[locale][key], data);
  };
};
export const translate = createTranslator({ fallbackLocale: "en" });
export const t = translate;
