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?
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.
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;
$poster->fromFile($image);
$newsletter->poster = $poster;
}
if (strlen($translatedImage)) {
$poster = new File;
$poster->fromFile($translatedImage);
$newsletter->setAttributeTranslated('poster', $poster, $locale);
}
$newsletter->save();