Skip to content

Commit

Permalink
Add fallback for gettext calls with woocommerce text domain to `wc-…
Browse files Browse the repository at this point in the history
…calypso-bridge`
  • Loading branch information
yuliyan committed May 22, 2024
1 parent 9f81bc2 commit f1bcead
Showing 1 changed file with 76 additions and 1 deletion.
77 changes: 76 additions & 1 deletion src/i18n-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,85 @@
* External dependencies
*/
import md5 from 'crypto-js/md5';
import { setLocaleData } from '@wordpress/i18n';
import {
setLocaleData,
hasTranslation,
__,
_n,
_nx,
_x,
} from '@wordpress/i18n';
import { addFilter, applyFilters } from '@wordpress/hooks';

const defaultLocale = 'en_US';

/**
* Some gettext calls in this package use the `woocommerce` text domain
* and expect that the translation data is already loaded from WooCommerce.
* If a translation is missing, these filters will attempt to transate the string
* using the `wc-calypso-bridge` text domain as a fallback.
*/
addFilter(
'i18n.gettext_woocommerce',
'wc-calypso-bridge',
( translation, text, domain ) => {
if (
text === translation &&
! hasTranslation( text, undefined, domain )
) {
// eslint-disable-next-line @wordpress/i18n-no-variables
return __( text, 'wc-calypso-bridge' );
}

return translation;
}
);
addFilter(
'i18n.ngettext_woocommerce',
'wc-calypso-bridge',
( translation, single, plural, number, domain ) => {
if (
( single === translation || plural === translation ) &&
! hasTranslation( single, undefined, domain )
) {
// eslint-disable-next-line @wordpress/i18n-no-variables
return _n( single, plural, number, 'wc-calypso-bridge' );
}

return translation;
}
);
addFilter(
'i18n.gettext_with_context_woocommerce',
'wc-calypso-bridge',
( translation, text, context, domain ) => {
if (
text === translation &&
! hasTranslation( text, context, domain )
) {
// eslint-disable-next-line @wordpress/i18n-no-variables
return _x( text, context, 'wc-calypso-bridge' );
}

return translation;
}
);
addFilter(
'i18n.ngettext_with_context_woocommerce',
'wc-calypso-bridge',
( translation, single, plural, number, context, domain ) => {
if (
( single === translation || plural === translation ) &&
! hasTranslation( single, context, domain )
) {
// eslint-disable-next-line @wordpress/i18n-no-variables
return _nx( single, plural, number, context, 'wc-calypso-bridge' );
}

return translation;
}
);

/**
* Load lazy loaded modules translation data.
*
Expand Down

0 comments on commit f1bcead

Please sign in to comment.