Is a text-domain necessary for a child theme

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.

Leave a Comment

tech