【问题标题】:How to change image according to language choice (i18n gem in Rails)?如何根据语言选择更改图像(Rails 中的 i18n gem)?
【发布时间】:2019-05-08 17:15:09
【问题描述】:
我愿意根据语言的选择更改图片。这是因为我的图像包含一些文本,并且我根据语言选择(荷兰语、法语和英语)制作了三个不同版本的图像。我需要怎么做?
我尝试添加我的
<%= image_tag("lansink_en.png", class: "center-image") %> 在我不同的 en.yml、fr.yml 和 nl.yml 文件中,但没有成功。
感谢您的帮助!
【问题讨论】:
标签:
html
css
internationalization
rails-i18n
【解决方案1】:
您可以转义 yaml 文件中的 html 内容。所以说你有一个 yml 喜欢
en:
url: '<%= image_tag("lansink_en.png", class: "center-image") %>'
然后显示它:
<%= raw t('url') %>
然后只需设置您的其他语言环境文件就可以了
【解决方案2】:
我认为翻译文件不应包含代码或任何比严格语义更具体的标记。
如果它们被运送给翻译人员(或使用 Google 翻译之类的工具自动翻译),那么英文代码 可能会被翻译,从而使您在一种或多种语言中出现问题,而在其他人。
如果您更改了类名,则不需要针对这种特殊情况进行一轮翻译或绕过。
我更喜欢旧的辅助方法:
module SomeHelper
def lansink_image_tag
image_tag("lansink_#{I18n.locale}.png", class: "center-image")
end
end
或者,如果您不知道所有语言都必须有一个文件,但您想要一个后备:
module SomeHelper
def lansink_image_tag
has_image = [:de, :fr, :en]
suffix = has_image.include?(I18n.locale) ? I18n.locale : :en
image_tag("lansink_#{suffix}.png", class: "center-image")
end
end