126 PART II • Designing and Developing WordPress Themes
CHILD THEMES AND INTERNATIONALIZATION
Preparing your theme for other languages than your own is important, even for child themes.
As with most other things, the child theme will inherit the internationalization from the
parent theme. This is covered more in-depth in Chapter 6, “Advanced Theme Usage.”
Child themes are created because they differ from their parent themes. Sometimes this means
that there are new strings that need to be translated. You obviously don’t want to add these to the
parent theme’s language files, as this would clash with the whole idea of parent/child themes.
Instead, you add to them by using load_child_theme_textdomain() and adding your
child theme language files instead. Again, internationalization is covered more in-depth in the
next chapter, so let’s just look at the function and hook needed to do this.
The following in functions.php will prepare your theme for internationalization:
function smashing_child_setup() {
load_child_theme_textdomain( 'smashing_child', get_stylesheet_directory().
'/lang' );
}
add_action( 'after_setup_theme', 'smashing_child_setup' );
The function smashing_child_setup() is a general one suitable for theme setup and is
added to after_setup_theme with add_action(), as you can see. By using load_
child_theme_textdomain(), you tell the theme that the textdomain is smashing_
child and that the language files will be in the lang folder within the child theme’s very own
theme folder. Now all you have to do is pass smashing_child as the textdomain for strings
that you want to be able to translate, much as you would have had it been a regular theme.
See Chapter 6 for more on internationalization.
THE FLIP SIDE OF INHERITANCE
You already know that most template files in a child theme take precedence over the parent
theme’s counterpart. A child theme’s style.css trumps the style.css of the parent theme, and so
does the child theme’s index.php compared to the parent theme’s index.php, and so on.
The child theme inherits the contents of the parent theme, but only if it needs it.
This brings up some issues, the most obvious probably being “what if they don’t match, design-
wise?” Well, the whole idea with child themes is to make customizations to themes you like. In
other words, if you create a child theme based on a parent theme that you end up changing
altogether, with new template files for just about everything, you may have defeated the purpose.
After all, that is just like taking the original theme and making a new version of it, which means
that you’re missing out on the smooth upgrading perks that child themes can claim.
A child theme is most warranted when most of your changes go in style.css, at most a few
template files, and possibly some changes using functions.php. The former can alter the look
and feel of the theme, whereas the latter two can change the necessary functionality.