【发布时间】:2013-12-02 13:55:16
【问题描述】:
我正在尝试在我的 rails 应用程序上实现国际化。这是我的应用程序控制器的一部分
before_action :set_locale
def set_locale
I18n.locale = extract_locale_from_tld || I18n.default_locale
end
def extract_locale_from_tld
parsed_locale = request.host.split('.').last
I18n.available_locales.include?(parsed_locale.to_sym) ? parsed_locale : nil
end
它似乎不起作用,我只能从 url 参数设置语言环境,让我在我不想使用的路线中使用 scope "(:locale)", locale: /en|se/ do。
从 Rails 指南中,他们指出切换菜单应该这样实现。
link_to("Deutsch", "#{APP_CONFIG[:deutsch_website_url]}#{request.env['REQUEST_URI']}")
你如何实现这个? 我当前的切换菜单是这样的。
<% if I18n.locale == I18n.default_locale %>
<li><%= link_to image_tag("eng.png", :alt => "England", :class =>"round"), :locale=>'en' %>
<li><h5><%= link_to_unless I18n.locale == :se, "Swedish", "#{'http://www.natkurser.se'}" %></h5>
<% else %>
<li><%= link_to image_tag("swe.png", :alt => "Sweden", :class =>"round"), :locale=>'se' %>
<li><h5><%= link_to_unless I18n.locale == :en, "English", "#{'http://gettheskill.com'}" %></h5>
<%end%>
我已将 127.0.0.1 gettheskill.com 和 127.0.0.1 natkuser.se 添加到 /etc/hosts,但它仍然无法用于开发。我要修改哪些文件才能使其在生产中运行?我在想nginx配置文件。最终,路线应该如何出现。这是 Rails 国际化文档中似乎遗漏的主要内容。详细解答将不胜感激。
【问题讨论】:
标签: ruby-on-rails ruby nginx internationalization