如何让Rails自动从YAML翻译键名中推断英文翻译?
问题描述:
有没有一种方法可以让Rails自动从关键字中推断英文翻译?如何让Rails自动从YAML翻译键名中推断英文翻译?
目前我必须明确写出翻译,因为否则它只会在视图中输出en, devise.confirmations.new.resend_confirmation_instructions
。
我现在有在config/locales/en.yml
文件中写入:
en:
devise:
confirmations:
new:
resend_confirmation_instructions: "Resend confirmation instructions."
我想什么有:
en:
devise:
confirmations:
new:
resend_confirmation_instructions:
和视图应该只是输出“重新发送确认指令。”这是从该密钥直接推断出来的。
答
你总是可以编写自己的异常处理程序:
#config/initializers/i18n.rb
module I18n
class MissingTranslationExceptionHandler < ExceptionHandler
def call(exception, locale, key, options)
if exception.is_a?(MissingTranslation)
key.split('.').last.try(:humanize)
else
super
end
end
end
end
I18n.exception_handler = I18n::MissingTranslationExceptionHandler.new
你可以阅读更多on the guides。
有没有办法做到这一点(我知道),因为这完全反驳了翻译的点。但是在生产中,如果翻译缺失,Rails将显示“重新发送确认说明” – 2015-02-11 15:25:52
啊,它会在生产中这样做吗?我不知道。 顺便说一句,从英文翻译键获取英文文本不完全是一个翻译。无论如何,当您的代码库和翻译密钥都是英文的时候,这只是一种避免重复工作的便捷方式。 – Magne 2015-02-11 17:07:27
为此,您可以配置备用语言。点击此处查看:http://stackoverflow.com/a/7886246/488195 – dgilperez 2015-02-11 17:13:45