variable in TextDomain Gettext & wordpress, bad practice?

372 views Asked by At

I dont want to manually type in "my_theme_textdomain" or "my_plugin_textdomain" every time.

so instead of

$hello =  __( 'Hello, dear user!', 'my-text-domain' );

I use

$my_txtdomain = "my-text-domain";
$hello =  __( 'Hello, dear user!', $my_txtdomain );

I feel like it shouldnt be a problem since $my_txtdomain is essentialy just a string. But is this a bad practice ?

1

There are 1 answers

0
Emin Özlem On

Turns out it is "kind of" a bad practice, but not really. But it will work. You can read the discussion below: https://markjaquith.wordpress.com/2011/10/06/translating-wordpress-plugins-and-themes-dont-get-clever/

And they think to themselves “hm, I sure am typing the 'my-plugin-name' string a lot. I’ll apply the DRY (Don’t Repeat Yourself) principle and throw that string into a variable or a constant!”

Stop! You’re being too clever! That won’t work!*


the domain argument isn’t parsed at all. It is used only when the string is translated to choose the correct .mo file and it’s perfectly OK to be a variable.