Support Forum
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);
Forgot to subscribe to replies...
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.
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);
?>
rwmb_the_value returns an unordered list if more than one is selected. Is the a way to return as an array only?
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
}
Thank you very much Anh! That did the job!