How to Create Translation Files for WordPress Themes

This guide provides a step-by-step explanation on how to create translation files for WordPress themes, preparing them for multilingual support.

1. Preparing for WordPress Theme Translation

Defining the Text Domain

First, you need to define the text domain in the theme’s `functions.php` file. This is required to load the translation files.

function my_theme_setup() {
    load_theme_textdomain('my-theme', get_template_directory() . '/languages');
}
add_action('after_setup_theme', 'my_theme_setup');

Using the Text Domain

Wrap the text to be translated with the translation functions, using the specified text domain.

<?php
echo __('Hello, World!', 'my-theme'); // Single string translation
_e('Welcome to my site!', 'my-theme'); // Direct output translation
?>

2. Creating Translation Files Using Poedit

Installing Poedit and Starting Translation Files

  • Install Poedit: Download and install [Poedit](https://poedit.net/).
  • Create a New Translation File: Open Poedit and click ‘New Translation’. Select the desired language (e.g., Korean `ko`).
  • Extract Strings: Click ‘Catalog > Update from Sources’ to automatically extract all translatable strings from the theme files.
  • Input Translations: Translate each extracted string and enter the translations.
  • Save .po and .mo Files: Poedit generates `.po` and `.mo` files. The file names follow the format `textdomain-languagecode_countrycode`. For example, `my-theme-ko_KR.po` and `my-theme-ko_KR.mo`.
  • Save File Location: Save the generated files in the theme’s `languages` folder.

3. Creating Translation Files Using gettext Commands

You can also use the terminal to create translation files instead of Poedit.

Installing gettext Tools and Creating Translation Template Files

sudo apt-get install gettext
xgettext --language=PHP --keyword=__ --keyword=_e --output=my-theme.pot path/to/theme/**/*.php

Generating .mo Files from .po Files

msgfmt my-theme-ko_KR.po -o my-theme-ko_KR.mo

4. Translation File Structure

The structure for storing translation files in the `languages` folder is as follows:

my-theme/
│
├── languages/
│   ├── my-theme-ko_KR.po
│   └── my-theme-ko_KR.mo
├── functions.php
...

5. Verifying Translation File Loading

To verify that the translation files are loaded correctly, add the following code to the `functions.php` file:

add_action('after_setup_theme', function() {
    load_theme_textdomain('my-theme', get_template_directory() . '/languages');
});

Summary

  • Store the translation files in the theme’s `languages` folder.
  • Use `load_theme_textdomain` in the `functions.php` file to load the translation files.
  • Create .po and .mo files using Poedit or `gettext` tools.

Following this process, you can translate a WordPress theme for multilingual support, providing a better experience for global users.

Appendix

Understanding the Default Path of `get_stylesheet_uri()`

The `get_stylesheet_uri()` function returns the URI of the `style.css` file of the currently active theme. The default path is the WordPress installation directory `wp-content/themes/[active theme folder]`. For example, you can use the following code to get the URI of the theme’s `style.css`:

<?php
$stylesheet_uri = get_stylesheet_uri();
echo $stylesheet_uri; // Example output: http://example.com/wp-content/themes/your-theme/style.css
?>

This function internally uses `get_stylesheet_directory_uri()` to return the URI of the current theme directory, appending `’/style.css’` to it.

Creating translation files and understanding the default path are essential steps in building a multilingual support site. This guide helps you perform translation tasks efficiently.

답글 남기기