I have completed my plugin, now want to provide a multilingual features for my users. I goggled about it, but it's hard to implement.
I've seen WordPress translation but need basic steps to follow and translate my plugin.
I have done these
- downloaded POEdit.
- created 'french.po' file in plugin dir
- complied 'french.po' -> 'french.mo'
Need to do
- How to define msgid & msgstr in po file?
- How to load po/mo file in plugin?
- How to replace labels/text through po/mo file?
- how to use
__e()
&___()
to replace 'msgstr' in plugin pages?
With a plugin called, Codestyling Localization, you don't need to use POEdit.
I'll show you an example of using 'localizationsample' as the text domain. In this case, the language files are in the /lang/ directory. They don't need to be those names in your actual plugin; they are just examples.
Steps
Add these lines in the plugin comment header to be recognized by Codestyling Localization.
Text Domain: localizationsample
Domain Path: /lang
Create a directory named
lang
in your plugin directory.Install and activate the Codestyling Localization plugin.
Go to
Tools
->Localization
Find your plugin and click on
Add New Language
Select the language (country) to localize in the radio button and press
Create po-file
At this point make sure a .po file is created in the lang folder.Press
Rescan
->scan now
This is recommended since in my system without doing this, the plugin always shows an error saying "not all items are using the same text domain."Press
Edit
This will bring you another page listing the messages available to be translated. Those messages are the ones passed to the functions__()
and_e()
in the plugin code.Click on
Edit
in the table next toCopy
then you'll get a dialog box to type your translation for each message. Finish translating.Press
generate mo-file
At this point, you should see a .mo file being created in thelang
folder.Change your locale specified in
wp-config.php
to reflect the translation. The default isdefine('WPLANG', '');
Sample plugin
(THIS EXAMPLE IS A TRANSLATION to DEUTCH. YOU CAN CHANGE the customs to YOUR DESIRED names.)
in every plugins head, there is an unique name. (for example:
then, in that plugin's folder, create a folder "languages";
then, into your plugin .php file (somewhere in the top), insert the initialization code:
then open any text editor, then insert like this code (NOTE, THAT we are only adding two sample messages, "hello" and "bye", so , you can ADD AS MANY messages AS YOU WANT with the similar lines).
then save this file as "my-pluginname-en_US.po" (note, that .po is an extension of file, so check that your text editor program has not saved to "my-pluginname-en_US.po.TXT").
then download POEDIT software, and open this file. then edit the "translation" field, and then save as "my-pluginname-de_DE" there will be generated two files ( If poEdit does not generate the second .mo file automatically, just go to File -> Preferences -> Editor and check the box that says "Automatically compile .mo file on save"),
then put those two file into "languages" folder.
after this, open wp-config.php and find this code:
and change to
thats all. when wordperss is loaded, it will read your plugins language file, with prefix -de_DE.
so, in the plugin's .php file, instead of:
you should use:
Finished. Now you should test your plugin.
p.s.used 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