import ht from "./../../lang/ht.json";
import pt from "./../../lang/pt.json";
export function getLocalizedMessages(locale) {
    console.log('localized import json');
    return {
        ht,
        pt
    }[locale];
}

export function getUserLocale() {
    return getLocalizedMessages(window.localStorage.getItem('locale') ?? 'pt');
}

const accessObject = (path, object) => {
  return path.split('.').reduce((o, i) => o[i], object);
}

export function translate(message) {
    if (message.match(/^\$/)) {
        const result = accessObject(message.substr(1), getUserLocale());
        if(result) {
            return result;
        } else {
            console.error(`path ${message} not found on lang`);
        }
    }
    return message;
}
