0

I'm using Poedit to create the .po and .mo files to translate the admin interface of a custom plugin.

All works fine, but I don't know where I've to store these new .po and .mo files to use these translations, how to rename the files and so on.

Please, could you help me?

Thanks

1

Excerpts from Plugin Handbook

The text domain must match the slug of the plugin. If your plugin is a single file called my-plugin.php or it is contained in a folder called my-plugin the domain name should be my-plugin. If your plugin is hosted on wordpress.org it must be the slug of your plugin URL (wordpress.org/plugins/). The text domain name must use dashes and not underscores. The text domain also needs to be added to the plugin header. WordPress uses it to internationalize your plugin meta-data even when the plugin is disabled. The text domain should be same as the one used when loading the text domain.

/*
 * Plugin Name: My Plugin
 * Author: Plugin Author
 * Text Domain: my-plugin
 */

The domain path is used so that WordPress knows where to find the translation when the plugin is disabled. Only useful if the translations are located in a separate language folder. For example, if .mo files are located in the languages folder within your plugin then Domain Path will be “/languages” and must be written with the first slash. Defaults to the languages folder of the plugin:

/*
 * Plugin Name: My Plugin
 * Author: Plugin Author
 * Text Domain: my-plugin
 * Domain Path: /languages
 */

Answer

Let's assume that your plugin name is My Plugin and your translation is for Polish language. There will be three files: my-plugin.pot, my-plugin-pl_PL.po, and my-plugin-pl_PL.mo.

It is a good practice to put in your plugin's languages folder, one .pot file, and pairs of .po / .mo for each language.

Put the following files, my-plugin.pot, my-plugin-pl_PL.po, my-plugin-pl_PL.mo into '/wp-content/plugins/my-plugin/languages' folder.

The following code will be used to properly load your plugin text domain:

function my_init() {
    load_plugin_textdomain( 'my-plugin', false, 'my-plugin/languages/' );
}
add_action( 'init', 'my_init' );

or, if you call it from a class:

function my_init() {
    load_plugin_textdomain( 'my-plugin', false, 'my-plugin/languages/' );
}
add_action( 'init', array( $this, 'my_init' ) );
  • Wow, thanks so much, I'll try it right now and then I'll back to you! – br0k3n Jun 28 '17 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.