Ask your WordPress questions! Pay money and get answers fast! (more info)

load_plugin_textdomain WordPress

  • SOLVED

i write a plugin
i want to internationalize this plugin
so, i use this king of code :
sprintf( __('Replacement of %d by %d in', 'wpduplicateblog'), $search, $replace )
or
__('Finished replacement', 'wpduplicateblog')

my plugin is a folder (no subfolders)
in my folder i have two files for languages :
wpduplicateblog-fr_FR.mo
wpduplicateblog-fr_FR.po

i have create this files with poedit. This files contains all translation in french.


I use the function load_plugin_textdomain in my plugin but i think that there is an error because the content of my plugin is not translated.
Could you write the function i must to use in my plugin to translate it, please.

Thanks

Answers (1)

2012-12-02

Dbranes answers:

Hi, I use it fx. like this in a plugin class:

function __construct(){
add_action('plugins_loaded', array(&$this,'i18n'));
//some more stuff
}


where the class has this function

function i18n() {
load_plugin_textdomain( 'myplugindomain', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/');
}


and the language files are in a subfolder called "/languages/" inside the 'myplugin' folder.

I can then use the usual underscore functions to translate:

<?php _e("Some text","myplugindomain");?>
<?php echo __("Some text","myplugindomain");?>


Sébastien | French WordpressDesigner comments:

could you show all your code ?
i try your code but that doesn't work


Dbranes comments:

this works for me:

if(class_exists('MyPlugin')){
new MyPlugin();
}

class MyPlugin {
function __construct(){
add_action('plugins_loaded', array(&$this,'i18n'));
add_filter('the_content', array(&$this,'test_translation'));
}
function i18n() {
load_plugin_textdomain( 'myplugindomain', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/');
}
function test_translation($content) {
return $content."<br/>test my translation:".__('Some translated string','myplugindomain');
}

} // end class


and in the wp_config.php file I select the language to use:

define('WPLANG', 'fr_FR');


and in the subfolder "/wp-content/plugins/myplugin/languages/" I have the files "myplugindomain-fr_FR.mo" and "myplugindomain-fr_FR.po".


Sébastien | French WordpressDesigner comments:

at the top of my plugin i use your code like this :
if(class_exists('MyPlugin')){
new MyPlugin();
}
class MyPlugin {
function __construct(){
add_action('plugins_loaded', array(&$this,'i18n'));
add_filter('the_content', array(&$this,'test_translation'));
}
function i18n() {
load_plugin_textdomain( 'wpduplicateblog', false, dirname( plugin_basename( __FILE__ ) ) . '/');
}
function test_translation($content) {
return $content."<br/><br/><br/><br/><br/><br/>test my translation:".__('Create a clone','wpduplicateblog');
}
} // end class

in the admin no result. I don't see the sentence ($content)


Sébastien | French WordpressDesigner comments:

i use wp multisite, is it important ?


Dbranes comments:

Maybe you could test this demo plugin code:

Create this folder

/wp-content/plugins/translationtest/


with the file "translationtest.php" with this code

<?php
/*
Plugin Name: Translation Test
Description: Translation Test plugin
Author: Dbranes
Version: 1.0
Text Domain: wpduplicateblog
*/

if(class_exists('TranslationTest')){
new TranslationTest();
}

if(!class_exists('TranslationTest')){
class TranslationTest {
function __construct(){
add_action('plugins_loaded', array(&$this,'i18n'));
add_filter('the_content', array(&$this,'test_translation'));
}
function i18n() {
load_plugin_textdomain( 'wpduplicateblog', false, dirname( plugin_basename( __FILE__ ) ) . '/');
}
function test_translation($content) {
return $content."<br/>test my translation:".__('Create a clone','wpduplicateblog');
}
} // end class
}


where your files wpduplicateblog-fr_FR.mo and wpduplicateblog-fr_FR.po are inside "/wp-content/plugins/translationtest/".

I have tested this on a multisite (both on where the plugin is activated for the whole network and activated per site) and it works.

(I see the translated text after each content post)

Maybe you can send me your language files to test?

cheers


Sébastien | French WordpressDesigner comments:

thanks
i have try your code and that doesn't work on my site
i send you my files by PM
thx


Dbranes comments:

ps: just for the reference to guide others in similar problems, this seems to work on multisite

add_action('init','my_i18n');
function my_i18n(){
load_plugin_textdomain( 'wpduplicateblog', false, dirname( plugin_basename( __FILE__ ) ) . '/');
}


The debug version to check if the .mo file is found and loaded:

add_action('init','my_i18n_debug');
function my_i18n_debug(){

$loaded=load_plugin_textdomain( 'wpduplicateblog', false, dirname( plugin_basename( __FILE__ ) ) . '/');

if ( ! $loaded ){
echo "<hr/>";
echo "Error: the mo file was not found! ";
exit();
}else{
echo "<hr/><strong>Debug info</strong>:<br/>";
echo "WPLANG: ". WPLANG;
echo "<br/>";
echo "translate test: ". __('Some text','wpduplicateblog');
exit();
}
}


where 'wpduplicateblog' is the text-domain for the plugin and the .mo files are stored in the same folder in this case.


best regards