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
Need to do
__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 to Copy
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 the lang
folder.
Change your locale specified in wp-config.php
to reflect the translation. The default is define('WPLANG', '');
Sample plugin
/*
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
}