0

I'm implementing a gender field in a custom registration form.

The gender field also exists as a user field, so I'd like to call the label and allowed values from the existing instance. I've written it as a conditional override just in case...:

$form['gender'] = array(
    '#title' => t('Gender'),
    '#type' => 'radios',
    '#options' => array('female' => t('Female'), 'male' => t('Male')),
    '#required' => TRUE,
);
if($field = field_info_field('field_gender')) {
    $form['gender']['#options'] = $field['settings']['allowed_values'];
    $info = field_info_instance('user', 'field_gender', 'user');
    $form['gender']['#title'] = t($info['label']);   
}

Everything's working great, except for the multilingual part: despite having translated the field instance at the admin account settings using i18n, the extracted options and title of the existing field are not being translated upon changing the front-end language. They DO get translated however when visiting the user account page in a different language.

It seems my custom form doesn't send its strings through i18n...?

Can anyone point me out the pitfall please?

Thanks

UPDATE

I used

$func = function($val) { return t($val); };
$form['gender']['#options'] = array_map($func, $field['settings']['allowed_values']);

as suggested by Shameem to translate the options. But now it seems t() is fetching translations from the Locale module repository. How to direct t() to fetch it from i18n?

2 Answers 2

0

I managed to set up a working solution. Could somebody possibly point out please if it is actually good practice to use those i18n functions per direct in the code?

    $form['gender'] = array(
    '#title' => t('Gender'),
    '#type' => 'radios',
    '#options' => array('female' => t('Female'), 'male' => t('Male')),
    '#required' => TRUE,
);
if($field = field_info_field('field_gender')) {
    $instance = field_info_instance('user', 'field_gender', 'user');    
    $form['gender']['#title'] = 
        module_exists('i18n_field') ? i18n_field_translate_property($instance, 'label') : t($instance['label']);   
    // Define this t() function for the array_map just in case i18n_field isn't enabled.
    $func = function($val) {
        return t($val);
    };
    $form['gender']['#options'] = 
        module_exists('i18n_field') ? i18n_field_translate_allowed_values($field) : array_map($func, $field['settings']['allowed_values']);
}
0

the field options are not going through the t function.

you would need to take each value from the following array and make it go through the t() function

$field['settings']['allowed_values'];

and please check that $info['label'] is a plain text and not an array.

1
  • Hi Shameem. I used array_map to apply t() to each of the options and it's working now. Thanks! However, I now discovered that t() is fetching its translations from the Locale repository (instead of the field instance translations which are being handled by i18n_field). This also explains the fact that $info['label'] is not being translated. So an update to my question: how do I point t() to use the field instance translation settings instead of the global Locale translations?? Jan 16, 2013 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.