Skip to content

Commit

Permalink
refactor i18nCreateInterpolateElement
Browse files Browse the repository at this point in the history
  • Loading branch information
vraja-pro committed Jan 14, 2025
1 parent 3500d3c commit e258374
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
17 changes: 17 additions & 0 deletions packages/js/src/helpers/i18n.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { setLocaleData } from "@wordpress/i18n";
import { createInterpolateElement } from "@wordpress/element";
import { get } from "lodash";

/**
Expand Down Expand Up @@ -26,3 +27,19 @@ export function setTextdomainL10n( textdomain, l10nNamespace = "wpseoYoastJSL10n
setLocaleData( translations, textdomain );
}
}

/**
* Wrapper function for `createInterpolateElement` to catch errors.
*
* @param {string} interpolatedString The interpolated string.
* @param {object} conversionMap The conversion map object.
* @returns {string} The interpolated string.
*/
export const i18nCreateInterpolateElement = ( interpolatedString, conversionMap ) => {
try {
return createInterpolateElement( interpolatedString, conversionMap );
} catch ( error ) {
console.error( "Error in translation for:", interpolatedString, error );
return interpolatedString;
}
};
3 changes: 2 additions & 1 deletion packages/js/src/settings/routes/site-basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from "../components";
import { withDisabledMessageSupport, withFormikDummySelectField } from "../hocs";
import { useDispatchSettings, useSelectSettings } from "../hooks";
import { i18nCreateInterpolateElement } from "../../helpers/i18n";

const ToggleFieldWithDisabledMessageSupport = withDisabledMessageSupport( ToggleField );
const FormikSelectPageWithDummy = withFormikDummySelectField( FormikPageSelectField );
Expand Down Expand Up @@ -104,7 +105,7 @@ const SiteBasics = () => {
strong: <strong className="yst-font-semibold" />,
}
), [] );
const taglineDescription = useMemo( () => createInterpolateElement(
const taglineDescription = useMemo( () => i18nCreateInterpolateElement(
sprintf(
/**
* translators: %1$s expands to an opening anchor tag.
Expand Down

0 comments on commit e258374

Please sign in to comment.