Projektstart

This commit is contained in:
2026-01-22 15:49:12 +01:00
parent 7212eb6f7a
commit 57e5f652f8
10637 changed files with 2598792 additions and 64 deletions

View File

@@ -0,0 +1,22 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.I18nextProvider = I18nextProvider;
var _react = require("react");
var _context = require("./context.js");
function I18nextProvider(_ref) {
let {
i18n,
defaultNS,
children
} = _ref;
const value = (0, _react.useMemo)(() => ({
i18n,
defaultNS
}), [i18n, defaultNS]);
return (0, _react.createElement)(_context.I18nContext.Provider, {
value
}, children);
}

View File

@@ -0,0 +1,55 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Trans = Trans;
Object.defineProperty(exports, "nodesToString", {
enumerable: true,
get: function () {
return _TransWithoutContext.nodesToString;
}
});
var _react = require("react");
var _TransWithoutContext = require("./TransWithoutContext.js");
var _context = require("./context.js");
function Trans(_ref) {
let {
children,
count,
parent,
i18nKey,
context,
tOptions = {},
values,
defaults,
components,
ns,
i18n: i18nFromProps,
t: tFromProps,
shouldUnescape,
...additionalProps
} = _ref;
const {
i18n: i18nFromContext,
defaultNS: defaultNSFromContext
} = (0, _react.useContext)(_context.I18nContext) || {};
const i18n = i18nFromProps || i18nFromContext || (0, _context.getI18n)();
const t = tFromProps || i18n && i18n.t.bind(i18n);
return (0, _TransWithoutContext.Trans)({
children,
count,
parent,
i18nKey,
context,
tOptions,
values,
defaults,
components,
ns: ns || t && t.ns || defaultNSFromContext || i18n && i18n.options && i18n.options.defaultNS,
i18n,
t: tFromProps,
shouldUnescape,
...additionalProps
});
}

View File

@@ -0,0 +1,270 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Trans = Trans;
exports.nodesToString = void 0;
var _react = require("react");
var _htmlParseStringify = _interopRequireDefault(require("html-parse-stringify"));
var _utils = require("./utils.js");
var _defaults = require("./defaults.js");
var _i18nInstance = require("./i18nInstance.js");
const hasChildren = (node, checkLength) => {
if (!node) return false;
const base = node.props ? node.props.children : node.children;
if (checkLength) return base.length > 0;
return !!base;
};
const getChildren = node => {
if (!node) return [];
const children = node.props ? node.props.children : node.children;
return node.props && node.props.i18nIsDynamicList ? getAsArray(children) : children;
};
const hasValidReactChildren = children => Array.isArray(children) && children.every(_react.isValidElement);
const getAsArray = data => Array.isArray(data) ? data : [data];
const mergeProps = (source, target) => {
const newTarget = {
...target
};
newTarget.props = Object.assign(source.props, target.props);
return newTarget;
};
const nodesToString = (children, i18nOptions) => {
if (!children) return '';
let stringNode = '';
const childrenArray = getAsArray(children);
const keepArray = i18nOptions.transSupportBasicHtmlNodes && i18nOptions.transKeepBasicHtmlNodesFor ? i18nOptions.transKeepBasicHtmlNodesFor : [];
childrenArray.forEach((child, childIndex) => {
if ((0, _utils.isString)(child)) {
stringNode += `${child}`;
} else if ((0, _react.isValidElement)(child)) {
const {
props,
type
} = child;
const childPropsCount = Object.keys(props).length;
const shouldKeepChild = keepArray.indexOf(type) > -1;
const childChildren = props.children;
if (!childChildren && shouldKeepChild && !childPropsCount) {
stringNode += `<${type}/>`;
} else if (!childChildren && (!shouldKeepChild || childPropsCount) || props.i18nIsDynamicList) {
stringNode += `<${childIndex}></${childIndex}>`;
} else if (shouldKeepChild && childPropsCount === 1 && (0, _utils.isString)(childChildren)) {
stringNode += `<${type}>${childChildren}</${type}>`;
} else {
const content = nodesToString(childChildren, i18nOptions);
stringNode += `<${childIndex}>${content}</${childIndex}>`;
}
} else if (child === null) {
(0, _utils.warn)(`Trans: the passed in value is invalid - seems you passed in a null child.`);
} else if ((0, _utils.isObject)(child)) {
const {
format,
...clone
} = child;
const keys = Object.keys(clone);
if (keys.length === 1) {
const value = format ? `${keys[0]}, ${format}` : keys[0];
stringNode += `{{${value}}}`;
} else {
(0, _utils.warn)(`react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.`, child);
}
} else {
(0, _utils.warn)(`Trans: the passed in value is invalid - seems you passed in a variable like {number} - please pass in variables for interpolation as full objects like {{number}}.`, child);
}
});
return stringNode;
};
exports.nodesToString = nodesToString;
const renderNodes = (children, targetString, i18n, i18nOptions, combinedTOpts, shouldUnescape) => {
if (targetString === '') return [];
const keepArray = i18nOptions.transKeepBasicHtmlNodesFor || [];
const emptyChildrenButNeedsHandling = targetString && new RegExp(keepArray.map(keep => `<${keep}`).join('|')).test(targetString);
if (!children && !emptyChildrenButNeedsHandling && !shouldUnescape) return [targetString];
const data = {};
const getData = childs => {
const childrenArray = getAsArray(childs);
childrenArray.forEach(child => {
if ((0, _utils.isString)(child)) return;
if (hasChildren(child)) getData(getChildren(child));else if ((0, _utils.isObject)(child) && !(0, _react.isValidElement)(child)) Object.assign(data, child);
});
};
getData(children);
const ast = _htmlParseStringify.default.parse(`<0>${targetString}</0>`);
const opts = {
...data,
...combinedTOpts
};
const renderInner = (child, node, rootReactNode) => {
const childs = getChildren(child);
const mappedChildren = mapAST(childs, node.children, rootReactNode);
return hasValidReactChildren(childs) && mappedChildren.length === 0 || child.props && child.props.i18nIsDynamicList ? childs : mappedChildren;
};
const pushTranslatedJSX = (child, inner, mem, i, isVoid) => {
if (child.dummy) {
child.children = inner;
mem.push((0, _react.cloneElement)(child, {
key: i
}, isVoid ? undefined : inner));
} else {
mem.push(..._react.Children.map([child], c => {
const props = {
...c.props
};
delete props.i18nIsDynamicList;
return (0, _react.createElement)(c.type, {
...props,
key: i,
ref: c.ref
}, isVoid ? null : inner);
}));
}
};
const mapAST = (reactNode, astNode, rootReactNode) => {
const reactNodes = getAsArray(reactNode);
const astNodes = getAsArray(astNode);
return astNodes.reduce((mem, node, i) => {
const translationContent = node.children && node.children[0] && node.children[0].content && i18n.services.interpolator.interpolate(node.children[0].content, opts, i18n.language);
if (node.type === 'tag') {
let tmp = reactNodes[parseInt(node.name, 10)];
if (rootReactNode.length === 1 && !tmp) tmp = rootReactNode[0][node.name];
if (!tmp) tmp = {};
const child = Object.keys(node.attrs).length !== 0 ? mergeProps({
props: node.attrs
}, tmp) : tmp;
const isElement = (0, _react.isValidElement)(child);
const isValidTranslationWithChildren = isElement && hasChildren(node, true) && !node.voidElement;
const isEmptyTransWithHTML = emptyChildrenButNeedsHandling && (0, _utils.isObject)(child) && child.dummy && !isElement;
const isKnownComponent = (0, _utils.isObject)(children) && Object.hasOwnProperty.call(children, node.name);
if ((0, _utils.isString)(child)) {
const value = i18n.services.interpolator.interpolate(child, opts, i18n.language);
mem.push(value);
} else if (hasChildren(child) || isValidTranslationWithChildren) {
const inner = renderInner(child, node, rootReactNode);
pushTranslatedJSX(child, inner, mem, i);
} else if (isEmptyTransWithHTML) {
const inner = mapAST(reactNodes, node.children, rootReactNode);
pushTranslatedJSX(child, inner, mem, i);
} else if (Number.isNaN(parseFloat(node.name))) {
if (isKnownComponent) {
const inner = renderInner(child, node, rootReactNode);
pushTranslatedJSX(child, inner, mem, i, node.voidElement);
} else if (i18nOptions.transSupportBasicHtmlNodes && keepArray.indexOf(node.name) > -1) {
if (node.voidElement) {
mem.push((0, _react.createElement)(node.name, {
key: `${node.name}-${i}`
}));
} else {
const inner = mapAST(reactNodes, node.children, rootReactNode);
mem.push((0, _react.createElement)(node.name, {
key: `${node.name}-${i}`
}, inner));
}
} else if (node.voidElement) {
mem.push(`<${node.name} />`);
} else {
const inner = mapAST(reactNodes, node.children, rootReactNode);
mem.push(`<${node.name}>${inner}</${node.name}>`);
}
} else if ((0, _utils.isObject)(child) && !isElement) {
const content = node.children[0] ? translationContent : null;
if (content) mem.push(content);
} else {
pushTranslatedJSX(child, translationContent, mem, i, node.children.length !== 1 || !translationContent);
}
} else if (node.type === 'text') {
const wrapTextNodes = i18nOptions.transWrapTextNodes;
const content = shouldUnescape ? i18nOptions.unescape(i18n.services.interpolator.interpolate(node.content, opts, i18n.language)) : i18n.services.interpolator.interpolate(node.content, opts, i18n.language);
if (wrapTextNodes) {
mem.push((0, _react.createElement)(wrapTextNodes, {
key: `${node.name}-${i}`
}, content));
} else {
mem.push(content);
}
}
return mem;
}, []);
};
const result = mapAST([{
dummy: true,
children: children || []
}], ast, getAsArray(children || []));
return getChildren(result[0]);
};
function Trans(_ref) {
let {
children,
count,
parent,
i18nKey,
context,
tOptions = {},
values,
defaults,
components,
ns,
i18n: i18nFromProps,
t: tFromProps,
shouldUnescape,
...additionalProps
} = _ref;
const i18n = i18nFromProps || (0, _i18nInstance.getI18n)();
if (!i18n) {
(0, _utils.warnOnce)('You will need to pass in an i18next instance by using i18nextReactModule');
return children;
}
const t = tFromProps || i18n.t.bind(i18n) || (k => k);
const reactI18nextOptions = {
...(0, _defaults.getDefaults)(),
...(i18n.options && i18n.options.react)
};
let namespaces = ns || t.ns || i18n.options && i18n.options.defaultNS;
namespaces = (0, _utils.isString)(namespaces) ? [namespaces] : namespaces || ['translation'];
const nodeAsString = nodesToString(children, reactI18nextOptions);
const defaultValue = defaults || nodeAsString || reactI18nextOptions.transEmptyNodeValue || i18nKey;
const {
hashTransKey
} = reactI18nextOptions;
const key = i18nKey || (hashTransKey ? hashTransKey(nodeAsString || defaultValue) : nodeAsString || defaultValue);
if (i18n.options && i18n.options.interpolation && i18n.options.interpolation.defaultVariables) {
values = values && Object.keys(values).length > 0 ? {
...values,
...i18n.options.interpolation.defaultVariables
} : {
...i18n.options.interpolation.defaultVariables
};
}
const interpolationOverride = values || count !== undefined || !children ? tOptions.interpolation : {
interpolation: {
...tOptions.interpolation,
prefix: '#$?',
suffix: '?$#'
}
};
const combinedTOpts = {
...tOptions,
context: context || tOptions.context,
count,
...values,
...interpolationOverride,
defaultValue,
ns: namespaces
};
const translation = key ? t(key, combinedTOpts) : defaultValue;
if (components) {
Object.keys(components).forEach(c => {
const comp = components[c];
if (typeof comp.type === 'function' || !comp.props || !comp.props.children || translation.indexOf(`${c}/>`) < 0 && translation.indexOf(`${c} />`) < 0) return;
function Componentized() {
return (0, _react.createElement)(_react.Fragment, null, comp);
}
components[c] = (0, _react.createElement)(Componentized);
});
}
const content = renderNodes(components || children, translation, i18n, reactI18nextOptions, combinedTOpts, shouldUnescape);
const useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent;
return useAsParent ? (0, _react.createElement)(useAsParent, additionalProps, content) : content;
}

View File

@@ -0,0 +1,19 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Translation = Translation;
var _useTranslation = require("./useTranslation.js");
function Translation(props) {
const {
ns,
children,
...options
} = props;
const [t, i18n, ready] = (0, _useTranslation.useTranslation)(ns, options);
return children(t, {
i18n,
lng: i18n.language
}, ready);
}

View File

@@ -0,0 +1,79 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.composeInitialProps = exports.ReportNamespaces = exports.I18nContext = void 0;
Object.defineProperty(exports, "getDefaults", {
enumerable: true,
get: function () {
return _defaults.getDefaults;
}
});
Object.defineProperty(exports, "getI18n", {
enumerable: true,
get: function () {
return _i18nInstance.getI18n;
}
});
exports.getInitialProps = void 0;
Object.defineProperty(exports, "initReactI18next", {
enumerable: true,
get: function () {
return _initReactI18next.initReactI18next;
}
});
Object.defineProperty(exports, "setDefaults", {
enumerable: true,
get: function () {
return _defaults.setDefaults;
}
});
Object.defineProperty(exports, "setI18n", {
enumerable: true,
get: function () {
return _i18nInstance.setI18n;
}
});
var _react = require("react");
var _defaults = require("./defaults.js");
var _i18nInstance = require("./i18nInstance.js");
var _initReactI18next = require("./initReactI18next.js");
const I18nContext = exports.I18nContext = (0, _react.createContext)();
class ReportNamespaces {
constructor() {
this.usedNamespaces = {};
}
addUsedNamespaces(namespaces) {
namespaces.forEach(ns => {
if (!this.usedNamespaces[ns]) this.usedNamespaces[ns] = true;
});
}
getUsedNamespaces = () => Object.keys(this.usedNamespaces);
}
exports.ReportNamespaces = ReportNamespaces;
const composeInitialProps = ForComponent => async ctx => {
const componentsInitialProps = ForComponent.getInitialProps ? await ForComponent.getInitialProps(ctx) : {};
const i18nInitialProps = getInitialProps();
return {
...componentsInitialProps,
...i18nInitialProps
};
};
exports.composeInitialProps = composeInitialProps;
const getInitialProps = () => {
const i18n = (0, _i18nInstance.getI18n)();
const namespaces = i18n.reportNamespaces ? i18n.reportNamespaces.getUsedNamespaces() : [];
const ret = {};
const initialI18nStore = {};
i18n.languages.forEach(l => {
initialI18nStore[l] = {};
namespaces.forEach(ns => {
initialI18nStore[l][ns] = i18n.getResourceBundle(l, ns) || {};
});
});
ret.initialI18nStore = initialI18nStore;
ret.initialLanguage = i18n.language;
return ret;
};
exports.getInitialProps = getInitialProps;

View File

@@ -0,0 +1,27 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.setDefaults = exports.getDefaults = void 0;
var _unescape = require("./unescape.js");
let defaultOptions = {
bindI18n: 'languageChanged',
bindI18nStore: '',
transEmptyNodeValue: '',
transSupportBasicHtmlNodes: true,
transWrapTextNodes: '',
transKeepBasicHtmlNodesFor: ['br', 'strong', 'i', 'p'],
useSuspense: true,
unescape: _unescape.unescape
};
const setDefaults = function () {
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
defaultOptions = {
...defaultOptions,
...options
};
};
exports.setDefaults = setDefaults;
const getDefaults = () => defaultOptions;
exports.getDefaults = getDefaults;

View File

@@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.setI18n = exports.getI18n = void 0;
let i18nInstance;
const setI18n = instance => {
i18nInstance = instance;
};
exports.setI18n = setI18n;
const getI18n = () => i18nInstance;
exports.getI18n = getI18n;

View File

@@ -0,0 +1,128 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "I18nContext", {
enumerable: true,
get: function () {
return _context.I18nContext;
}
});
Object.defineProperty(exports, "I18nextProvider", {
enumerable: true,
get: function () {
return _I18nextProvider.I18nextProvider;
}
});
Object.defineProperty(exports, "Trans", {
enumerable: true,
get: function () {
return _Trans.Trans;
}
});
Object.defineProperty(exports, "TransWithoutContext", {
enumerable: true,
get: function () {
return _TransWithoutContext.Trans;
}
});
Object.defineProperty(exports, "Translation", {
enumerable: true,
get: function () {
return _Translation.Translation;
}
});
Object.defineProperty(exports, "composeInitialProps", {
enumerable: true,
get: function () {
return _context.composeInitialProps;
}
});
exports.date = void 0;
Object.defineProperty(exports, "getDefaults", {
enumerable: true,
get: function () {
return _defaults.getDefaults;
}
});
Object.defineProperty(exports, "getI18n", {
enumerable: true,
get: function () {
return _i18nInstance.getI18n;
}
});
Object.defineProperty(exports, "getInitialProps", {
enumerable: true,
get: function () {
return _context.getInitialProps;
}
});
Object.defineProperty(exports, "initReactI18next", {
enumerable: true,
get: function () {
return _initReactI18next.initReactI18next;
}
});
exports.selectOrdinal = exports.select = exports.plural = exports.number = void 0;
Object.defineProperty(exports, "setDefaults", {
enumerable: true,
get: function () {
return _defaults.setDefaults;
}
});
Object.defineProperty(exports, "setI18n", {
enumerable: true,
get: function () {
return _i18nInstance.setI18n;
}
});
exports.time = void 0;
Object.defineProperty(exports, "useSSR", {
enumerable: true,
get: function () {
return _useSSR.useSSR;
}
});
Object.defineProperty(exports, "useTranslation", {
enumerable: true,
get: function () {
return _useTranslation.useTranslation;
}
});
Object.defineProperty(exports, "withSSR", {
enumerable: true,
get: function () {
return _withSSR.withSSR;
}
});
Object.defineProperty(exports, "withTranslation", {
enumerable: true,
get: function () {
return _withTranslation.withTranslation;
}
});
var _Trans = require("./Trans.js");
var _TransWithoutContext = require("./TransWithoutContext.js");
var _useTranslation = require("./useTranslation.js");
var _withTranslation = require("./withTranslation.js");
var _Translation = require("./Translation.js");
var _I18nextProvider = require("./I18nextProvider.js");
var _withSSR = require("./withSSR.js");
var _useSSR = require("./useSSR.js");
var _initReactI18next = require("./initReactI18next.js");
var _defaults = require("./defaults.js");
var _i18nInstance = require("./i18nInstance.js");
var _context = require("./context.js");
const date = () => '';
exports.date = date;
const time = () => '';
exports.time = time;
const number = () => '';
exports.number = number;
const select = () => '';
exports.select = select;
const plural = () => '';
exports.plural = plural;
const selectOrdinal = () => '';
exports.selectOrdinal = selectOrdinal;

View File

@@ -0,0 +1,15 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.initReactI18next = void 0;
var _defaults = require("./defaults.js");
var _i18nInstance = require("./i18nInstance.js");
const initReactI18next = exports.initReactI18next = {
type: '3rdParty',
init(instance) {
(0, _defaults.setDefaults)(instance.options.react);
(0, _i18nInstance.setI18n)(instance);
}
};

View File

@@ -0,0 +1,32 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.unescape = void 0;
const matchHtmlEntity = /&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34|nbsp|#160|copy|#169|reg|#174|hellip|#8230|#x2F|#47);/g;
const htmlEntities = {
'&amp;': '&',
'&#38;': '&',
'&lt;': '<',
'&#60;': '<',
'&gt;': '>',
'&#62;': '>',
'&apos;': "'",
'&#39;': "'",
'&quot;': '"',
'&#34;': '"',
'&nbsp;': ' ',
'&#160;': ' ',
'&copy;': '©',
'&#169;': '©',
'&reg;': '®',
'&#174;': '®',
'&hellip;': '…',
'&#8230;': '…',
'&#x2F;': '/',
'&#47;': '/'
};
const unescapeHtmlEntity = m => htmlEntities[m];
const unescape = text => text.replace(matchHtmlEntity, unescapeHtmlEntity);
exports.unescape = unescape;

View File

@@ -0,0 +1,35 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useSSR = void 0;
var _react = require("react");
var _context = require("./context.js");
const useSSR = function (initialI18nStore, initialLanguage) {
let props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
const {
i18n: i18nFromProps
} = props;
const {
i18n: i18nFromContext
} = (0, _react.useContext)(_context.I18nContext) || {};
const i18n = i18nFromProps || i18nFromContext || (0, _context.getI18n)();
if (i18n.options && i18n.options.isClone) return;
if (initialI18nStore && !i18n.initializedStoreOnce) {
i18n.services.resourceStore.data = initialI18nStore;
i18n.options.ns = Object.values(initialI18nStore).reduce((mem, lngResources) => {
Object.keys(lngResources).forEach(ns => {
if (mem.indexOf(ns) < 0) mem.push(ns);
});
return mem;
}, i18n.options.ns);
i18n.initializedStoreOnce = true;
i18n.isInitialized = true;
}
if (initialLanguage && !i18n.initializedLanguageOnce) {
i18n.changeLanguage(initialLanguage);
i18n.initializedLanguageOnce = true;
}
};
exports.useSSR = useSSR;

View File

@@ -0,0 +1,115 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useTranslation = void 0;
var _react = require("react");
var _context = require("./context.js");
var _utils = require("./utils.js");
const usePrevious = (value, ignore) => {
const ref = (0, _react.useRef)();
(0, _react.useEffect)(() => {
ref.current = ignore ? 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) => (0, _react.useCallback)(alwaysNewT(i18n, language, namespace, keyPrefix), [i18n, language, namespace, keyPrefix]);
const useTranslation = function (ns) {
let props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
const {
i18n: i18nFromProps
} = props;
const {
i18n: i18nFromContext,
defaultNS: defaultNSFromContext
} = (0, _react.useContext)(_context.I18nContext) || {};
const i18n = i18nFromProps || i18nFromContext || (0, _context.getI18n)();
if (i18n && !i18n.reportNamespaces) i18n.reportNamespaces = new _context.ReportNamespaces();
if (!i18n) {
(0, _utils.warnOnce)('You will need to pass in an i18next instance by using initReactI18next');
const notReadyT = (k, optsOrDefaultValue) => {
if ((0, _utils.isString)(optsOrDefaultValue)) return optsOrDefaultValue;
if ((0, _utils.isObject)(optsOrDefaultValue) && (0, _utils.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 && i18n.options.react.wait !== undefined) (0, _utils.warnOnce)('It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.');
const i18nOptions = {
...(0, _context.getDefaults)(),
...i18n.options.react,
...props
};
const {
useSuspense,
keyPrefix
} = i18nOptions;
let namespaces = ns || defaultNSFromContext || i18n.options && i18n.options.defaultNS;
namespaces = (0, _utils.isString)(namespaces) ? [namespaces] : namespaces || ['translation'];
if (i18n.reportNamespaces.addUsedNamespaces) i18n.reportNamespaces.addUsedNamespaces(namespaces);
const ready = (i18n.isInitialized || i18n.initializedStoreOnce) && namespaces.every(n => (0, _utils.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] = (0, _react.useState)(getT);
let joinedNS = namespaces.join();
if (props.lng) joinedNS = `${props.lng}${joinedNS}`;
const previousJoinedNS = usePrevious(joinedNS);
const isMounted = (0, _react.useRef)(true);
(0, _react.useEffect)(() => {
const {
bindI18n,
bindI18nStore
} = i18nOptions;
isMounted.current = true;
if (!ready && !useSuspense) {
if (props.lng) {
(0, _utils.loadLanguages)(i18n, props.lng, namespaces, () => {
if (isMounted.current) setT(getNewT);
});
} else {
(0, _utils.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) i18n.on(bindI18n, boundReset);
if (bindI18nStore && i18n) i18n.store.on(bindI18nStore, boundReset);
return () => {
isMounted.current = false;
if (bindI18n && i18n) bindI18n.split(' ').forEach(e => i18n.off(e, boundReset));
if (bindI18nStore && i18n) bindI18nStore.split(' ').forEach(e => i18n.store.off(e, boundReset));
};
}, [i18n, joinedNS]);
(0, _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) {
(0, _utils.loadLanguages)(i18n, props.lng, namespaces, () => resolve());
} else {
(0, _utils.loadNamespaces)(i18n, namespaces, () => resolve());
}
});
};
exports.useTranslation = useTranslation;

View File

@@ -0,0 +1,91 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.loadNamespaces = exports.loadLanguages = exports.isString = exports.isObject = exports.hasLoadedNamespace = exports.getDisplayName = void 0;
exports.warn = warn;
exports.warnOnce = warnOnce;
function warn() {
if (console && console.warn) {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
if (isString(args[0])) args[0] = `react-i18next:: ${args[0]}`;
console.warn(...args);
}
}
const alreadyWarned = {};
function warnOnce() {
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
if (isString(args[0]) && alreadyWarned[args[0]]) return;
if (isString(args[0])) alreadyWarned[args[0]] = new Date();
warn(...args);
}
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));
};
exports.loadNamespaces = loadNamespaces;
const loadLanguages = (i18n, lng, ns, cb) => {
if (isString(ns)) ns = [ns];
ns.forEach(n => {
if (i18n.options.ns.indexOf(n) < 0) i18n.options.ns.push(n);
});
i18n.loadLanguages(lng, loadedClb(i18n, cb));
};
exports.loadLanguages = loadLanguages;
const oldI18nextHasLoadedNamespace = function (ns, i18n) {
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
const lng = i18n.languages[0];
const fallbackLng = i18n.options ? i18n.options.fallbackLng : false;
const lastLng = i18n.languages[i18n.languages.length - 1];
if (lng.toLowerCase() === 'cimode') return true;
const loadNotPending = (l, n) => {
const loadState = i18n.services.backendConnector.state[`${l}|${n}`];
return loadState === -1 || loadState === 2;
};
if (options.bindI18n && options.bindI18n.indexOf('languageChanging') > -1 && i18n.services.backendConnector.backend && i18n.isLanguageChangingTo && !loadNotPending(i18n.isLanguageChangingTo, ns)) return false;
if (i18n.hasResourceBundle(lng, ns)) return true;
if (!i18n.services.backendConnector.backend || i18n.options.resources && !i18n.options.partialBundledLanguages) return true;
if (loadNotPending(lng, ns) && (!fallbackLng || loadNotPending(lastLng, ns))) return true;
return false;
};
const hasLoadedNamespace = function (ns, i18n) {
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
if (!i18n.languages || !i18n.languages.length) {
warnOnce('i18n.languages were undefined or empty', i18n.languages);
return true;
}
const isNewerI18next = i18n.options.ignoreJSONStructure !== undefined;
if (!isNewerI18next) {
return oldI18nextHasLoadedNamespace(ns, i18n, options);
}
return i18n.hasLoadedNamespace(ns, {
lng: options.lng,
precheck: (i18nInstance, loadNotPending) => {
if (options.bindI18n && options.bindI18n.indexOf('languageChanging') > -1 && i18nInstance.services.backendConnector.backend && i18nInstance.isLanguageChangingTo && !loadNotPending(i18nInstance.isLanguageChangingTo, ns)) return false;
}
});
};
exports.hasLoadedNamespace = hasLoadedNamespace;
const getDisplayName = Component => Component.displayName || Component.name || (isString(Component) && Component.length > 0 ? Component : 'Unknown');
exports.getDisplayName = getDisplayName;
const isString = obj => typeof obj === 'string';
exports.isString = isString;
const isObject = obj => typeof obj === 'object' && obj !== null;
exports.isObject = isObject;

View File

@@ -0,0 +1,28 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.withSSR = void 0;
var _react = require("react");
var _useSSR = require("./useSSR.js");
var _context = require("./context.js");
var _utils = require("./utils.js");
const withSSR = () => function Extend(WrappedComponent) {
function I18nextWithSSR(_ref) {
let {
initialI18nStore,
initialLanguage,
...rest
} = _ref;
(0, _useSSR.useSSR)(initialI18nStore, initialLanguage);
return (0, _react.createElement)(WrappedComponent, {
...rest
});
}
I18nextWithSSR.getInitialProps = (0, _context.composeInitialProps)(WrappedComponent);
I18nextWithSSR.displayName = `withI18nextSSR(${(0, _utils.getDisplayName)(WrappedComponent)})`;
I18nextWithSSR.WrappedComponent = WrappedComponent;
return I18nextWithSSR;
};
exports.withSSR = withSSR;

View File

@@ -0,0 +1,43 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.withTranslation = void 0;
var _react = require("react");
var _useTranslation = require("./useTranslation.js");
var _utils = require("./utils.js");
const withTranslation = function (ns) {
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return function Extend(WrappedComponent) {
function I18nextWithTranslation(_ref) {
let {
forwardedRef,
...rest
} = _ref;
const [t, i18n, ready] = (0, _useTranslation.useTranslation)(ns, {
...rest,
keyPrefix: options.keyPrefix
});
const passDownProps = {
...rest,
t,
i18n,
tReady: ready
};
if (options.withRef && forwardedRef) {
passDownProps.ref = forwardedRef;
} else if (!options.withRef && forwardedRef) {
passDownProps.forwardedRef = forwardedRef;
}
return (0, _react.createElement)(WrappedComponent, passDownProps);
}
I18nextWithTranslation.displayName = `withI18nextTranslation(${(0, _utils.getDisplayName)(WrappedComponent)})`;
I18nextWithTranslation.WrappedComponent = WrappedComponent;
const forwardRef = (props, ref) => (0, _react.createElement)(I18nextWithTranslation, Object.assign({}, props, {
forwardedRef: ref
}));
return options.withRef ? (0, _react.forwardRef)(forwardRef) : I18nextWithTranslation;
};
};
exports.withTranslation = withTranslation;