Viewing File: /home/ubuntu/voice-assistant-frontend/node_modules/react-multi-lang/lib/index.js

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.withTranslation = exports.useTranslation = exports.t = exports.getLanguage = exports.setLanguage = exports.setTranslations = exports.setDefaultTranslations = exports.setDefaultLanguage = exports.unsubscribe = exports.subscribe = void 0;
const hoist_non_react_statics_1 = __importDefault(require("hoist-non-react-statics"));
const react_1 = __importStar(require("react"));
const useForceUpdate = () => {
    const setState = (0, react_1.useState)(true)[1];
    return () => setState((n) => !n);
};
let language = 'pt';
let id = 1;
const subscribes = {};
let translations = {};
function subscribe(cb) {
    const newId = id;
    subscribes[newId] = cb;
    id += 1;
    return newId;
}
exports.subscribe = subscribe;
function unsubscribe(componentId) {
    delete subscribes[componentId];
}
exports.unsubscribe = unsubscribe;
function triggerSubscriptions() {
    Object.keys(subscribes).forEach((componentId) => {
        new Promise((resolve, reject) => {
            if (subscribes[componentId] && typeof subscribes[componentId] === 'function') {
                subscribes[componentId]();
            }
            resolve(componentId);
        }).then();
    });
}
function setDefaultLanguage(lang) {
    language = lang;
}
exports.setDefaultLanguage = setDefaultLanguage;
function setDefaultTranslations(userTranslations) {
    if (Object.keys(translations).length !== 0) {
        setTranslations(userTranslations);
        return;
    }
    translations = userTranslations;
}
exports.setDefaultTranslations = setDefaultTranslations;
function setTranslations(userTranslations) {
    translations = userTranslations;
    triggerSubscriptions();
}
exports.setTranslations = setTranslations;
function setLanguage(lang) {
    language = lang;
    triggerSubscriptions();
}
exports.setLanguage = setLanguage;
function getLanguage() {
    return language;
}
exports.getLanguage = getLanguage;
function t(path, args) {
    const translationKeys = path.split('.');
    let translation = '';
    if (translations[language]) {
        let translationObj = translations[language];
        translationKeys.forEach((key) => {
            const temp = translationObj[key];
            if (typeof translationObj[key] === 'object') {
                translationObj = translationObj[key];
            }
            if (typeof temp === 'string') {
                translation = temp;
            }
        });
        if (translation) {
            if (args) {
                Object.keys(args).forEach((key) => {
                    translation = translation.replace(`{${key}}`, args ? args[key] : '');
                });
            }
            return translation;
        }
    }
    return path;
}
exports.t = t;
function useTranslation(basePath) {
    const forceUpdate = useForceUpdate();
    (0, react_1.useEffect)(() => {
        const subId = subscribe(() => forceUpdate());
        return () => unsubscribe(subId);
    }, [forceUpdate]);
    return (path, args) => t(basePath ? (basePath + '.' + path) : path, args);
}
exports.useTranslation = useTranslation;
function withTranslation(Component, basePath) {
    class TranslatedComponent extends react_1.default.Component {
        componentDidMount() {
            this.id = subscribe(() => this.forceUpdate());
        }
        componentWillUnmount() {
            if (this.id) {
                unsubscribe(this.id);
            }
        }
        render() {
            return react_1.default.createElement(Component, Object.assign({}, this.props, { t: (path, args) => t(basePath ? (basePath + '.' + path) : path, args) }));
        }
    }
    return (0, hoist_non_react_statics_1.default)(TranslatedComponent, Component);
}
exports.withTranslation = withTranslation;
exports.default = {
    setDefaultLanguage,
    setLanguage,
    setDefaultTranslations,
    setTranslations,
    withTranslation,
    useTranslation,
    subscribe,
    unsubscribe,
    t,
};
Back to Directory File Manager