4

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 ?

4

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.

  • thnx those posts were what I needed. I am not stuck with translations just wanted to make it easier for the users but I guess they will have to do the hard work – Benn Apr 27 '15 at 13:47
3

A technique like this has one really big advantage, and one really big disadvantage.

Advantage

The good thing is, like you mentioned, that the owner can dynamically change the theme name, and therefor the source where the translations are pulled from. If you have a special use case, for example different versions of the translations, this could be really useful.

I have one project where I use the first string as a variable. With this method I can dynamically translate strings that are returned from different functions, in my case from an API. In detail, I receive errors like AdError.INVALID_INPUT, and I can output an error description for each language.

Problem

The big disadvantage is however, there is no way for you to find all the strings in your theme/plugin. A lot of translation programs (or plugins like WPML) loop throug your files and search for __() and _e() functions (as well as all the other translation functions), and parse the strings to create the references for the translation.

If you use variables in your calls, either the string for the textdomain or the string for the translationstring can not be found, as they are dynamic, and not hardcoded into the file.

If you choose to take this path, you need an extra file somewhere, where all your translations are defined:

__( 'Your new String', 'yourtheme' );
__( 'Your new String2', 'yourtheme' );

In case you use .mo-files, it gets a bit easier, as you just have to change the file, but as soon as you add another string, all the translation files must be updated as well.

Conclusion

Generally, I would discourage you from using this approach for textdomains. It would be a lot easier to just Search/Replace throughout your themefiles (most editors can do that in a few clicks).

If you want translations for strings that you do not know or are dynamically generated, it can be a great simplification. Just be sure to add a filter to your gettext functions, to find out if the required translations are already recognized in your translation system/mo/po files.

Something like that should do the trick (untested):

add_filter( 'gettext', 'f711_check_for_translations', 10, 3 );

function f711_check_for_translations( $translated, $text, $domain ) {
    if ( $text == $translated ) {
        AddNewStringToTranslationStrings( $text ); // Function to add the string to your translation strings, however you are going to do that.
    }
    return $translated;
}
  • I cant accept both which I would rather do. – Benn Apr 27 '15 at 13:46
  • 1
    No problem. I think it is better to have the other answer accepted, as my solution can cause problems if you do not know exactly what you are doing. – fischi Apr 27 '15 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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