Support

Account

Home Forums General Issues translate choices of radio/select/checkbox fields

Solved

translate choices of radio/select/checkbox fields

  • How do I translate the choices for a raido/select/checkbox field.

    I’ve translated the field_label, left the field_name the same in both languages.
    Field type = select.
    No I need to translate the choices
    In English I have:
    Short
    medium
    long

    On the Hebrew translation should I translate this?
    קצר
    בינוני
    ארוך
    How does ACF know which translation is for which term. Is the order important here?

    If I save this and look at a Hebrew post I still don’t see the translation, only the English.
    How do I translate the choice correctly?

  • Hi @renar

    Could you please test the field object on the translated page by using this code:

    echo "<pre>";
    $select_object = get_field_object('select_field_name', 99);
    var_dump($select_object);
    echo "</pre>";

    Where ’99’ is the ID of the post/page that has the custom field.

    I believe you should add the choices in English like this:

    short : Short
    medium : Medium
    long : Long

    and like this in Hebrew:

    short : קצר
    medium : בינוני
    long : ארוך

    After that, you can get the label based on the value like the example on this page: https://www.advancedcustomfields.com/resources/select/.

    Thanks 🙂

  • I did var_dump and it returned:

    array(22) {
      ["ID"]=>
      int(29)
      ["key"]=>
      string(19) "field_56d2d662eda3d"
      ["label"]=>
      string(11) "I work with"
      ["name"]=>
      string(11) "i_work_with"
      ["prefix"]=>
      string(0) ""
      ["type"]=>
      string(8) "checkbox"
      ["value"]=>
      array(2) {
        [0]=>
        string(7) "Couples"
        [1]=>
        string(3) "Men"
      }
      ["menu_order"]=>
      int(15)
      ["instructions"]=>
      string(0) ""
      ["required"]=>
      int(1)
      ["id"]=>
      string(0) ""
      ["class"]=>
      string(0) ""
      ["conditional_logic"]=>
      int(0)
      ["parent"]=>
      int(8)
      ["wrapper"]=>
      array(3) {
        ["width"]=>
        string(0) ""
        ["class"]=>
        string(0) ""
        ["id"]=>
        string(0) ""
      }
      ["_name"]=>
      string(11) "i_work_with"
      ["_input"]=>
      string(0) ""
      ["_valid"]=>
      int(1)
      ["choices"]=>
      array(9) {
        ["Children"]=>
        string(8) "Children"
        ["Adolescents"]=>
        string(11) "Adolescents"
        ["Adults"]=>
        string(6) "Adults"
        ["Couples"]=>
        string(7) "Couples"
        ["Families"]=>
        string(8) "Families"
        ["Men"]=>
        string(3) "Men"
        ["Women"]=>
        string(5) "Women"
        ["Geriatric"]=>
        string(9) "Geriatric"
        ["Other"]=>
        string(5) "Other"
      }
      ["default_value"]=>
      array(0) {
      }
      ["layout"]=>
      string(8) "vertical"
      ["toggle"]=>
      int(0)
    }

    The translation is working for the ‘edit post’ but when I view the page it still only shows the English.
    How do I make it show the Hebrew on the hebrew page?

    I’ve set the choice in the field to:
    Children : יְלָדִים
    Adolescents : מתבגר
    Adults : מבוגרים
    Couples : זוגות
    Families : משפחות
    Men : גברים
    Women : נשים
    Geriatric : גֵרִיאַטרִי
    Other : אחר

    I’m using the_field(‘i_work_with’);
    Do I need to use something different for a translation language?
    The field_name is the same in both languages…

  • Hi @renar

    The the_field() function will only show the value of the selected choice, not the label. Could you please check the var_dump() result on the Hebrew page? Please keep in mind that the translated page has a different ID than the original page. If the choices show the Hebrew label, then you can show the label like this:

    <?php
    $field = get_field_object('field_name');
    $value = get_field('field_name');
    $label = $field['choices'][ $value ];
    ?>
    
    <p>Color: <?php echo $label; ?></p>

    Please check the link I gave you before to learn more about it.

    If it doesn’t show the Hebrew label, could you please var_dump() it without the ID of the page like the following?

    echo "<pre>";
    $select_object = get_field_object('select_field_name');
    var_dump($select_object);
    echo "</pre>";

    If still no joy, I’m afraid you need to translate it by using WordPress localization functions. This page should give you more idea about it: https://codex.wordpress.org/I18n_for_WordPress_Developers.

    Hope this helps 🙂

  • Thank you, this was helpful and I can get the Hebrew showing now.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.