在Rails中全球化文件(附件,图像) [英] Globalizing files (attachments, images) in rails

查看:71
本文介绍了在Rails中全球化文件(附件,图像)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以使用全球化gem 来全球化模型中的文本字段.对于全球化(翻译)雇员的指定"属性,我们在雇员模型中使用translates :designation,并且employee.translations打印雇员对象的翻译(每个受支持语言环境的一个对象,并在相应的语言环境中进行指定).

我有一个特定于模型的属性(员工的图像),它是一个回形针附件.需要全球化图像,以便employee.image将提供实际图像的默认区域设置(:en),而employee.image.translations将返回图像的所有译文(每个受支持的区域设置一个图像/回形针附件)

如何全局化轨道中的回形针附件?

解决方案

当我们向其添加回形针附件image时,会向Employee模型添加一组属性.我没有尝试翻译回形针附件,而是在父级Employee模型中添加了这些属性的翻译,添加的属性为image_file_name, image_content_type, image_file_size and image_file_updated_at.因此,添加

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

并为雇员模型运行必要的转换表迁移,并使用这些附加的列有助于全球化回形针附件.

现在,employee.translations将为每个受支持的语言环境返回一个对象,并且每个对象的对应语言环境都将具有designation, image_file_name, image_content_type, image_file_size and image_file_updated_at.

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?

解决方案

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.

这篇关于在Rails中全球化文件(附件,图像)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆