Loading a language translation in a Child Theme

When we need introduce some changes in the behaviour, features or style of the theme that we are using, Best practices in the WordPress environment advises us to never change the principal theme but writting a small child theme containing those modifications. In that case, when those modifications show text on the screen, of course we can opt for outputting directly this text but it’s usually better to use the translation functions. But then, How to read a child theme translation keeping the principal (parent) theme translation?

Loading a language translation in a Child Theme

The solution is very simple because in fact there are two different functions to do that. For reading the translation of the parent theme, we have to use the function called load_theme_textdomain however, for reading the translation of the child theme, we have to use the function called load_child_theme_textdomain therefore, inside a child theme, we could use a code so simple as this:

<?php
function load_the_child_translation() {
   load_child_theme_textdomain( 
       wp_get_theme()->get( 'TextDomain' ), 
       esc_url( get_stylesheet_directory() ) . wp_get_theme()->get( 'DomainPath' ) 
   );
}
add_action( 'after_setup_theme', 'load_the_child_translation' );
?>

But if you want to extend this function and that it can be used in both contexts, child and parent themes, we could use something like this:

<?php
// give it another name inside the functions.php file of the child theme
function load_the_correct_translation() {
        
   if ( ! is_child_theme()  ) {
       load_theme_textdomain( 
             wp_get_theme( get_template() )->get( 'TextDomain' ), 
             esc_url( get_template_directory() ) . wp_get_theme( get_template() )->get( 'DomainPath' ) 
       );
   } else {
       load_child_theme_textdomain( 
             wp_get_theme( get_stylesheet() )->get( 'TextDomain' ), 
             esc_url( get_stylesheet_directory( get_stylesheet() ) ) . wp_get_theme()->get( 'DomainPath' ) 
       );
   }
}
// remeber to change the name of the funcion in child theme
add_action( 'after_setup_theme', 'load_the_correct_translation' );
?>

Of course, if you use this structure in both parent and child themes (via copy/paste), remember that in the second one, after copy/paste, you have to give a new name to this function and to modify the add_action line introducing the new name.

For a further information about loading a Language Domain you can see the load_theme_textdomain and the load_child_theme_textdomain oficial documentation. Finally, in both cases it could be important to read carefully the section User Contributed Notes of the oficial documentation for find out the correct model names for these language-only.mo files.

Have a nice WordPressing!

Leave a Comment

   Mandatory field
You can use these HTML tags inside the commment.
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>