Rails i18n:我可以关闭“翻译缺失"吗?错误? [英] Rails i18n: Can I turn off "translation missing" errors?

查看:52
本文介绍了Rails i18n:我可以关闭“翻译缺失"吗?错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多租户应用程序,我正在尝试使用i18n gem,以使我们每个客户都可以根据自己的喜好自定义系统,更改各个页面上的文本,自定义电子邮件等等.诚然,我没有按原计划使用i18n,因为我实际上并没有翻译不同的语言",所以一切都是英语,但是每个客户都有不同的英语(如果可以的话).

I have a multi-tenant application and I'm experimenting with using the i18n gem to allow each of our clients to customize the system to their liking, changing the text on various pages, customizing emails, and so forth. Admittedly, I'm not using i18n as it was intended to be used, since I'm not actually translating different "languages", everything is in English, but each client has a DIFFERENT English, if that makes sense.

仍然,我遇到了我认为i18n gem中的一个非常糟糕的设计决策:如果不存在翻译,而不是简单地不进行翻译并打印出通常会打印的内容,就会产生一个错误.错误.例如,

Still, I've come across what I think is a horribly bad design decision in the i18n gem: if ever a translation does not exist, rather than simply not doing a translation and printing out whatever it normally would, it raises an error. For example,

<%= distance_of_time_in_words_to_now @press_release.submitted_at %>

出现为

translation missing: en, datetime, distance_in_words, x_days

我的意思是,加油!我什至不希望被翻译.

I mean, come on! I don't even WANT that to be translated.

我知道发生这种情况的原因是因为我没有加载默认的翻译,但是我将ActiveRecord用作后端,所以我想保持它的干净. 解决方案"是将所有yaml翻译文件导入到我的数据库翻译存储中,但这似乎不是一个好主意.如果将来我要升级滑轨怎么办?我将不得不担心使所有这些翻译保持同步.

I understand that the reason this is happening is because I don't have the default translations loaded, but I'm using ActiveRecord as a backend and I wanted to keep it clean. The "solution" would be to import all of the yaml translation files into my database translation store, but that doesn't seem like a good idea. What if I upgrade rails in the future? I'm going to have to worry about keeping all of these translations in sync.

同样,我无法理解为什么这是默认行为. ANYBODY什么时候会希望显示该时髦的错误消息,而不是仅使用默认的"3天前"?

Again, I cannot fathom why this is the default behavior. When would ANYBODY want that funky error message to show up instead of just using the default "3 days ago"?

无论如何,我的问题是,如果翻译不存在,有没有办法让它自动关闭翻译并使用未翻译的消息?谢谢!

Anyway, my question is, is there a way to have it automatically turn off the translation and use the untranslated message if the translation doesn't exist? Thanks!

推荐答案

这似乎可以解决问题.

require 'i18n' # without this, the gem will be loaded in the server but not in the console, for whatever reason

# store translations in the database's translations table
I18n.backend = I18n::Backend::ActiveRecord.new

# for translations that don't exist in the database, fallback to the Simple Backend which loads the default English Rails YAML files
I18nSimpleBackend = I18n::Backend::Simple.new
I18n.exception_handler = lambda do |exception, locale, key, options|
  case exception
  when I18n::MissingTranslationData
    I18nSimpleBackend.translate(:en, key, options || {})
  else
    raise exception
  end
end

这篇关于Rails i18n:我可以关闭“翻译缺失"吗?错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆