Help Wordpress

Blog about WordPress, plugins, themplates and more…
MENU
Home » Tutorials » Internationalization of the WordPress Plugins

Internationalization of the WordPress Plugins

i18n-tutorial

So, suppose we wrote a plugin which is already working successfully. One way to make it more popular – is to add the ability to translate into other languages.

Internationalization and transliteration.

First, let’s define some terms. Translation of a ready plugins – this is a transliteration. When a plugin writing, we create the opportunity to translation – this is a internationalization.
In today’s lesson we will talk about internationalization. It should be noted that internationalization is sometimes referred to as i18n (in word “Internationalization” between ‘i’ and ‘n’ 18 letters), also the localization is sometimes referred to as l10n (because between ‘l’ and ‘n’ 10 letters).

Set Translation Headers.

The first step towards internationalization will be to add a translation headers. The translation headers are the Text Domain and the Domain Path. Text Domain used to refer to all the text belonging to the plugin. This is a unique identifier that allows WordPress to distinguish any downloaded translations. This allows you to save files and compact size provides better interaction with existing tools WordPress.
A simple example, compare two versions of a button settings:

<input type="submit" name="settingsUpdated" value="<?php _e('Save Changes') ?>" />

and

<input type="submit" name="settingsUpdated" value="<?php _e('Save Changes', 'my-plugin') ?>" />

In the first case, the plugin displays the text corresponding to the line “Save Changes” of WordPress. In the second case Set Text Domain, and the plugin will display the text you specified in the translate plugin.
Domain Path – is the directory where WordPress will search for files .mo. If Domain Path not specified, WordPress will search for .mo files in the root directory of the plugin.
A simple example Text Domain and Domain Path:
Text Domain: my-plugin
Domain Path: /languages/

Strings Translation.

To make a string translatable in your plugin, wrap the original string in a __() function call as follows:

$text =  __( 'Hello, User!', 'my-plugin' );

If you want to echo the string to the browser, instead of the echo language construct, use the _e function:

_e( 'Hello, User!', 'my-plugin' );

If you are using variables in strings like the example below, you should use placeholders.

$name = 'Donald';
printf(
    __( 'Your name is %s.', 'my-plugin' ),
    $name
);

Sometimes the string that changes when the number of items changes. You can use _n() function. This function accepts 4 arguments, namely:
Singular – the singular form of the string;
Plural – the plural form of the string;
Count – the number of objects;
Text Domain – the plugins text domain;

printf(
    _n(
        'One message',
        '%s messages',
        $messages,
        'my-plugin'
    ),
    $messages
);

Creating .PO and .MO files

After the strings are marked for translation in the source files, you can use a special tool xgettext. He will extract the original strings and create file POT (Portable Object Template).
Example strings in a file POT:

#: my-plugin/my-plugin.php:159
msgid "Your settings are saved"
msgstr ""

.PO file (Portable Object) has the same format, but also contains a translated string:

#: my-plugin/my-plugin.php:159
msgid "Your settings are saved"
msgstr "Ваші налаштування збережені"

From the translated .PO file using a suitable tool (eg Poedit), you can create .MO file (Machine Object) – binary file containing the original strings and their translations in a format suitable for rapid retrieval.

Tips for internationalization plugins:

  • Don’t translate literally, translate organically;
  • Try to keep the same level of formality (or informality);
  • Don’t use slang or audience-specific terms;
  • Use paragraph breaks – combine related phrases;
  • Use full phrases – in most languages ​​the word order differs from the use of English;
  • Use formatting strings instead of concatenation: sprintf(__(‘Replace %1$s with %2$s’), $a, $b); better than __(‘Replace ‘).$a.__(‘ with ‘).$b;
  • Your domain name, which is often reminiscent of the name of the plug-in, or coincide with it, should not contain underscores;

Happy coding!

Posted 1 year ago
Category: Tutorials
Tags:

If the article proved helpful to you, please click on one of the buttons to share it with your friends. Thanks!

Related posts:

puilo

Puilo (plugin for WordPress)

This plugin replaces the surname of Russian dictator Vladimir Putin in publications WordPress. In March 2014 this Ukrainian and Russian slogan was performed in its current form and [...]

No Comments

No comments yet.

RSS feed for comments on this post.

Leave a comment


Last Polls: