Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// empty
}
const pathPrefix = withPrefix('/').replace(/\/$/, '');
const path = location.pathname.replace(pathPrefix, '');
const currentLangKey = getCurrentLangKey(lngs, 'zh', path);
const isHomePage =
path === '/' ||
path === `/${currentLangKey}` ||
path === `/${currentLangKey}/`;
i18n.init({
lng: currentLangKey,
});
if (!i18n.options.resources) {
i18n.init({
resources,
});
}
if (
location.pathname === pathPrefix ||
(children && children.type && (children as any).type.noLayout)
) {
return children;
}
const getRediectUrl = () => {
const list = redirects || [];
for (let i = 0; i < list.length; i += 1) {
const { from = '', to, keepUrl } = list[i] as {
node,
tOptions,
parent,
parentOverrideKey,
currentDepth = 0,
opts
) {
const nodeIsNotExcluded = isNotExcluded(node);
const nodeIsUnTranslated = isUnTranslated(node, opts);
const realNodeIsUnTranslated = isUnTranslated(node); // ignoring forced threatment
tOptions = getTOptions(tOptions, node);
let parentKey = currentDepth === 0 ? parentOverrideKey : '';
if (
currentDepth > 0 &&
parentOverrideKey &&
!i18next.options.ignoreWithoutKey
) {
parentKey = `${parentOverrideKey}.${currentDepth}`;
}
const overrideKey = getAttribute(node, i18next.options.keyAttr) || parentKey; // normally we use content as key, but optionally we allow to override it
// translate node as one block
const mergeFlag = getAttribute(node, 'merge');
if (
mergeFlag !== 'false' &&
(mergeFlag === '' || canInline(node, tOptions))
) {
if (nodeIsNotExcluded && nodeIsUnTranslated) {
// wrap children into dummy node and remove that outer from translation
const dummyNode = new VNode('I18NEXTIFYDUMMY', null, node.children);
let key = removeIndent(toHTML(dummyNode), '')
.replace('', '')
i18n.getInitialProps = (req, namespaces) => {
if (!namespaces) namespaces = i18n.options.defaultNS
if (typeof namespaces === 'string') namespaces = [namespaces]
req.i18n.toJSON = () => null // do not serialize i18next instance and send to client
const initialI18nStore = {}
req.i18n.languages.forEach(l => {
initialI18nStore[l] = {}
namespaces.forEach(ns => {
initialI18nStore[l][ns] =
(req.i18n.services.resourceStore.data[l] || {})[ns] || {}
})
})
return {
i18n: req.i18n, // use the instance on req - fixed language on request (avoid issues in race conditions with lngs of different users)
initialI18nStore,
i18n.getInitialProps = (req, namespaces) => {
if (!namespaces) namespaces = i18n.options.defaultNS
if (typeof namespaces === 'string') namespaces = [namespaces]
req.i18n.toJSON = () => {} // do not serialize i18next instance to prevent circular references on the client
const initialI18nStore = {}
req.i18n.languages.forEach((l) => {
initialI18nStore[l] = {}
namespaces.forEach((ns) => {
initialI18nStore[l][ns] = (req.i18n.services.resourceStore.data[l] || {})[ns] || {}
})
})
return {
i18n: req.i18n, // use the instance on req - fixed language on request (avoid issues in race conditions with lngs of different users)
initialI18nStore,
initialLanguage: req.i18n.language
i18n.getInitialProps = (req, namespaces) => {
if (!namespaces) { namespaces = i18n.options.defaultNS }
if (typeof namespaces === 'string') { namespaces = [namespaces] }
req.i18n.toJSON = () => null // do not serialize i18next instance and send to client
const initialI18nStore = {}
req.i18n.languages.forEach((l) => {
initialI18nStore[l] = {}
namespaces.forEach((ns) => {
initialI18nStore[l][ns] = (req.i18n.services.resourceStore.data[l] || {})[ns] || {}
})
})
return {
i18n: req.i18n, // use the instance on req - fixed language on request (avoid issues in race conditions with lngs of different users)
initialI18nStore,
initialLanguage: req.i18n.language
namespaces.forEach(ns => {
if (i18next.options.ns && i18next.options.ns.indexOf(ns) < 0) i18next.options.ns.push(ns);
});
export function useT(
ns: string = i18n.options.defaultNS[0],
): [TFunction, (resources: ObjectOf) => void] {
const { t } = useTranslation(ns.toLowerCase(), { useSuspense: false });
return [
t,
(resources: ObjectOf) => {
i18n.addResourceBundle(
i18n.language, ns.toLowerCase(), resources, true, true,
);
},
];
}