Make theme translatable for WPML

You don’t have to make anything special for WPML, using the regular translation code should be enough. See I18n for WordPress Developers in the Codex.

Code preparation

style.css

Add Text Domain and Domain Path to your theme’s style.css.

Example:

/*
 * Theme Name:    My awesome theme
 * Text Domain:   my_awesome_theme
 * Domain Path:  /languages
 */

Templates

Find all strings that should be translated and use the Text Domain value with the proper translation functions.

Example:

Replace …

echo 'Comments'

… with …

esc_html_e( 'Comments', 'my_awesome_theme' );

See wp-includes/l10n.php for available functions, and follwing the links in the Codex article mentioned earlier.

Theme directory

Create a directory for translation files from the value of Domain Path.

Example:

my-awesome-theme/languages

Now WPML should be able to find all strings for translation and to create the proper language files.

Finally, make sure the language file is actually loaded. Add the following code to the functions.php in you theme:

add_action( 'wp_loaded', 'my_awesome_theme_load_theme_language' );

/**
 * Load translations.
 *
 * @wp-hook wp_loaded
 * @return  bool
 */
function my_awesome_theme_load_theme_language()
{
    $lang_dir = get_stylesheet_directory() . '/languages';
    return load_theme_textdomain( 'my_awesome_theme', $lang_dir );
}

Leave a Comment