Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter amplifiction

    (@amplifiction)

    @threadi Indeed, option B is doable but not ideal. The use of functions is a good idea, though. I went with that:

        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>';
    

    (Side question: why do many developers include “public” in the definition of their functions? Isn’t that default?)

    As to why the class loads (too) early: I had previously fiddled with the order in which the relevant parts of the code load, but to no avail. This is what I settled on. (This is in co_product_configurator.php, which is the “main” .php file.)

    add_action('plugins_loaded', 'wan_load_textdomain');
    function wan_load_textdomain() {
    	load_plugin_textdomain( 'co_product_configurator', false, dirname( plugin_basename(__FILE__) ) . '/lang/' );
    }
    
    function run_co_product_configurator() {
    	$plugin = new Co_product_configurator();
    	$plugin->run();
    }
    run_co_product_configurator();

    As you may have guessed, I am a beginner and don’t know where to look beyond that. What counts is that the translation is working, so I am satisfied. Thank you for your help, @threadi !

    Thread Starter amplifiction

    (@amplifiction)

    @threadi Thanks for the suggestion, but even without special characters, the strings don’t get translated. $ins_none is an example of that.

    Furthermore, replacing the variables with their content results in correct translations:

    // WORKS:
    
            $select_html .= '<div class="copc_banister_field"><label>'. __( "Rampe d'escalier à gauche", 'co_product_configurator') .'</label><select name="copc_banister_left">'; // $this->ins_banister_left
            $select_html .= '<option value="">'. __( 'Aucun', 'co_product_configurator') .'</option>'; // $this->ins_none
    
    // DOESN'T WORK:
    
            $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>';
    

    So there must be something wrong with the variables: with their declaration, initialization and/or usage. To reiterate:

    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');
        }

    I would prefer to use variables because the strings they contain appear several times in the code, and I require a translation to multiple languages.

    Elsewhere in the plugin, I use variables in (almost) the same way, with the key difference that it’s outside of a class. These variables get translated succesfully.

    $msg_choose_height = __( 'Saisissez une hauteur comprise entre 700 mm et 4300 mm', 'co_product_configurator' );
    $msg_choose_step = __( 'Choisir un type de démarche', 'co_product_configurator');
    $msg_choose_width = __( 'Choisissez la largeur des marches pour continuer.', 'co_product_configurator');
    $msg_choose_banister = __( "Veuillez sélectionner une rampe d'escalier", 'co_product_configurator');
    
    // Example of using these variables:
    
                else if(jQuery(this).parent().hasClass('copc_step_4') && jQuery('select[name="copc_select_sizes"]').val() == ''){
                    var msg__ = '';
                    if(jQuery('input[name="copc_max_height"]').val() == ''){
                        msg__ = '<?php echo $msg_choose_height; ?>';
                    }
                    else if(jQuery('input[name="copc_select_type"]:checked').length == 0){
                        msg__ = '<?php echo $msg_choose_step; ?>';
                    }
                    else{
                        msg__ = '<?php echo $msg_choose_width; ?>';
                    }
                    alert(msg__); return;
                }
    

    So there seems to be something tricky about the use of variables in a class.

Viewing 2 replies - 1 through 2 (of 2 total)