最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

translation - I can't translate text in my plugin using a .mo file, load_plugin_textdomain() function is always false

matteradmin6PV0评论

I create a plugin just for testing

/**
 * @package SurveyPlugin
 * @version 1.0
 */
/*
Plugin Name: Survey Plugin
Plugin URI: http:marcogomesweb
Description: This is survey
Author: NodeLondon
Version: 1.0
License: GPLv2 or later
Text Domain: survey-plugin
*/

function get_dummy_text() {
    $text = [
        __( 'city hotels' , 'survey-plugin' )
    ];
    return $text[0];
}

add_action('admin_notices', 'show_dummy_text');

function show_dummy_text(){
    $text = get_dummy_text();
    global $locale;
    echo "<br>". $locale . "<br>";
    echo "<p id='wp-admin-motivation'>$text</p>";
}


add_action( 'plugins_loaded', 'wan_loaded_textdomain');


function wan_loaded_textdomain(){

     $loadfiles = load_plugin_textdomain('survey-plugin', false, 
     dirname( plugin_basename( __FILE__ ) ) . '/languages/' );

     plugin_basename( __FILE__ ). '/languages/' );



     var_dump($loadfiles); // is always false


}

I install Poedit I create a .mo file survey-plugi-es_ES.mo in /plugins/survey-plugin/languages/

survey-plugi-es_ES.mo

msgid ""
msgstr ""
"Project-Id-Version: Survey Plugin\n"
"POT-Creation-Date: 2019-03-15 14:16+0000\n"
"PO-Revision-Date: 2019-03-15 14:16+0000\n"
"Language-Team: Marcogomesr\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 2.2.1\n"
"X-Poedit-Basepath: ..\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Poedit-KeywordsList: __\n"
"Last-Translator: \n"
"Language: es\n"
"X-Poedit-SearchPath-0: .\n"
"X-Poedit-SearchPath-1: survey-plugin.php\n"
"X-Poedit-SearchPathExcluded-0: src\n"
"X-Poedit-SearchPathExcluded-1: dist\n"

#: survey-plugin.php:79
msgid "city hotels"
msgstr "ciudad"

and this is what echo out

after changing the languages in the settings / site languages still showing the same,

I'm following this tutorial but still having no idea why

I create a plugin just for testing

/**
 * @package SurveyPlugin
 * @version 1.0
 */
/*
Plugin Name: Survey Plugin
Plugin URI: http:marcogomesweb
Description: This is survey
Author: NodeLondon
Version: 1.0
License: GPLv2 or later
Text Domain: survey-plugin
*/

function get_dummy_text() {
    $text = [
        __( 'city hotels' , 'survey-plugin' )
    ];
    return $text[0];
}

add_action('admin_notices', 'show_dummy_text');

function show_dummy_text(){
    $text = get_dummy_text();
    global $locale;
    echo "<br>". $locale . "<br>";
    echo "<p id='wp-admin-motivation'>$text</p>";
}


add_action( 'plugins_loaded', 'wan_loaded_textdomain');


function wan_loaded_textdomain(){

     $loadfiles = load_plugin_textdomain('survey-plugin', false, 
     dirname( plugin_basename( __FILE__ ) ) . '/languages/' );

     plugin_basename( __FILE__ ). '/languages/' );



     var_dump($loadfiles); // is always false


}

I install Poedit I create a .mo file survey-plugi-es_ES.mo in /plugins/survey-plugin/languages/

survey-plugi-es_ES.mo

msgid ""
msgstr ""
"Project-Id-Version: Survey Plugin\n"
"POT-Creation-Date: 2019-03-15 14:16+0000\n"
"PO-Revision-Date: 2019-03-15 14:16+0000\n"
"Language-Team: Marcogomesr\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 2.2.1\n"
"X-Poedit-Basepath: ..\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Poedit-KeywordsList: __\n"
"Last-Translator: \n"
"Language: es\n"
"X-Poedit-SearchPath-0: .\n"
"X-Poedit-SearchPath-1: survey-plugin.php\n"
"X-Poedit-SearchPathExcluded-0: src\n"
"X-Poedit-SearchPathExcluded-1: dist\n"

#: survey-plugin.php:79
msgid "city hotels"
msgstr "ciudad"

and this is what echo out

after changing the languages in the settings / site languages still showing the same,

I'm following this tutorial but still having no idea why

Share Improve this question edited Mar 21, 2019 at 0:02 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Mar 15, 2019 at 14:27 MarcogomesrMarcogomesr 1313 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 2

(Revised answer)

The simple answer is because WordPress could not find the MO file, despite the file itself exists (in the plugin's languages directory).

And here's why: The MO file needs to be named following this format: {text-domain}-{locale}.mo, and {text-domain}-{locale}.po for the PO file. See Loading Text Domain for further information.

So you're trying to load the MO file for the survey-plugin domain:

load_plugin_textdomain( 'survey-plugin', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' )

Hence for the es_ES locale (Español),

  • The MO file name has to be survey-plugin-es_ES.mo.

  • The PO file name has to be survey-plugin-es_ES.po.

And I think you may not be aware that the POT (translation template) file needs be named like so: {text-domain}.pot? E.g. survey-plugin.pot in your case.

Or was the survey-plugi just a typo? :)

But even if so, I hope this answer helps (other developers/readers).

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far