WordPress plugins internationalization using Poedit

Internationalization in WordPress allows plugins to be accessible to users of different languages. In this article, we’ll explore how to internationalize a plugin and use Poedit to simplify the process.

1: Internationalization in Code

Internationalization begins with identifying text strings that need to be translated in the plugin’s code. We use special functions like __() or _e() to mark these strings. Here’s an example:

<?php
// String to be translated
$mensaje = __('Hello World!', 'my-plugin');

// String to be directly outputted in content
_e('Welcome to my plugin', 'my-plugin');

 

After marking the strings, we create a translation file (.pot) using tools like Poedit. Here’s an example of the .pot file structure:

msgid ""
msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Language: en_US\n"

msgid "Hello World!"
msgstr "Hello World!"

msgid "Welcome to my plugin"
msgstr "Welcome to my plugin"

 

Next, we load and use these translations in the plugin:

<?php
// Load plugin domain
load_plugin_textdomain('my-plugin', false, dirname(plugin_basename(__FILE__)) . '/languages');

// Use translated strings
$translated_message = __('Hello World!', 'my-plugin');
_e('Welcome to my plugin', 'my-plugin');

2: Using Poedit for Internationalization

  1. Download and Install Poedit:
    • Go to the Poedit website (https://poedit.net/) and download the version suitable for your operating system.
    • Install Poedit on your computer.
  2. Create .pot File with Poedit:
    • Open Poedit and select File -> New Catalog from POT File...
    • Choose the main file of your plugin (e.g., my-plugin.php).
    • Save the catalog with the .pot extension in your plugin’s folder, within the ‘languages’ directory.
  3. Translate with Poedit:
    • Open the .pot file in Poedit, enter translations, and save the .po file in the ‘languages’ folder.
  4. Generate .mo File:
    • When saving the .po file, Poedit will automatically generate the corresponding .mo file.

With these steps, you’ve internationalized your plugin and made translation easier with Poedit. Remember to keep the translation files up to date when modifying the code. Now your plugin can be enjoyed in multiple languages!

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Back to Top
0
Would love your thoughts, please comment.x
()
x