Translate images in model


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 = [

 * @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.


Hey, thank you for your reply. I’m refactoring some code and came back to use this method of translating images.

I tried translating an image, and I notice that it gets added into the system_files table with the locale as suffix (for the default locale one. The translated one goes in rainlab_translate_attributes).
Fetching the file attribute will return me null, but fetching the translated file will successfully give me the image translated.

I removed the suffix from the system_files table (in my case :en), and I can get the file via the attribute without a problem.

Here’s an example of my model Newsletter:

public $attachOne = [
	'poster' => File::class
public $implement = ['Rainlab.Translate.Behaviors.TranslatableModel'];
public $translatable = ['poster'];

How I insert the attachment ($image & $translatedImage are local paths):

if (strlen($image)) {
	$poster = new File;
	$newsletter->poster = $poster;
if (strlen($translatedImage)) {
	$poster = new File;
	$newsletter->setAttributeTranslated('poster', $poster, $locale);

Now when I try accessing them (en = default locale):

$newsletter->poster; // null
$newsletter->getAttributeTranslated('poster', 'en'); // null
$newsletter->getAttributeTranslated('poster', 'fr'); // The translated attachment.

It seems like the suffix is probably not needed in attachment_type, though I haven’t went deep enough in the code to confirm.