Globalizing files (attachments, images) in rails

2019-08-10 01:18发布

We can use globalize gem for globalizing text fields in models. For globalizing(translating) 'designation' attribute of an employee, we use translates :designation in the employee model, and employee.translations prints the translations for the employee object (one object for each supported locale with designation in corresponding locale).

I have a model specific attribute (image for employee) which is a paperclip attachment. Need to globalize the image, so that employee.image will give the actual image for default locale (:en) and employee.image.translations will return all the translations of the image (one image/paperclip attachment for each supported locale)

How to globalize the paperclip attachments in rails?

1条回答
家丑人穷心不美
2楼-- · 2019-08-10 01:55

There are a set of attributes that get added to the Employee model when we add a paperclip attachment image to it. Instead of trying to translate paperclip attachment, I added the translation of those attributes in the parent Employee model.The attributes that get added are image_file_name, image_content_type, image_file_size and image_file_updated_at. So, adding

translates :image_file_name, :image_content_type, 
           :image_file_size, :image_file_updated_at

and running necessary translation table migration for the employee model with these additional columns helped to globalize the paperclip attachment.

Now, employee.translations will return one object for each supported locale and each of the objects will have the designation, image_file_name, image_content_type, image_file_size and image_file_updated_at for the corresponding locale.

查看更多
登录 后发表回答