Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Form Questions /

How to translate Enum-based dropdowns


Reply


1239 Views

Avatar
javelin

Community Member, 12 Posts

9 June 2010 at 7:53am

Edited: 09/06/2010 7:56am

I've found that keeping a site multilingual is quite easy with Silverstripe except for a few parts.

Recently I ran into trouble with some forms that are automatically generated by getFrontEndFields() (used extensively in this project).

Some Dropdowns are generated from enum-type db-fields and they display the hardcoded strings from my datamodel. I didn't find an easy way to translate these so I resorted to hacking the core. I inserted the following function into Enum.php and redirected a call in the enumValues-function inside Enum.php.

I hope that maybe someone can make use of this or, even better, maybe that someone has a better way to implement this without hacking core. I'm no programmer so this was created out of necessity.

This then requires a line in a lang-file formatted something like:
$lang['sv_SE']['Enum-ApartmentCategory']['LÄGENHET'] = 'Lägenhet';

   

function translateArray($array) {
      foreach ($array as $key => $value) {
         $array[$key] = _t('Enum-'.$this->name.".".mb_strtoupper($key), $value);
      }
      return $array;
   }