Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to supress interpolation warning #1825

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/TransWithoutContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const mergeProps = (source, target) => {
return newTarget;
};

export const nodesToString = (children, i18nOptions) => {
export const nodesToString = (children, i18nOptions, i18nKey) => {
if (!children) return '';
let stringNode = '';

Expand Down Expand Up @@ -72,11 +72,13 @@ export const nodesToString = (children, i18nOptions) => {
stringNode += `<${type}>${childChildren}</${type}>`;
} else {
// regular case mapping the inner children
const content = nodesToString(childChildren, i18nOptions);
const content = nodesToString(childChildren, i18nOptions, i18nKey);
stringNode += `<${childIndex}>${content}</${childIndex}>`;
}
} else if (child === null) {
warn(`Trans: the passed in value is invalid - seems you passed in a null child.`);
if (!i18nOptions?.silentReactInterpolationWarning) {
warn(`Trans: the passed in value is invalid - seems you passed in a null child.`, i18nKey);
}
} else if (isObject(child)) {
// e.g. lorem {{ value, format }} ipsum
const { format, ...clone } = child;
Expand All @@ -85,17 +87,19 @@ export const nodesToString = (children, i18nOptions) => {
if (keys.length === 1) {
const value = format ? `${keys[0]}, ${format}` : keys[0];
stringNode += `{{${value}}}`;
} else {
} else if (!i18nOptions?.silentReactInterpolationWarning) {
// not a valid interpolation object (can only contain one value plus format)
warn(
`react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.`,
child,
i18nKey,
);
}
} else {
} else if (!i18nOptions?.silentReactInterpolationWarning) {
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,
i18nKey,
);
}
});
Expand Down Expand Up @@ -382,7 +386,7 @@ export function Trans({
let namespaces = ns || t.ns || i18n.options?.defaultNS;
namespaces = isString(namespaces) ? [namespaces] : namespaces || ['translation'];

const nodeAsString = nodesToString(children, reactI18nextOptions);
const nodeAsString = nodesToString(children, reactI18nextOptions, i18nKey);
const defaultValue =
defaults || nodeAsString || reactI18nextOptions.transEmptyNodeValue || i18nKey;
const { hashTransKey } = reactI18nextOptions;
Expand Down
Loading