Translate images in model

Hello,

I have a translatable model that attaches a single image, but I would like to know if it’s possible to “translate” the image as well, such as having a different image depending on the active locale.
I understand that I could just attachMany, but it seems like a sloppy workaround and I’m wondering if there’s a more intuitive way, such as $model->setAttributeTranslatable.

For example, my site has 3 languages and my model can attachOne image. Instead of changing it to attachMany for the 2 other languages, is there a better way to have the image be translatable?

(OctoberCMS 3.x and PHP 8.1)

Thank you

Hi @seif

Support for translatable file attachments has been added in RainLab.Translate v2.1.0

Now you can add the relation name to the translatable attributes property. For example:

/**
 * @var array translatable attributes
 */
public $translatable = [
    'myimage'
];

/**
 * @var array attachOne relation
 */
public $attachOne = [
    'myimage' => \System\Models\File::class
];

Each attachment will be unique to the locale. However, there is no fallback support, so each locale needs to be uploaded separately. A solution to this could be two define two attachments, one for fallback, and one for translation.

I hope this helps.

1 Like