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?

Topic textdomain translation child-theme hooks Wordpress

Category Web


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' );

Here's a quote from the source of load_textdomain (in WP 4.6):

If the text domain already exists, the translations will be merged. If both sets have the same string, the translation from the original value will be taken.

In other words, in order to make your translation override TwentyFifteen's, you must make sure your mo-files are loaded first, so they will be regarded as 'the original'. To do this, you can use the fact that a child theme's functions.php is loaded before the parent's.

In the parent theme find the function that is loading the textdomain. It probably uses the after_setup_theme hook. Make sure your child theme uses the same textdomain as the parent. Then load your textdomain with load_child_theme_textdomain on the same hook, but with a higher priority.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.