Skip to content

Instantly share code, notes, and snippets.

@agkovalev
Last active February 17, 2021 10:57
Show Gist options
  • Save agkovalev/267f4b0c53c5942c198915dfb8a915e3 to your computer and use it in GitHub Desktop.
Save agkovalev/267f4b0c53c5942c198915dfb8a915e3 to your computer and use it in GitHub Desktop.
Override translations in child theme for plugins, themes etc.
<?php
/**
* Translations in Child Theme!
*/
if (!function_exists('agk_reload_child_theme_domain')) {
function agk_reload_child_theme_domain($domain)
{
$path = get_stylesheet_directory() . '/languages/';
$mofile = $path . $domain . '-' . get_locale() . '.mo';
if (is_readable($mofile)) {
unload_textdomain($domain);
load_textdomain($domain, $mofile);
} else {
var_dump('Not found: ', $mofile);
}
}
}
add_action('after_setup_theme', function () {
$domains = [
'woocommerce', // Woocommerce translations don't work so...
'woocommerce-germanized'
];
foreach ($domains as $domain) {
agk_reload_child_theme_domain($domain);
}
});
// OR:
add_filter('load_textdomain_mofile', function ($mofile, $domain) {
$domains = [
'woocommerce',
'woocommerce-germanized'
];
if (in_array($domain, $domains)) {
$path = get_stylesheet_directory() . '/languages/';
$mofileNew = $path . $domain . '-' . get_locale() . '.mo';
$mofile = is_readable($mofileNew) ? $mofileNew : $mofile;
}
return $mofile;
}, 10, 2);
/**
* END of: Translations in Child Theme!
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment