I am working with a site that is running WordPress in English, but they would like to run one of their plugins using the Welsh language. We already have the appropriate .po/.mo files for the plugin, but they are not being used while WordPress is running in English. Is there any way, perhaps with some custom PHP code in the plugin, that we can force WordPress to use the Welsh language files just for this plugin, but leave the rest of the site in English?
2 Answers
Assuming the plugin makes use of localization at all, you could for example try exchanging the possible load_plugin_textdomain
call with load_textdomain
where you can point to the file you like.
A cleaner way would be using the plugin_locale
filter that let's you modify the locale used by a plugin:
function my_plugin_locale_filter($locale, $domain)
{
if($domain === 'the_plugins_textdomain')
{
return 'cy_CY';
}
return $locale;
}
add_filter('plugin_locale', 'my_plugin_locale_filter', 10, 2);
That way the plugin can more or less safely be updated without the custom code being overwritten.
// to force use English, this filter value must return true.
add_filter('override_load_textdomain', 'myPlugin_OverrideLoadTextDomain', 10, 3);
add_filter('plugin_locale', 'myPlugin_forceUseLanguageForCertainPlugin', 10, 2);
function myPlugin_OverrideLoadTextDomain($override, $domain, $mofile) {
if ($domain === 'woocommerce') {// change text domain from woocommerce to what you want.
$override = true;
}
return $override;
}
function myPlugin_forceUseLanguageForCertainPlugin($locale, $domain) {
if ($domain ==== 'woocommerce') {// change text domain from woocommerce to what you want.
$locale = 'en_US';// change your locale here to whatever you want.
}
return $locale;
}
It required 2 filter hooks to work.