Is a text-domain necessary for a child theme

Sorry for the noob question.

Is a text-domain necessary for a child theme? I am building a simple child theme with no text-domain declared. So, when I use strings which are to be translated, should I use the parent theme text-domain (yes parent theme has text-domain loaded and also has .mo/.po files).

For example adding this line in my child theme template

?php __('Some String', 'parent-text-domain'); 

Will be above string be translated?

Thanks in advance

Topic textdomain translation child-theme Wordpress

Category Web


If your child theme conains different strings than the parent theme.

The proper way to use a different textdomain in a child theme is the load_child_theme_textdomain() function now. You can use it the same way as other load_..._textdomain functions.

Beware!

Unlike plugin language files, a name like my_child_theme-de_DE.mo will NOT work. Although plugin language files allow you to specify the text-domain in the filename, this will NOT work with themes and child themes. Language files for themes should include the language shortcut ONLY.


TL;DR: If you use strings that are in the parent theme, exaclty how they are used in parent theme, you don't need to have a text domain for your child theme.

But if you use strings that aren't used in parent theme, to make them translatable, you'll need another text domain with related translation (.mo) files.


Translation workflow

When WordPress encounter a string in a translatation function it:

  1. Checks if a translation for required text domain has been loaded (via load_plugin_textdomain or load_theme_textdomain or load_textdomain), if so go to point 3.
  2. Checks if translations folder (by default wp-content/languages) contains a matching textdomain file. Matching textdomain file is "{$domain}-{$locale}.mo" where $domain is the text domain of the string to translate and $locale is the current locale for the website. If that file is not found the original string is returned, otherwise it is loaded and WP forwards to next point.
  3. When the textdomain is loaded, WP looks if the required string is contained in that file, if not the original string is returned, otherwiseWP forwards to next point.
  4. If the found translated string needs some singular / plural resolution (e.g. when using _n()) those are done. Otherwise WP forwards to next point.
  5. Filter hooks are applied on the translated string (see https://developer.wordpress.org/?s=gettext&post_type%5B%5D=wp-parser-hook) and finally the result is returned.

So?

When you use parent theme text domain in translation function from child theme (assuming parent theme ships and loads the textdomain file, or it has a translation file in translations folder), WordPress will arrive at point 3. in the list above, and so if the string is available in the file (because used in parent theme) it will be translated, otherwise not so.

It means that custom strings in parent theme needs own translation file.

In theory, it is possible to use parent textdomain in another translation file, because WordPress is capable to load more times the same text domain, "merging" them, but that has issues because only one file may exists in the format "{$domain}-{$locale}.mo" in translation folders (see point 2. in the list above).

So, in conclusion, the only viable way to make a child theme translatable, if it contains strings not used in parent theme, is to use own text domain and own translation file.

About

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