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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

How to translate fields labels


Go to End


10 Posts   6030 Views

Avatar
Martijn

Community Member, 271 Posts

25 September 2010 at 9:34pm

Can somebody remove spammer batpurev...

Avatar
Jare

Community Member, 39 Posts

23 September 2017 at 4:39am

Edited: 23/09/2017 5:19am

A note to self (as I have tried to Google this before, and now I did it again, and most definitively will do it again):

For some reason SilverStripe does not look for field translations in the lang files automatically. Or if it does, I don't know how I should define the fields in the language files so that SilverStripe would find them. I'm using SilverStripe 3.6.1.

Anyway, here is a workaround. Put this to any class inherited from DataObject:

public function fieldLabel($name)
{
	$label = parent::fieldLabel($name);
	$label = _t($this->class.'.'.$name, $label); //Try to translate the field
	return $label;
}

And then you can translate your fields in your mysite/lang/en.yml (or whatever language file):

en:
  Title: MyTitle
  AnotherField: AnotherTranslation

Please do note that the fieldnames are case sensitive, so "Title", "AnotherField" or whatever you have must be written as-is. Also remember to run ?flush=all after editing your language file.

EDIT:

It seems that my workaround does not translate summary fields. But I found this:

http://www.balbuss.com/translating-statics-fields-and-labels/#TranslatingFieldlabels

class MyExample extends DataObject implements i18nEntityProvider {
 
static $db = array(
    'Name' => 'Varchar',
    ...
);
 
function fieldLabels($includerelations = true) {
    $labels = parent::fieldLabels($includerelations);
 
    // add a translation to the $labels array for each db field
    // if $includerelations == true (and it normally is) you need to add 
    // the db_ prefix, cause that's what SS will look for
    foreach($this->stat('db') as $key => $value) {
        $labels[$key] = _t("MyExample.db_{$key}", $key);
    }
 
    return $labels;
}

Go to Top