How to use i18n with complex fields in ModelAdmin

Silverstripe Version: 4.7

Question: How to use i18n with complex fields in ModelAdmin

I extend the cms using ModelAdmion. Everything is fine but i did not get the labels beeing translated.

class Person extends DataObject {
...
    private static $has_one = array(
		'myImage' => Image::class
    );
...
}

In my de_DE.yml I have the following variables:

de_DE:
  Person :
    db_myImageID: Bild1
    db_myImage: Bild2
    myImage: Bild2

But the translated title is not shown in cms if i create a new record. Instead there is “My Image” as caption of the UploadField. Any ideas?

Best regards,
chrclaus

The static variable field_names ist set

     private static $field_labels = [
        'Image.Title' => 'to-be-changed-via-i18n',
        ];

There is a function in DataObject

    public function fieldLabels($includerelations = true)    {
        ...
    }

which looks only for the has_one-relations if the parameter $includerelations is set to true. Unfortunately, the relevant call for my DataObject sets it to false. Any ideas, how to get the field-labels for the has_one-relations too before looking for the second solution = overriding?

Best regards,
chrclaus

The db_ prefix works only with bare fields. Try this instead:

de_DE:
  Person :
    has_one_myImage: Bild1

See the DataObject::fieldLabels for the other possible prefixes.

If you need more control (e.g. you want to build the label dynamically), you can also override that method in your DataObject.

Thats it. Thank you very much! :slight_smile:

Best regards,
chrclaus