admin管理员组

文章数量:1122846

I have been handed an existing WP plugin for translation. I am using the WP __() function, which works fine in all cases, except for the following. (This is the relevant part of the code, of course, not all of it.)

    class Co_product_configurator_Public {

    private $ins_banister_right;
    private $ins_banister_left;
    private $ins_none;

    public function __construct( $plugin_name, $version ) {

        $this->ins_banister_right = __( "Rampe d'escalier à droite", 'co_product_configurator');
        $this->ins_banister_left =  __( "Rampe d'escalier à gauche", 'co_product_configurator');
        $this->ins_none = __( 'Aucun', 'co_product_configurator');
    }

    public function copc_get_banister_product_by_height( $height = ''){

        $select_html .= '<div class="copc_banister_field"><label>'. $this->ins_banister_left .'</label><select name="copc_banister_left">';
        $select_html .= '<option value="">'. $this->ins_none .'</option>';

        $select_html .= '<div class="copc_banister_field"><label>'. $this->ins_banister_right .'</label><select name="copc_banister_right">';
        $select_html .= '<option value="">'. $this->ins_none .'</option>';
    }


}

My .po file contains: (line numbers are accurate)

#: public/class-co_product_configurator-public.php:64
msgid "Rampe d'escalier à droite"
msgstr "Trapleuning rechts"

#: public/class-co_product_configurator-public.php:65
msgid "Rampe d'escalier à gauche"
msgstr "Trapleuning links"

#: public/class-co_product_configurator-public.php:66
msgid "Aucun"
msgstr "Geen"

Problem is, the original French strings are showing up, (msgid) not the translated variants (msgstr).

My .po file is functioning, as strings anywhere else in the plugin are correctly translated.

Any help would be appreciated!

I have been handed an existing WP plugin for translation. I am using the WP __() function, which works fine in all cases, except for the following. (This is the relevant part of the code, of course, not all of it.)

    class Co_product_configurator_Public {

    private $ins_banister_right;
    private $ins_banister_left;
    private $ins_none;

    public function __construct( $plugin_name, $version ) {

        $this->ins_banister_right = __( "Rampe d'escalier à droite", 'co_product_configurator');
        $this->ins_banister_left =  __( "Rampe d'escalier à gauche", 'co_product_configurator');
        $this->ins_none = __( 'Aucun', 'co_product_configurator');
    }

    public function copc_get_banister_product_by_height( $height = ''){

        $select_html .= '<div class="copc_banister_field"><label>'. $this->ins_banister_left .'</label><select name="copc_banister_left">';
        $select_html .= '<option value="">'. $this->ins_none .'</option>';

        $select_html .= '<div class="copc_banister_field"><label>'. $this->ins_banister_right .'</label><select name="copc_banister_right">';
        $select_html .= '<option value="">'. $this->ins_none .'</option>';
    }


}

My .po file contains: (line numbers are accurate)

#: public/class-co_product_configurator-public.php:64
msgid "Rampe d'escalier à droite"
msgstr "Trapleuning rechts"

#: public/class-co_product_configurator-public.php:65
msgid "Rampe d'escalier à gauche"
msgstr "Trapleuning links"

#: public/class-co_product_configurator-public.php:66
msgid "Aucun"
msgstr "Geen"

Problem is, the original French strings are showing up, (msgid) not the translated variants (msgstr).

My .po file is functioning, as strings anywhere else in the plugin are correctly translated.

Any help would be appreciated!

Share Improve this question edited May 29, 2024 at 17:10 Bart asked May 29, 2024 at 17:07 BartBart 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

I'm still not sure why this problem occured. I'm guessing the class loads before the Wordpress translation functions do.

I ended up circumventing the problem by using functions instead of variables:

    function ins_banister_right(): string {
    return __( "Rampe d'escalier à droite", 'co_product_configurator');
}
function ins_banister_left(): string {
    return __( "Rampe d'escalier à gauche", 'co_product_configurator');
}
function ins_none(): string {
    return __( "Aucun", 'co_product_configurator');
}

    $select_html .= '<div class="copc_banister_field"><label>'. $this->ins_banister_left() .'</label><select name="copc_banister_left">';
    $select_html .= '<option value="">'. $this->ins_none() .'</option>';

More details can be found in this post.

本文标签: translationStrings in class not being translated (All other strings are)