Hi,
I've added some CCK seleclist fields to my site.
They have the following allowed values:

0|Attorney
1|Associate
2|Intern

How can I translate the textual values?
In drupal 6 you had the option to write PHP code for allowed values, so in that way translation was available (For more details see http://drupal.org/node/182884#comment-280925).
But I don't see an equivalent field in drupal 7...
Neither can i find any other issues that solves this problem.

Does anybody have a clue?

Comments

rantebi’s picture

After searching and posting in many blogs i found no answers. this is the solution I found:

/**
 * Implements hook_field_formatter_info().
 */
function my_module_field_formatter_info() {
  return array(
    'list_default_t' => array(
      'label' => t('Default with localization'),
      'field types' => array('list_integer', 'list_float', 'list_text', 'list_boolean'),
    ),
  );
}

/**
 * Implements hook_field_formatter_view().
 */
function my_module_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  switch ($display['type']) {
    case 'list_default_t':
      $allowed_values = list_allowed_values($field);
      foreach ($items as $delta => $item) {
        if (isset($allowed_values[$item['value']])) {
          $output = t(field_filter_xss($allowed_values[$item['value']]));
        }
        else {
          // If no match was found in allowed values, fall back to the key.
          $output = t(field_filter_xss($item['value']));
        }
        $element[$delta] = array('#markup' => $output);
      }
      break;
  }

  return $element;
}

This is copied almost exactly from the list core module, I just added the t function.
Usually you better not translate with t dynamic expressions but I was a bit lazy -> does anybody have a better way?

Instructions:
1. Add this code to any of your enabled modules.
2. You would then have to select the "Default with localization" in your content-type's manage display section.
3. The terms you support in your field should be translated and imported to the translation interface under "Built-in interface".

franknecklace’s picture

good post.

jason.fisher’s picture

hook_form_alter would let you adjust #options values less invasively. You might instead store a list of "translatable" content types as a variable, with eventual configuration with a setting on the content type itself. Then the fields could remain untouched.

czigor’s picture

The D7 version of cck module lets you write PHP code for allowed_values, just like in D6.

iongion’s picture

"D7 version of cck module lets you write PHP code for allowed_values"

- where/how do you see that ?!?

Norberto Ostallo’s picture

You have to implement a hook in a custom module.

joetsuihk’s picture

in case anyone up to this, there are two methods to provide:
1. install D7 cck module, and you have new textarea for PHP code, and also a textfield to supply a function name for that array.
2. use hook_field_info_alter, there is a allowed_values_function, you can supply that function name also

p.s. I am using first method, on cck 7.x dev 2011-Aug-23

yul63’s picture

Took me hours to find. Though it's for Fields, it might work with CCK.

First you need to translate the values in the field form (/admin/structure/types/manage/MYNODETYPE/fields/MYFIELD/translate). Then go to "Manage display" (/admin/structure/types/manage/MYNODETYPE/display) and set the format of MYFIELD to "Default translated".

See http://drupal.org/node/1218880

aquadrop’s picture

... it worked like a charm. I was wondering why field names showed up correctly, while the values didn't.

i.explose’s picture

That made the trick. thanks!

yoeld’s picture

Yul63: excellent! Simple and elegant. Many thanks.

drupalop’s picture

It works perfect.

mjk3r’s picture

Thank you so much! You saved my day. Was going crazy over this one.....

saify’s picture

Thanks, for this trick...

Geolim4’s picture

That's exactly what i need, thanks my savior !!

ivincent89’s picture

Thank you so much! it works really well...

sirmanux’s picture

You are my hero. Thank you so much.

seeem’s picture

guys i did every thing but still not working !!! the list still in english !!
any idea ?
by the way the select list in search views ,,,,

memoday’s picture

worked great for me. Thanks!

frazac’s picture

wow thanks. but don't work for translate dropdowns in the exposed filters in the views. any further idea? tks

Marcus_Johansson’s picture

david852’s picture

Even years later this article still does a wonderful job of giving someone new to Drupal Translations a good overview of the pro's con's between these two approaches!

Thank you!!

seeem’s picture

hello guys i found the solution for how to translate a select list in a views after a lot of searching follow this steps and i hope that's will work
step 1: translate the select list in (structure/content types/Manage fields) and translate the options there.
step 2: go to (structure/content types/Manage display)and change the format for the select list from default to default translated .
step 3: go to the view and change the old content in FILTER CRITERIA to the new content the translated one .

by this steps the select list will be translate ;)

andey12345’s picture

Thank you very much)

seeem’s picture

U welcome my friend ;)

TwoD’s picture

Look at how i18n_field_field_formatter_view() checks if there is a translation function available before falling back to printing out the untranslated list. (This function first checks to see if the 'Default Translated" formatter has been selected. If you always want translated strings, skip that part.)

scuba_fly’s picture

For facets you could use the hook_facet_items_alter.

Goto your facet configuration, check the box "Rewrite facet items via callback function" as active filter and in the filter settings.

Then implement the hook in your module:

function HOOK_facet_items_alter(&$build, &$settings) {
  if ($settings->facet == "YOUR_FACET_NAME") {
    foreach($build as $key => $item) {
      $build[$key]["#markup"] = t($item["#markup"]);
    }
  }
}

Drupal developer @limoengroen