string translation wpml

How to Translate Strings with WPML

Last Updated on April 28, 2022 by WP Knowledge Hub

Have you ever used WPML to translate content, only to realize that you still have untranslated sentences or words, such as your website’s tagline, footer widgets, or other template content? Well, WPML String Translation allows you to translate these bits of text known as “strings”. In tech-talk, a string is technically a sequence of characters that forms variables, words or sentences. This term covers pretty much everything outside of your WordPress website’s page and post copy.

Why Do I Need WPML String Translation?

If you’re translating content with WPML, translating posts, pages, and even Custom Post Types is a breeze, but translating template content is a bit trickier. If your theme uses a custom footer or header, odds are, you’ll need to translate those. Since WPML’s Advanced Translator doesn’t have a way to address that content directly, you’ll need to install and activate the WPML extension called String Translation.

Activating WPML String Translation

WPML’s String Translation extension is part of your WPML Multilingual CMS account. To get it, you need to download and install it from your account, under the Downloads section. Alternatively, you can download the OTGS plugin from the same downloads section, and this plugin will automatically detect which components your website needs, including String Translation.

By installing the OTGS plugin, you can download WPML Multilingual CMS and String Translation at the same time, and activate them from here.

Scanning For Strings

A large site with many pages, posts and plugins will most certainly contain many strings to translate. Not only will String Translator allow you to translate strings from your custom theme’s template, but you can also scan plugins to translate every available string in each individual one. To start scanning your themes and plugins, go to WPML > Themes and Plugin Localization:

Once you’ve selected the theme you want to scan by clicking the checkbox, you’ll be able to hit the “Scan Selected Themes for Strings” button. You can do the same for plugins in the Strings in the plugins box below.

How to translate a string

Once you’ve selected the theme and hit the scan button, WPML will start scanning the selected item for all possible strings, and list the templates in which it found them:

In this example, it found 915 strings in the Astra theme.

Next, you’ll want to go to WPML > String Translation.

In this page, you’ll see all the strings that can be translated listed under Domain. The domain is the area in which WPML is searching, such as the theme, the widgets, or other parts of the website. You can narrow down your search by clicking on “In Domain” and choosing a specific component, or use the search bar at the top of the page.

Once you’ve found the string you want to translate, you can do so by hitting the little + at the end of that line, and entering your translation in the box that appears:

You can save your translation by hitting the Enter.

What If I Don’t See My String in Any Domains?

For developers who created custom templates, you’ll need to wrap your strings in a special custom domain called ‘theme-text-domain’ in order for String Translation to detect them. Here’s an example of a custom hard-coded footer navigation with the proper code wrapped around each string:

      <li><a href="/programs/"><?php _e('Programs','theme-text-domain'); ?></a></li>
      <li><a href="/partners/nren/"><?php _e('Partners','theme-text-domain'); ?></a></li>
      <li><a href="/members/"><?php _e('Members','theme-text-domain'); ?></a></li>
      <li><a href="/about-us/"><?php _e('About Us','theme-text-domain'); ?></a></li>
      <li><a href="/contact-us/"><?php _e('Contact Us','theme-text-domain'); ?></a></li>

In the example above, the first list item <li> element is Programs, therefore by wrapping that word like so…

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

…it becomes a scannable string!

Now, if you go back to WPML > Themes and Plugin Localization, scan your theme again, and select theme-text-domain from the “in domain” list, you will see your new string!