Ruby on rails 图像定位的最佳方法
在i18n rails应用程序中本地化图像(按钮和其他内容)的最佳方法是什么?在最近的一个应用程序中,我第一次尝试了i18n翻译,并取得了一些成功,尽管代码可能有点混乱 在应用程序控制器中,我有:Ruby on rails 图像定位的最佳方法,ruby-on-rails,internationalization,Ruby On Rails,Internationalization,在i18n rails应用程序中本地化图像(按钮和其他内容)的最佳方法是什么?在最近的一个应用程序中,我第一次尝试了i18n翻译,并取得了一些成功,尽管代码可能有点混乱 在应用程序控制器中,我有: class ApplicationController < ActionController::Base before_filter :set_locale AVAILABLE_LOCALES = I18n.backend.available_locales private
class ApplicationController < ActionController::Base
before_filter :set_locale
AVAILABLE_LOCALES = I18n.backend.available_locales
private
##########
# Internationalisation
# For more information see http://guides.rubyonrails.org/i18n.html
# Additional locale starting points can be found here http://github.com/svenfuchs/rails-i18n/tree/a0be8dd219ccce6716955566ee557cc75122cb33/rails/locale
def tld
domain_array = request.subdomains.split(".")
domain_array[0] == "www" ? domain_array[1].to_s : domain_array[0].to_s
end
def available_locales
AVAILABLE_LOCALES
end
def set_locale
I18n.locale = extract_locale_from_subdomain
end
def extract_locale_from_subdomain
(available_locales.include? tld.to_sym) ? tld : nil unless tld.blank?
end
end
意大利文件包含:
it:
admin_menu: "Menu Amministrazione"
Arabic: "Italian Arabic"
back: "Indietro"
Basque: "Italian Basque"
我通常在每个路径或图像名称中插入区域设置名称。 在第一种情况下,为不同的地区创建不同的文件夹
/public/images/en/file.png
/public/images/it/file.png
image_tag("#{I18n.locale}/file.png")
在第二种情况下
/public/images/file.it.png
/public/images/file.en.png
image_tag("file.#{I18n.locale}.png")
我通常更喜欢第二种解决方案,因为有许多资产我不需要本地化,而且我经常需要将翻译的/通用的资产保存在相同的文件夹中,以利用常规而不是配置模式。我对这个老问题的回答是2美分 我更喜欢使用helper方法并忽略默认的区域设置,因此我不必将
.en
附加到我的所有资产中
def本地化(图像路径,类型=:svg)
如果I18n.locale!=I18n.default\u区域设置
image_path.concat('.').concat(type.to_s)如果类型为
图像路径
结束
用法如下:
=图像\标记本地化('svg/功能一个气泡副本')
这个问题指的是图像,而不是文本内容。
/public/images/en/file.png
/public/images/it/file.png
image_tag("#{I18n.locale}/file.png")
/public/images/file.it.png
/public/images/file.en.png
image_tag("file.#{I18n.locale}.png")