Adventskalender Day 20 – How can I teach Languages to a WordPress Theme?

marketpress-adventskalender-20

In this post I would like to teach you, and “your” WordPress Theme, how to use it international. Especially for multilingual websites, with differnent languages, it makes sense to translate a theme.

As a WordPress-Trainer, there is a frequently asked question in my trainings. My students want to know how to prepare already existing, or self build themes, for the localisation. WordPress offers perfect conditions for translating themes, without the need of editing the source code for each language.

Overview of the steps

  • Loading the text domain
  • Strings in WordPress function
  • Creating the language file
  • Translate
  • Testing

Loading the text domain

First we need to tell WordPress that our theme is translatable.

We insert the following code into the functions.php:
load_theme_textdomain('mp_theme_domain', get_template_directory() . '/languages');

„mp_theme_domain“ is our text domain, which should be unique. The most suitable approach here is the theme name as identificator. The second argument in the function defines the folder, where the language files will be saved. To guarantee the loading of the text domain, we need to call it within the setup function.

„marketpress_theme_setup“ stands for the setup function. At an existing theme it can variate.

Strings in WordPress functions

In the next step we define the strings WordPress has to load later. Beyond PHP functions we use _e($text_message); and withing __(&text_message);. If WordPress does not find any translation for $text_message, the original string will be delivered.

becomes to

The second variety will be used withing the functions.

becomes to

Sometimes strings contain dynamic data like digits or terms. In this case we take the sprintf PHP function, to control the final output of the string.

Singular and Plural

WordPress enables us to deliver singe or multiple strings. This is necessary and useful for search results.

Creating the language file

Therewith WordPress knows which string should be translated, and how, we create a list. There are three file extensions.

  • The .POT file contains all translated strings
  • The .PO Datei contains all translatable and translated strings
  • The .MO Datei is a binary file with the same content like the .po file, which can be read by WordPress only

In the first step we create a .POT file with the help of Poedit.

1. Open Poedit and create a new catalog

2. Fill the informations about the translation project
catalog
3. In the tab „Source paths“ you enter the folder. They behave relatively to the folder /languages/ within the theme folder
sources


4. „Key Words“ contains the function calls __ and _e.
keywords


5. Poedit searchs for a defined string, and we get a list, we can translate.
translationlist


6. Respective the .pot file will be saved in the folder /mytheme/languages/

This file is not necessary for WordPress, but it can be uses for further translations.

Translate

Now we start the translation! After you finished all strings, you save the file as de_DE.po in the folder /languages/ within the theme folder. Poedit automatically generates a .mo file, that is needed by WordPress.

Testing

After the language files are stored in the theme folder, we can start testing it within the frontend. Of corse only if the wp-config.php is defined with de_DE as language.

Conclusion

Now we have a localized Theme. That brings a bonus for your project and other WordPress users who want to use your theme.

Post Sharing

Author Avatar

WordPress ist meine Leidenschaft. Als Coach & Trainerin ist es mir wichtig, dass das vermittelte Wissen nachhaltig auch in der Praxis angewendet werden kann. Individuelle Anwenderschulungen für WordPress & Social Media sind meine Spezialität. Sprechen Sie mich an, wenn Sie wissen möchten, wie man das Beste aus WordPress für Ihre Projekte holen kann.

Similar Posts

slack-blog

New Plugin: Slack Connector - Connect WordPress, WooCommerce and Slack

by Birgit Olzem

Initially we merely wanted to optimize our own Slack-processes. With automated notifications from our MarkettPress shop, the blog and our forums. The resul ...

Read more
vip

Inpsyde is the first WordPress.com VIP partner in Germany

by Birgit Olzem

Automattic has given us the title of WordPress.com VIP Service Partner, the first in Germany, Austria and Switzerland. Worldwide, there are only eleven com ...

Read more
wooexperts

Inpsyde is a WooExperts Gold Partner of WooCommerce

by Birgit Olzem

A few weeks ago, Inpsyde GmbH with MarketPress became a certified WooExperts Gold Partner. This is confirmation from WooThemes of our agency’s know-h ...

Read more
backwpup

BackWPup Pro: Secure WordPress Backup with Google Drive

by Birgit Olzem

The Pro version of our BackWPup plugin supports the backup of WordPress databases and files to Google Drive. But how do you set something like that up? And ...

Read more

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">