则回退到默认语言

则回退到默认语言

本文介绍了如果缺少翻译,则回退到默认语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在国际化的 Rails (2.3.5) 应用程序中,我想显示默认语言环境的翻译而不是缺少翻译" - 有一张票,但它似乎仍在等待中:

in an internationalised Rails (2.3.5) app, I'd like to show a translation from the default locale instead of "translation missing" - there's a ticket for it but it seems it's still pending:

https://rails.lighthouseapp.com/projects/8994/tickets/2637-patch-i18n-look-up-a-translation-with-the-default-locale-when-its-missed-with-another-specific-locale

例如(取自ticket),有两个翻译文件,en.yml和es.yml:

For example (taken from the ticket), with two translation files, en.yml and es.yml:

en:

  hello: 'hello'

  hello_world: 'hello world'



es:

  hello_world: 'hola mundo'

当我执行这段代码时:

I18n.t :hello, :locale => :es

Rails 返回hello"而不是带有translation missing"的 span.

Rails returns "hello" instead of a span with "translation missing".

由于工单仍处于待处理状态,我该如何实现此功能?我知道我可以通过并更改所有 I18n.t 调用以具有 :default 选项,但如果可以避免,我宁愿不必通过所有视图!由于它是一个补丁,我想我可以将它应用到 Rails 的冷冻宝石上,但如果可以的话,我宁愿避免这样做.

As the ticket is still pending, how could I implement this functionality? I know I could go through and change all my I18n.t calls to have the :default option, but I'd rather not have to go through all the views if I can avoid it! As it's a patch, I suppose I could apply it to the Rails frozen gems, but I'd rather avoid that if I can.

推荐答案

如果您使用的是 Rails 2,前提是您使用的是最新的 I18n gem,请将其添加到初始化程序中:

If you are using Rails 2, provided that you use the latest I18n gem, add this to an initializer:

I18n.backend.class.send(:include, I18n::Backend::Fallbacks)

然后你可以像这样添加你的后备:

Then you can add your fallbacks like this:

I18n.fallbacks.map('es' => 'en')

这篇关于如果缺少翻译,则回退到默认语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 02:12