Custom Fields
Re-Envisioned
Support General Show translated content

This topic contains 6 replies, has 2 voices, and was last updated by  Ale 1 year, 1 month ago.

  • Creator
    Topic
  • #11256
    Resolved
    Ale
    Participant

    Hello, I have a select advanced that gives as options several languages. I am using esc_html__ to translate the key and value. The translation is accesible on the .po file but I can’t display it in the page, in page only shows English language.

    The metabox:

    array(
        'id'      => $prefix . 'languages',
        'name'    => esc_html__( 'Languages', 'yanse' ),
        'type'    => 'select_advanced',
        'options' => array(
            esc_html__( 'English', 'yanse' )    => esc_html__( 'English', 'yanse' ),
            esc_html__( 'Spanish', 'yanse' )    => esc_html__( 'Spanish', 'yanse' ),
            esc_html__( 'Japanese', 'yanse' )   => esc_html__( 'Japanese', 'yanse' ),
            esc_html__( 'Korean', 'yanse' )     => esc_html__( 'Korean', 'yanse' ),
        ),
        'multiple' => true,
        //'required'  => true,
        'std' => array( 'English', 'Chinese' ),
    ),

    This is how I display the fields.

    $valueArray  = rwmb_meta( 'mb_languages', array( 'multiple' => true) );
    $pieces = array();
    
    foreach ($valueArray as $item) {
        $pieces [] = $item;
    }
    echo implode(', ', $pieces);
Viewing 6 replies - 1 through 6 (of 6 total)
  • Author
    Replies
  • #11257

    Ale
    Participant

    Forgot to subscribe to replies…

    #11267

    Anh Tran
    Keymaster

    Hi Ale,

    This is a great question!

    Firstly, I think you should translate the field labels only. Translating the values makes it’s very hard to control what is saved in the database. And the data in the database should not be user’s concern, it’s for developers only.

    Secondly, if you use rwmb_meta, you’ll get the field value stored in the database. It’s static and can’t be changed. That’s why you don’t see the translated text. I’d suggest using rwmb_the_value to get the option labels, which meaningful to users and translatable. See the field documentation for details.

    Finally, make sure you load the text domain for the code of meta boxes.

    #11281

    Ale
    Participant

    Thanks ANh.

    You are right about not translating both.
    Thanks for the link to rwmb_the_value.

    A way I found was to use esc_html__ for each array item:

                                 <?php
                                    $valueArray  = rwmb_meta( 'mb_languages', array( 'multiple' => true) );
                                    $pieces = array();
    
                                    foreach ($valueArray as $item) {
                                        $pieces [] = esc_html__(  $item,  $domain = 'yanse' );
    
                                    }
                                    echo implode(', ', $pieces);
                                    ?>
    #11282

    Ale
    Participant

    rwmb_the_value returns an unordered list if more than one is selected. Is the a way to return as an array only?

    #11283

    Anh Tran
    Keymaster

    Hi Ale,

    To get the values in an array, you have to use rwmb_meta or rwmb_get_value. Then parse the field options to output the labels:

    $field_id = 'mb_languages';
    $field = rwmb_get_field_settings( $field_id );
    $options = $field['options'];
    
    $values = rwmb_meta( $field_id );
    foreach ( $values as $value ) {
        echo $options[$value]; // Label
    }
    #11284

    Ale
    Participant

    Thank you very much Anh! That did the job!

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

You must be logged in to reply to this topic.