@localazy/strapi-plugin
Version:
The official Strapi Plugin by Localazy.
189 lines (188 loc) • 6.88 kB
JavaScript
;
const React = require("react");
const initReactI18next = require("./initReactI18next-B0H8_DlW.js");
const warn = (i18n, code, msg, rest) => {
const args = [msg, {
code,
...rest || {}
}];
if (i18n?.services?.logger?.forward) {
return i18n.services.logger.forward(args, "warn", "react-i18next::", true);
}
if (isString(args[0])) args[0] = `react-i18next:: ${args[0]}`;
if (i18n?.services?.logger?.warn) {
i18n.services.logger.warn(...args);
} else if (console?.warn) {
console.warn(...args);
}
};
const alreadyWarned = {};
const warnOnce = (i18n, code, msg, rest) => {
if (isString(msg) && alreadyWarned[msg]) return;
if (isString(msg)) alreadyWarned[msg] = /* @__PURE__ */ new Date();
warn(i18n, code, msg, rest);
};
const loadedClb = (i18n, cb) => () => {
if (i18n.isInitialized) {
cb();
} else {
const initialized = () => {
setTimeout(() => {
i18n.off("initialized", initialized);
}, 0);
cb();
};
i18n.on("initialized", initialized);
}
};
const loadNamespaces = (i18n, ns, cb) => {
i18n.loadNamespaces(ns, loadedClb(i18n, cb));
};
const loadLanguages = (i18n, lng, ns, cb) => {
if (isString(ns)) ns = [ns];
if (i18n.options.preload && i18n.options.preload.indexOf(lng) > -1) return loadNamespaces(i18n, ns, cb);
ns.forEach((n) => {
if (i18n.options.ns.indexOf(n) < 0) i18n.options.ns.push(n);
});
i18n.loadLanguages(lng, loadedClb(i18n, cb));
};
const hasLoadedNamespace = (ns, i18n, options = {}) => {
if (!i18n.languages || !i18n.languages.length) {
warnOnce(i18n, "NO_LANGUAGES", "i18n.languages were undefined or empty", {
languages: i18n.languages
});
return true;
}
return i18n.hasLoadedNamespace(ns, {
lng: options.lng,
precheck: (i18nInstance, loadNotPending) => {
if (options.bindI18n?.indexOf("languageChanging") > -1 && i18nInstance.services.backendConnector.backend && i18nInstance.isLanguageChangingTo && !loadNotPending(i18nInstance.isLanguageChangingTo, ns)) return false;
}
});
};
const isString = (obj) => typeof obj === "string";
const isObject = (obj) => typeof obj === "object" && obj !== null;
const I18nContext = React.createContext();
class ReportNamespaces {
constructor() {
this.usedNamespaces = {};
}
addUsedNamespaces(namespaces) {
namespaces.forEach((ns) => {
if (!this.usedNamespaces[ns]) this.usedNamespaces[ns] = true;
});
}
getUsedNamespaces() {
return Object.keys(this.usedNamespaces);
}
}
const usePrevious = (value, ignore) => {
const ref = React.useRef();
React.useEffect(() => {
ref.current = value;
}, [value, ignore]);
return ref.current;
};
const alwaysNewT = (i18n, language, namespace, keyPrefix) => i18n.getFixedT(language, namespace, keyPrefix);
const useMemoizedT = (i18n, language, namespace, keyPrefix) => React.useCallback(alwaysNewT(i18n, language, namespace, keyPrefix), [i18n, language, namespace, keyPrefix]);
const useTranslation = (ns, props = {}) => {
const {
i18n: i18nFromProps
} = props;
const {
i18n: i18nFromContext,
defaultNS: defaultNSFromContext
} = React.useContext(I18nContext) || {};
const i18n = i18nFromProps || i18nFromContext || initReactI18next.getI18n();
if (i18n && !i18n.reportNamespaces) i18n.reportNamespaces = new ReportNamespaces();
if (!i18n) {
warnOnce(i18n, "NO_I18NEXT_INSTANCE", "useTranslation: You will need to pass in an i18next instance by using initReactI18next");
const notReadyT = (k, optsOrDefaultValue) => {
if (isString(optsOrDefaultValue)) return optsOrDefaultValue;
if (isObject(optsOrDefaultValue) && isString(optsOrDefaultValue.defaultValue)) return optsOrDefaultValue.defaultValue;
return Array.isArray(k) ? k[k.length - 1] : k;
};
const retNotReady = [notReadyT, {}, false];
retNotReady.t = notReadyT;
retNotReady.i18n = {};
retNotReady.ready = false;
return retNotReady;
}
if (i18n.options.react?.wait) warnOnce(i18n, "DEPRECATED_OPTION", "useTranslation: It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.");
const i18nOptions = {
...initReactI18next.getDefaults(),
...i18n.options.react,
...props
};
const {
useSuspense,
keyPrefix
} = i18nOptions;
let namespaces = ns || defaultNSFromContext || i18n.options?.defaultNS;
namespaces = isString(namespaces) ? [namespaces] : namespaces || ["translation"];
i18n.reportNamespaces.addUsedNamespaces?.(namespaces);
const ready = (i18n.isInitialized || i18n.initializedStoreOnce) && namespaces.every((n) => hasLoadedNamespace(n, i18n, i18nOptions));
const memoGetT = useMemoizedT(i18n, props.lng || null, i18nOptions.nsMode === "fallback" ? namespaces : namespaces[0], keyPrefix);
const getT = () => memoGetT;
const getNewT = () => alwaysNewT(i18n, props.lng || null, i18nOptions.nsMode === "fallback" ? namespaces : namespaces[0], keyPrefix);
const [t, setT] = React.useState(getT);
let joinedNS = namespaces.join();
if (props.lng) joinedNS = `${props.lng}${joinedNS}`;
const previousJoinedNS = usePrevious(joinedNS);
const isMounted = React.useRef(true);
React.useEffect(() => {
const {
bindI18n,
bindI18nStore
} = i18nOptions;
isMounted.current = true;
if (!ready && !useSuspense) {
if (props.lng) {
loadLanguages(i18n, props.lng, namespaces, () => {
if (isMounted.current) setT(getNewT);
});
} else {
loadNamespaces(i18n, namespaces, () => {
if (isMounted.current) setT(getNewT);
});
}
}
if (ready && previousJoinedNS && previousJoinedNS !== joinedNS && isMounted.current) {
setT(getNewT);
}
const boundReset = () => {
if (isMounted.current) setT(getNewT);
};
if (bindI18n) i18n?.on(bindI18n, boundReset);
if (bindI18nStore) i18n?.store.on(bindI18nStore, boundReset);
return () => {
isMounted.current = false;
if (i18n) bindI18n?.split(" ").forEach((e) => i18n.off(e, boundReset));
if (bindI18nStore && i18n) bindI18nStore.split(" ").forEach((e) => i18n.store.off(e, boundReset));
};
}, [i18n, joinedNS]);
React.useEffect(() => {
if (isMounted.current && ready) {
setT(getT);
}
}, [i18n, keyPrefix, ready]);
const ret = [t, i18n, ready];
ret.t = t;
ret.i18n = i18n;
ret.ready = ready;
if (ready) return ret;
if (!ready && !useSuspense) return ret;
throw new Promise((resolve) => {
if (props.lng) {
loadLanguages(i18n, props.lng, namespaces, () => resolve());
} else {
loadNamespaces(i18n, namespaces, () => resolve());
}
});
};
exports.I18nContext = I18nContext;
exports.isObject = isObject;
exports.isString = isString;
exports.useTranslation = useTranslation;
exports.warn = warn;
exports.warnOnce = warnOnce;