本地化图像的最佳方法

时间:2009-08-10 06:56:37

标签: ruby-on-rails internationalization

在i18n rails应用中本地化图像(按钮和其他内容)的最佳方法是什么?

3 个答案:

答案 0 :(得分:26)

我通常在每个路径或图像名称中注入区域设置名称。 在第一种情况下,为不同的区域设置创建不同的文件夹

/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")

我通常更喜欢第二种解决方案,因为有许多资产我不需要本地化,我经常需要将翻译/公共资产保存在相同的文件夹中,以利用配置模式。

答案 1 :(得分:4)

我对这个老问题的2美分。

我更喜欢帮助方法并忽略默认区域设置,因此我不必将.en附加到我的所有资源

def localize(image_path, type = :svg) image_path.concat('.').concat(I18n.locale.to_s) if I18n.locale != I18n.default_locale image_path.concat('.').concat(type.to_s) if type image_path end

用过:

= image_tag localize('svg/feature-one-bubble-copy')

答案 2 :(得分:-3)

在最近的一个应用程序中,我第一次尝试了i18n翻译并取得了一些成功,尽管代码可能有点混乱。

在application_controller中我有:

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

这基本上会从指定的子域中查找相应的语言,因此http://en.mysite.com为英语,http://it.mysite.com为意大利语。

接下来是您的观点,您只需使用:

<%= t(:click_to_edit) %>

最后,在config / locales中创建一个包含所需翻译的en.yml文件:

en:
  admin_menu: "Admin Menu"
  Arabic: "Arabic"
  back: "Back"
  Basque: "Basque"
  blogs: "Blogs"
  blog_list: "Blog List"
  blogroll: "Blogroll"

意大利文件包含:

it:    
  admin_menu: "Menu Amministrazione"
  Arabic: "Italian Arabic"
  back: "Indietro"
  Basque: "Italian Basque"