Can we use a variable hook for language string?

I am using this to localize strings in my theme

__('Background image', 'themename');

there are plenty of strings that are localized but to make sure that the theme lang files and localization strings can be quickly changed, I would like to use something like this

$theme_name="themename";// this would be global var or var from a theme class

__('Background image', $theme_name); // or  $myClass->themename

and than use that variable everywhere it is needed.
This way the owner of the theme can quickly change theme name and does not have to walk trough all locaizations.

Do you see a problem with this ?

2 Answers
2

Yes this is problematic as i18n tools that parse your theme to generate a *.pot file for translation can’t understand this as they do not run PHP code but just search your code as text.

Here is a blogpost detailing on why this may cause trouble:

Bottom line: Inside all translation functions, no PHP variables are allowed in the strings, for any reason, ever. Plain single-quoted strings only.

But nevertheless the fact that you think about DRY is great! Actually as I’ve already talked about the i18n tools they can help you with automatically adding a textdomain. There even is a Grunt plugin to automate this plus the option to replace/rename textdomains which would solve your actual issue. Just comment if you need more help on that.

Leave a Comment