Text Domains Across Multiple Plugins & Themes

All we need is an easy explanation of the problem, so here it is.

A common situation: a developer builds an entire site, based on WordPress, of course, for a client.

This site includes many custom plugins as well as a custom theme — maybe another theme or a child theme or two for good measure.

Doesn’t make a lot of sense for all those custom plugins and such to have their own text domain. A single translator will probably doing all the work. It’s also probably much easier to maintain a single set of translations.

So, how does one use a single text domain across multiple plugins and/or themes?

Just so we’re clear, I want the equivalent of load_plugin_textdomain and load_theme_textdomain that I can use for multiple plugins/themes.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You don’t. Translations are part of each item’s commit history, they should be kept as separate as the PHP code. In terms of performance, you don’t win much with a combined text domain. Actually, you could lose performance, because if you need that one translation array almost everywhere, you cannot load it selectively anymore or destroy it when you don’t need it.

One way to do what you want, is a separate plugin, maybe named “Client Name translations”, duplicate all strings in that and let the translators translate this file only. Prepare for sync hell. 🙂

Or you move all strings to a separate file with a single array …

return array(
    'name1' => __( 'Name one', 'your_text_domain' ),
    'name2' => __( 'Name two', 'your_text_domain' )

… and require_once that file in all other sub-projects (or write a wrapper plugin for it). Then you access the strings by their keys.

But I would try to keep separate commit histories for all projects. Once you want to reuse one of them you have to rewrite everything anyway.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply