2

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

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' );
1
  • Edited my original reply to be more brief and objective. Dec 13 '16 at 20:05
1

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.

4
  • i tried this: function blognevisi_setup() { load_theme_textdomain( 'twentyfifteen', get_stylesheet_directory() . '/languages'); } add_action( 'after_setup_theme', 'blognevisi_setup', 8 ); in which "blognevisi" is my child theme and "twentyfifteen" is my parent theme. Oct 12 '16 at 12:13
  • it did not work Oct 12 '16 at 12:13
  • i think you did not understand my problem, it's not like TwentyFifteen has its own language files in /languages folder. the language files for TwentyFifteen are in wp-content/languages/themes. which loads before my files no matter what! Oct 12 '16 at 12:19
  • I have the same problem.... all the standard solutions online for child translations don't work
    – AlphaX
    Mar 30 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.