如果缺少翻译,请使用默认语言 [英] Fall back to default language if translation missing
问题描述
在一个国际化的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:
例如(取自票证),带有两个翻译文件,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",而不是缺少翻译"的跨度.
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')
这篇关于如果缺少翻译,请使用默认语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!