How to translate WordPress plugin into any language?

I completed my plugin, now I want to provide multilingual functions for my users. I studied this, but it is difficult to implement.

I saw the translation of WordPress, but you need the basic steps to follow and translate my plugin.

I did it

  1. loaded POEdit.
  2. created the file "french.po" in the plugin
  3. performed 'french.po' β†’ 'french.mo'

Need to do

  1. How to determine the MSGID in the output of a ro file?
  2. How to load a po / mo file in a plugin?
  3. How to replace shortcuts / text through po / mo file?
  4. how to use __e() & ___() to replace "msgstr" on plugin pages?
+5
source share
2 answers

With the Codestyling Localization plugin connected , you do not need to use POEdit.

I will show you an example of using 'localizationsample' as a text domain. In this case, the language files are located in the / lang / directory. They do not have to be these names in your real plugin; these are just examples.

Steps

  • Add these lines to the comment header of the plugin that will be recognized by Codestyling Localization.

    Text domain: localizationsample
    Domain Path: / lang

  • Create a directory called lang in your plugins directory.

  • Install and activate the Codestyling Localization plugin.

  • Go to Tools Localization

  • Find your plugin and click Add New Language

  • Select the language (country) to localize in the switch and click Create po-file . At this point, make sure the .po file is created in the lang folder.

  • Click Rescan β†’ scan now This is recommended because on my system, without doing this, the plugin always shows an error saying that "not all elements use the same text domain."

  • Click Edit This will result in messages available for translation on another page. These messages are passed to the __() and _e() functions in the plugin code.

  • Click Edit in the table next to Copy , then you will get a dialog box for entering a translation for each message. Complete the translation.

  • Click generate mo-file . At this point, you should see the .mo file created in the lang folder.

  • Change your language specified in wp-config.php to reflect the translation. The default is define('WPLANG', '');

Plugin example

 /* Plugin Name: Sample Localization Description: Demonstrates how to localize your plugin. Text Domain: localizationsample Domain Path: /lang */ // Localization add_action('init', 'localizationsample_init'); function localizationsample_init() { $path = dirname(plugin_basename( __FILE__ )) . '/lang/'; $loaded = load_plugin_textdomain( 'localizationsample', false, $path); if ($_GET['page'] == basename(__FILE__) && !$loaded) { echo '<div class="error">Sample Localization: ' . __('Could not load the localization file: ' . $path, 'localizationsample') . '</div>'; return; } } // Add Admin Menu add_action('admin_menu','localizationsample_menu'); function localizationsample_menu() { add_options_page( 'Localization Demo', // admin page title 'Localization Demo', // menu item name 'manage_options', // access privilege basename(__FILE__), // page slug for the option page 'localization_demo_adminpanel' // call-back function name ); } function localization_demo_adminpanel() { echo '<div class="wrap"><div id="icon-themes" class="icon32"></div>'; echo '<h2>' . __('Hi there!', 'localizationsample') . '</h2>'; echo '<p>'; _e('Hello world!', 'localizationsample'); echo '</p>'; echo '</div>'; // end of wrap } 
+14
source

(THIS EXAMPLE IS A TRANSLATION WHICH YOU CAN CHANGE TRADITIONS FOR YOUR ART.)

Each plugin chapter has a unique name. (eg:

 /* Plugin Name: my-pluginname ....... */ 

then in the plugin folder create the β€œLanguages” folder;

then, in your plugin's .php file (somewhere on top), paste the initialization code:

 class load_language { public function __construct() { add_action('init', array($this, 'load_my_transl')); } public function load_my_transl() { load_plugin_textdomain('my-pluginname', FALSE, dirname(plugin_basename(__FILE__)).'/languages/'); } } $zzzz = new load_language; 

then open any text editor, then paste like this code (NOTE: The fact that we add only two examples of messages, "hello" and "bye", so that you can ADD AS MANY messages as YOU WANT with similar lines).

 # English translations for PACKAGE package. # Copyright (C) 2012 THE PACKAGE COPYRIGHT HOLDER # This file is distributed under the same license as the PACKAGE package. # Automatically generated, 2012. # msgid "" msgstr "" "Project-Id-Version: my-pluginname 1.0\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2012-08-06 13:46-0400\n" "PO-Revision-Date: 2013-03-21 11:20+0400\n" "Last-Translator: Automatically generated\n" "Language-Team: none\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" "X-Poedit-SourceCharset: iso-8859-1\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n" "X-Generator: Poedit 1.5.4\n" #: mypluginindex.php:87 (it is just a line of a note, to remind where our code appears) msgid "mymessage1" msgstr "Hello" #: mypluginindex.php:88 msgid "mymessage2" msgstr "Bye" 

then save this file as "my-pluginname-en_US.po" (note that .po is a file extension, so make sure your text editor program has not been saved to "my-pluginname-en_US.po.TXT").

then download the POEDIT software and open this file. then edit the "translation" field and then save it as "my-pluginname -de_DE", two files will be created (if poEdit does not create a second file .mo automatically, just go to "File" β†’ "Settings" β†’ "Editor" and install checkbox "Automatically compile .mo file when saving"),

then put these two files in the Languages ​​folder.

after that open wp-config.php and find this code:

 define ('WPLANG, ''); 

and change to

 define ('WPLANG, 'de_DE'); 

it's all. when wordperss loads, it will read your plugin language file with the -de_DE prefix.

therefore, in the plugin file.php instead:

 echo "Something string"; 

you should use:

 echo __("mymessage1", 'my-pluginname'); 



Finished. Now you should check your plugin.
psused links: https://codex.wordpress.org/I18n_for_WordPress_Developers
http://codex.wordpress.org/Translating_WordPress
https://codex.wordpress.org/Writing_a_Plugin
http://codex.wordpress.org/Installing_WordPress_in_Your_Language

+5
source

Source: https://habr.com/ru/post/1436782/


All Articles