Home » Why don’t string translations appear?

Why don’t string translations appear?

December 30th, 2010 by Amir

WPML’s String Translation lets you translate texts in your theme and plugins.

However, in order for these translations to appear in the site, some conditions have to be met:

  • The texts must be wrapped in GetText calls.
  • There must be a textdomain argument.

Any texts that are hard-coded in the PHP and not wrapped in GetText calls with a textdomain, will not be translated by WPML.


Static text not wrapped with GetText


Problem: The word ‘Links’ is not wrapped in a GetText call. It’s not translatable.

Solution: Wrap the text in the GetText Echo call:

<h2><?php _e('Links','theme-text-domain'); ?></h2>

We’ve wrapped ‘Links’ in the gettext call _e() (translatable echo). Now, WPML can translate it.

Text in function not wrapped with GetText

by <?php echo(the_author('', false)); ?>

Problem: The phrase is not translatable.

Solution: This phrase includes some static text and some dynamic text. It all needs to be wrapped in one GetText call and use arguments.

<?php printf( __( 'by %s', 'theme-text-domain' ), the_author('', false) ); ?>

The text that the translator will get is ‘by %s’. This is good because it allows the translator to switch the order of the words, which is required in some languages.

Notice that in this case we’ve used the __() GetText call. This call is similar to _e(), but it doesn’t write to the output (like echo does). The printf call does the output.

Text-domain argument is missing

<?php _e('Roadmap') ?>

Problem: There’s no textdomain argument in the GetText call.

Solution: Add a text-domain.

<?php _e('Roadmap','theme-text-domain') ?>

A text-domain tells GetText (and WPML) what the text belongs to. It’s a string that doesn’t mean anything, except to you. You can choose any string that you like, but you should be consistent. The text-domain value will group related strings together.

Obviously, ‘theme-text-domain’ is not a good and unique name. For instance, if you’ve called your theme ‘star-geeks’, that would be a better value for the text-domain.