How to override languages files in wp-content/languages/themes with child theme

I want to create a child theme for TwentyFifteen theme, which will customize a lot of things, including translation. When I install WordPress in my language (Farsi), it includes TwentyFifteen language files in wp-content/languages/themes

So when I create a languages folder in my child theme and add customized language files to it and add load_theme_textdomain( 'twentyfifteen', get_stylesheet_directory() . '/languages' ) to my child theme’s functions.php my customized language files do not load and instead the files in wp-content/languages/themes load. What can I do to override those files?

2 Answers
2

Since WP 4.6 load_theme_textdomain() (and consequently load_child_theme_textdomain()) will give priority to .mo files downloaded from WP’s online translation platform (translate.wordpress.org). Due to some new code (here, on line 769) these functions will completely ignore your local .mo files if the textdomain is found in the general languages/ directory.

You can, however, use the more basic load_textdomain() function to directly load your .mo file and override strings from the original domain, like this:

$domain = 'textdomain-to-override';
$path = get_stylesheet_directory() . '/languages/'; // adjust to the location of your .mo file

$locale = apply_filters( 'theme_locale', get_locale(), $domain );
load_textdomain( $domain, $path . $locale . '.mo' );

Leave a Comment