2
votes

Is it possible to translate the File Title for all files in the assets folder?

I installed the translatable plugin but there is no documentation for file translation in the i18n SilverStripe chapter or plugin documentation.

CMS File Title field screenshot

In my ProductPage I have a custom field for File upload:

$fields->addFieldToTab(
    'Root.Attachments',
    $uploadField = UploadField::create('Files', 'Dateien')
);

The files can be on the English and German website. I need to translate the File Title for each language.

I tried the following but when I change the File Title it changes for both German and English:

// Apply Translatable modifications
$this->applyTranslatableFieldsUpdate($fields, 'updateCMSFields');
2
Can you be a little more clear about what you want translated and what you have tried please - Barry
I updatet my question - Seryoga

2 Answers

2
votes

To enable the Translatable module on our File class we must add the Translatable extension to File in our mysite/_config.php file:

File::add_extension('Translatable');

However, I do not think the Translatable module is currently working for files.

There was an error when accessing assets as the Translatable class tries to add fields to the Folder class, which causes an error. This has now been fixed in the latest version with this pull request to fix the Folder error.

The next problem is Translatable requires the object to have a CMSEditLink function. SiteTree and SiteConfig have a CMSEditLink function, but File (and DataObject) does not.

To fix this we can create a FileExtension to define a CMSEditLink function for File:

class FileExtension extends DataExtension {
    public function CMSEditLink() {
        return singleton('AssetAdmin')->Link('EditForm/field/File/item/' . $this->owner->ID . '/edit');
    }
}

To enable this extension we add the following to our config.yml:

File:
  extensions:
    - FileExtension

After all of this, we can create file translations and view our translation list on each file, but the translation file links redirect to the root admin screen. It seems the assets link will redirect if it has the ?Locale= URL parameter set. This URL parameter is set by the translatable module.

So this still won't work after all that work.

1
votes

As you are getting started with this then I advise you read the docs

To answer to Whenever you create a Field then where you specify the title you need to use the _t or internationalisation function.

The _t function is what silverstripe uses for all of it's fields, so you only need to find the right Namespace and Entity name string for the specified function...

_t("Namespace.Entity","This is a string") 

For the field highlighted it is in the Object File which is the namespace and the Entity is Name and you can see this in framework\lang\en.yml

To override this you create your own lang files in mysite or your module folder with the detail to override it like this...

en:
  File:
    Name: My New Name Title