- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在尝试在我的 Rails 应用程序上实现国际化。这是我的应用程序 Controller 的一部分
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 参数设置语言环境,使我在我不使用的 route 使用 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 国际化文档中似乎遗漏的主要内容。一个详细的答案将不胜感激。
最佳答案
我已遵循 Rails 指南并相应地设置了 i18n。我无法理解你的问题。我唯一的不同是语言环境的提取。我使用了 request.subdomains.first 而不是最后一个。
def extract_locale_from_subdomain
parsed_locale = request.subdomains.first
I18n.available_locales.include?(parsed_locale.to_sym) ? parsed_locale : nil
end
您是否按照 Rails 指南中的说明设置了加载路径。
# in config/initializers/locale.rb
# tell the I18n library where to find your translations
I18n.load_path += Dir[Rails.root.join('lib', 'locale', '*.{rb,yml}')]
# set default locale to something other than :en
I18n.default_locale = :en
并检查 I18n.available_locales 返回什么,nil 或 locales。而且您使用的切换菜单看起来不错。它也会那样工作。
如果您能更具体地解释您的问题,也许我可以提供帮助。谢谢。
编辑:
这足以让您的 View 具有切换语言链接。
<% if I18n.locale == I18n.default_locale %>
<li><%= link_to image_tag("swe.png", :alt => "Sweden", :class =>"round"), "http://www.natkurser.se" %> %>
<li><h5><%= link_to_unless I18n.locale == :se, "Swedish", "#{'http://www.natkurser.se'}" %></h5>
<% else %>
<li><%= link_to image_tag("eng.png", :alt => "England", :class =>"round"), "http://gettheskill.com" %>
<li><h5><%= link_to_unless I18n.locale == :en, "English", "#{'http://gettheskill.com'}" %></h5>
<%end%>
关于ruby-on-rails - Rails 国际化 : Setting the Locale from the Domain Name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20064335/
我有一个 Java 字符串,但在处理时遇到了问题。我有一个字符串 s,它的值为丞(我随机选择的一个汉字,我不会说中文)。如果我打电话 String t = new String(s.getBytes(
在Qt中,所有的输入部件和文本绘制方式对Qt支持的所有语言都提供了内置的支持。Qt内置的字体引擎可以在同一时间正确而且精细地绘制不同的文本,这些文本可以包含来自众多不同书写系统的字符。如果想了解更多的
如何实现字段标签、帮助文本等的国际化支持。字段的标准乘法似乎不起作用例如文档中给出的用于创建联系表单的示例。我尝试为其他语言添加额外的字段(文档中描述的第一种方法) from django.db
是否有可能当我在操作类中使用“getText(”keyName”); 时,我可以指定仅从 ENGLISH 资源包中选择值。因为我只想将此值用于日志记录目的。 任何想法我怎么能做到这一点? BR 标准委
我有一些关于 GWT 的国际化解决方法的问题。 1) 处理直接写入 ui.xml 文件的静态文本的最佳方式是什么? 目前,我正在使用 Messages-Interface 来翻译 Java 生成的文本
我在 Symfony 2 中遇到了一个奇怪的翻译问题。 这是我的config.yml imports: - { resource: parameters.yml } - { resou
对于我的大部分应用程序,我可以使用 https://developers.google.com/web-toolkit/doc/latest/DevGuideI18n 中列出的推荐国际化技术。 (主要
我使用 Android Studio 创建了一个新的 Flutter 项目。我正在使用null-safety,并且正在物理设备上运行应用程序,但代码生成无法正常工作,没有flutter_gen.dar
这里是菜鸟。 我正在尝试对基于命令行的程序实现国际化。以下是 java 国际化路径中可用的内容。 import java.util.*; public class I18NSample { s
我使用 Android Studio 创建了一个新的 Flutter 项目。我正在使用null-safety,并且正在物理设备上运行应用程序,但代码生成无法正常工作,没有flutter_gen.dar
我正在开发一个需要多语言的应用程序。 我正在尝试关注 this国际化教程。但是,当我尝试“使用基本国际化”时, View 中没有列出我的 Storyboard。 我正在使用 XCode 4.6.3 我
我住在印度(有超过20种语言)我正在尝试在网站中实现java国际化。 但是在 Locale.getAvailableLocales() 中,本地语言不可用。我的情况是否可以实现国际化? 如果可能的话字
我有以下 SDN 4 实体: 决策、特征和值: @NodeEntity public class Value { private final static String SET_FOR = "
关于MySql,是否有支持所有或绝大多数语言的字符集? 最佳答案 统一码。它有几种编码:UTF-8、UTF-16 和 UTF-32。 来自 http://en.wikipedia.org/wiki/U
我目前正在使用 Vuepress。但是我想要在我的 Vuepress 站点中使用多种语言。经过 3 天的挣扎,我决定把我的问题放在这里。 (是的,我查看了 Vuepress 文档:https://vu
我需要返回一个 strftime() 调用,该调用使用与我本地机器/操作系统上设置的语言不同的语言。是否可以选择返回语言? 最佳答案 对于可靠的 i18n/L10N,可由必须在同一运行中提供不同本地化
我有一些按钮,按下它们后我想播放一些音频。按钮的名称是音频文件的名称 private func playAudio(title : String){ audioPlayer = AVAudio
如何更改 HTML5 的消息(或者它会根据浏览器的语言自动更改?) 例如: E-mail: 当我在 Opera 运行此代码(来自 W3schools)
我发现很多关于 url schemes 的话题,最常见的是: www.mysite.com/fr/products www.mysite.com/en/products www.mysite.com/
在 Android WebView 中处理国际化/本地化的最佳方式是什么。理想情况下,我想访问以下位置的所有字符串资源: res/values/strings.xmlres/values-de/str
我是一名优秀的程序员,十分优秀!