现在的位置: 首页 > 综合 > 正文

rails web 国际化

2014年03月05日 ⁄ 综合 ⁄ 共 865字 ⁄ 字号 评论关闭
You can implement it like this in yourApplicationController:

before_filter :set_locale
 
def set_locale
  I18n.locale = extract_locale_from_tld || I18n.default_locale
end
 
# Get locale from top-level domain or return nil if such locale is not available
# You have to put something like:
#   127.0.0.1 application.com
#   127.0.0.1 application.it
#   127.0.0.1 application.pl
# in your /etc/hosts file to try this out locally
def extract_locale_from_tld
  parsed_locale = request.host.split('.').last
  I18n.available_locales.include?(parsed_locale.to_sym) ? parsed_locale  : nil
end

We can also set the locale from the subdomain in a very similar way:

# Get locale code from request subdomain (like http://it.application.local:3000)
# You have to put something like:
#   127.0.0.1 gr.application.local
# in your /etc/hosts file to try this out locally
def extract_locale_from_subdomain
  parsed_locale = request.subdomains.first
  I18n.available_locales.include?(parsed_locale.to_sym) ? parsed_locale : nil
end

抱歉!评论已关闭.