Show translated content
- This topic has 6 replies, 2 voices, and was last updated 6 years, 7 months ago by
Ale.
-
AuthorPosts
-
September 5, 2018 at 6:24 PM #11256
Ale
ParticipantHello, 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);
September 5, 2018 at 7:19 PM #11257Ale
ParticipantForgot to subscribe to replies...
September 6, 2018 at 8:37 AM #11267Anh Tran
KeymasterHi 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 usingrwmb_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.
September 8, 2018 at 10:48 AM #11281Ale
ParticipantThanks 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); ?>
September 8, 2018 at 11:36 AM #11282Ale
Participantrwmb_the_value returns an unordered list if more than one is selected. Is the a way to return as an array only?
September 8, 2018 at 11:50 AM #11283Anh Tran
KeymasterHi Ale,
To get the values in an array, you have to use
rwmb_meta
orrwmb_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 }
September 8, 2018 at 12:06 PM #11284Ale
ParticipantThank you very much Anh! That did the job!
-
AuthorPosts
- You must be logged in to reply to this topic.