Make theme translatable for WPML

I have been really hard time to get my site translated.
I have MO and PO files, but my theme is not compatible with WPML and the developer team doesn’t to change it.

Therefore I may have to build a plugin or compatibility coding by myself. But I can’t really find how to do and what I need to do.

Does anybody know how to do? Or any tutorial for it? I can’t really find WMPL site as well.

2 Answers
2

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