Tag: rails i18n

Ruby on Rails 3.2.13 – 使用I18n获取自定义错误页面的错误

我有一个Rails 3.2.13应用程序,我最初使用路由filtergem为I18n。 我计划在Rails 4中重写它。我删除了gem,因为没有可用的Rails 4生产版本​​,我无法让beta版本工作。 我成功地为大多数应用程序设置了路由。 我唯一的问题是我的自定义错误页面。 我在使用routing-filter gem时创建了自定义错误页面。 我需要有关如何为自定义错误页面设置路由的帮助。 以下是我在config / application.rb中进行配置的方法 config.exceptions_app = self.routes 这是我在application_controller.rb中的内容 before_filter :set_locale def default_url_options(options={}) { :locale => I18n.locale } end private def set_locale I18n.locale = (params[:locale] if params[:locale].present?) || cookies[:locale] || ‘en’ cookies[:locale] = I18n.locale if cookies[:locale] != I18n.locale.to_s end 这是我在使用路由filtergem时在路由文件中的内容。 match “/404”, to: ‘errors#error_404’ match “/500”, to: […]

动态添加到I18N的翻译

我添加了人工资金访问器,如下所述: 在Ruby on Rails表单中输入数字时的小数和逗号 现在,我的模型中有两个属性用于相同类型的数据:原始版本和人类可读版本。 问题:由于我使用的是activerecord-translation-yml-files,我必须为原始属性和humanized_attribute添加相同的翻译,因为我的表单显示了humanized_attribute的名称,但是在validation错误时,原始名称属性显示。 是否可以动态添加翻译? 这样我可以在调用humanized_accessor-class-method时为字段的人性化版本添加翻译,从yml文件中获取原始翻译字符串,而不是将它们(具有相同的值)写入yml -file,只是为了让DRY更多。

启动轨道I18n和url助手似乎将区域设置与id混淆

我刚刚开始尝试更新我们的rails 3.2应用程序以实现国际化。 我在路线中有一个可选的范围,比如 范围“(:locale)”,locale:/ en | es | zh-HK | de | fr / do 如http://guides.rubyonrails.org/i18n.html所述 。 我的url助手就像 watch_url(@watch) 在表单中获取错误 没有路由匹配{:action =>“show”,:controller =>“watches”,:locale =>#”“,”收件人“ =>“”,“attributes”=>“”},状态:{},隐藏:false,评论:“”>} 关于它为什么会发生以及我如何解决它的任何想法? 谢谢! 编辑: 在我的应用程序控制器中,我有 before_filter :set_locale def set_locale I18n.locale = params[:locale] || I18n.default_locale end 和手表控制器的路线(这是一个8岁的专有应用程序,我们有100多个控制器:(,所以我不会包括所有路线)… $ rake routes | grep watch watches GET (/:locale)/watches(.:format) watches#index {:locale=>/en|es|zh-HK|de|fr/} POST (/:locale)/watches(.:format) watches#create {:locale=>/en|es|zh-HK|de|fr/} […]

I18n-active_record后端使用RailsAdmin

我正在使用rails_admin gem来处理我的数据,对于动态转换我正在使用I18n-active_record gem,一切正常但是,为了看到新的翻译我每次都要重启服务器。 我试过I18n.backend.reload! 没有成功。 有谁知道如何解决这个问题?

Way Rails不会引发I18n :: MissingInterpolationArgumentexception?

我创建了一个虚拟轨道4.1.5应用程序,以显示当未提供要插补的变量时,I18n translate方法不会引发I18n :: MissingInterpolationArgument。 只有在提供错误的情况下才会引发exception。 这是预期的行为吗? Loading development environment (Rails 4.1.5) irb(main):001:0> I18n.backend.store_translations :en, thanks: ‘Thanks %{name}!’ => {:thanks=>”Thanks %{name}!”} irb(main):002:0> I18n.translate :thanks => “Thanks %{name}!” irb(main):003:0> I18n.translate :thanks, foo: ‘bar’ I18n::MissingInterpolationArgument: missing interpolation argument :name in “Thanks %{name}!” ({:foo=>”bar”} given)

Rails国际化不在生产模式下工作

我有一个非AR模型,我用它来表单/组合对象。 有很多输入字段,我收集这些字段是为了在AR事务中进行一些数据操作。 我使用I18n进行国际化。 阳明: pl: activemodel: attributes: catalog/checkout: name: Imię 形成: = f.input :name, required: true, label: I18n.t(:’.activemodel.attributes.catalog/checkout.name’) 问题是在开发和生产模式(本地)中我看到输入具有从config/locales获取的适当标签,但在生产服务器上它表示 “translation missing: pl.activemodel.attributes.catalog/checkout.name” 它只发生在这种非AR模型中 – 在其他任何地方I18n工作得非常好。 有什么想法吗? 编辑 登录到生产服务器的控制台: I18n.reload! #=> nil I18n.t(:’.activemodel’).keys #=> [:errors, :attributes] # attributes is what wee need 但 I18n.t(:’.activemodel.attributes’) #=> “translation missing: pl.activemodel.attributes” WTH?… 编辑#2 我必须注意到,有 pl: activemodel: attributes: catalog/checkout: errors: […]

I18n locale无视后备

我问过一个关于区域设置的问题 。 我正试图将各种挪威语言的后备设置为挪威语Bokmal(:nb)。 期望的行为是,如果浏览器传递nn或no作为区域设置请求,则I18n.locale将设置为:nn或:no,然后在没有这些区域设置的翻译的情况下,:nb将被提供给浏览器。 基于我之前的问题的答案,我在我的应用程序初始化程序中有这一行: config.i18n.default_locale = :en config.i18n.fallbacks = {:nn => [:nb], :no => [:nb]} 在rails控制台中,这给了我以下结果: > I18n.fallbacks => {:en=>[:en]} > I18n.fallbacks[:nn] => [:nn, :nb, :en] > I18n.fallbacks[:no] => [:no, :nb, :en] 使用在语言列表中只有nn&no的浏览器,这不起作用 – 它会回退到以下的默认语言环境:en。 这是请求标头: Accept-Language: “nn,no;q=0.5” 如果我将:nb添加到浏览器语言堆栈,我正确地提供了挪威语内容。 我在这个过程中缺少什么?

如何递归地将YAML文件展平为JSON对象,其中键是以点分隔的字符串?

例如,如果我有YAML文件 en: questions: new: ‘New Question’ other: recent: ‘Recent’ old: ‘Old’ 这最终会像json对象一样 { ‘questions.new’: ‘New Question’, ‘questions.other.recent’: ‘Recent’, ‘questions.other.old’: ‘Old’ }

Rails:Model.human_attribute_name:字段应该在找不到翻译时引发错误? (也许是由state_machine引起的?)

我们经常在应用程序中偶然发现未翻译的模型属性。 它们通常是因为某个属性被重命名或类似的东西而来。 当Model.human_attribute_name :field找不到翻译时,让I18n引发错误会非常有帮助。 有没有办法实现这个目标? 更新 : 似乎还有其他一些问题。 这是我的I18n设置: I18n.enforce_available_locales = false config.i18n.load_path += Dir[Rails.root.join(‘config’, ‘locales’, ‘**’, ‘*.{rb,yml}’)] config.i18n.default_locale = ‘de-CH’ config.i18n.available_locales = [‘de’, ‘de-CH’, ‘en’] config.i18n.locale = ‘de-CH’ config.i18n.fallbacks = {‘de-CH’ => ‘de’, ‘en-GB’ => ‘en’} 我不能设置fallbacks = false因为我想要de-CH缺失翻译来优雅地委托给de ,这通常似乎工作正常。 但是对于我的状态机属性human_to_state方法,它似乎不起作用。 这是导致问题的视图代码: = f.input :state_event, collection: f.object.state_transitions, label_method: :human_to_name # This causes the problem! […]

Rails 4多域应用程序,为每个域i18n语言环境设置语言环境

在Rails 4多域应用程序中,我需要一组针对每个域的4种语言的区域设置文件(总共3个域)。 一些翻译在域之间重叠,但其中一些是非常具体的,所以我在想一个有点像这样的结构: config/locales/en.yml ..fr.yml ..de.yml ..it.yml #is picked up by all domains config/locales/domain1/en.yml ..fr.yml ..de.yml ..it.yml #is picked up by domain 1 config/locales/domain2/en.yml ..fr.yml ..de.yml ..it.yml #is picked up by domain 2 config/locales/domain3/en.yml ..fr.yml ..de.yml ..it.yml #is picked up by domain 3 这可能在Rails 4中吗? 如果是这样的话,最好的方法是什么呢?