9

I am working on a theme and I want to make it available for translating. I have not been including a text domain on translated text because I don't know what the text domain is going to be yet. I figured, until I can come up with a name for my theme I should just use a function to output my text domain name instead of hard coding it on translated text.

EXAMPLE:

This is the traditional way of adding a text domain:

echo _e('Hello', 'my-theme-name');

my approach is like this:

function text_domain() {
   define('TEXT_DOMAIN', 'my-theme-name', true );
   echo TEXT_DOMAIN;
}

and using like this:

 echo _e('Hello', text_domian());

...and when I do come up with a name for my theme I can just update it in one place instead a bunch of times in every file.

THE QUESTION IS: Is this an ok way to do it and will I encounter problems getting my theme approved by the WordPress theme review team?

1 Answer 1

16

No, this is not okay. NO NO NO. Here's my favourite Otto quote:

Inside all translation functions, no PHP variables are allowed in the strings, for any reason, ever.

Otto explains this in more detail, but the simple way to say it is that translation calls are parsed, not executed . So your variable (let alone your function) may not be evaluated and the text-domain breaks.

So, you're better off using a unique name like wpse241589 for your textdomain temporarily and do a search and replace once you have a better name.

3
  • 3
    The article you referenced answered my question and more, I greatly appreciate you taking the time to answer this question. Commented Oct 5, 2016 at 8:39
  • Touting my own, debatable take on textdomain: wordpress.stackexchange.com/questions/227148/…
    – cjbj
    Commented Oct 6, 2016 at 7:38
  • Very instructive answer and link
    – adelval
    Commented Oct 11, 2016 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.