/* eslint-disable import/prefer-default-export */
import en from './en';
import zh from './zh';

const locales = { en, zh };

const replace = (template, data) =>
  template.replace(/{(\w*)}/g, (_, key) => (Object.prototype.hasOwnProperty.call(data, key) ? data[key] : ''));

export function t(key, locale = 'en', params = {}) {
  const _locale = locale in locales ? locale : 'en';
  return replace(locales[_locale][key], params) || key;
}
