For example:
例如:
I18n.t('something')
should output only
应该只输出
something
if the translation missing.
如果翻译失踪。
5 个解决方案
#1
-8
nope, not possible. If you use I18 you need to have a file that corresponds to the language otherwise I18n will complain.
不,不可能。如果您使用I18,您需要有一个与该语言相对应的文件,否则I18n会抱怨。
Of course you can set the default language in your environment.rb file. Should be near the bottom and you can set this for whatever language you want but in your locales/
folder you will need to have a corresponding yml
translation.
当然,您可以在environment.rb文件中设置默认语言。应该在底部附近,您可以将其设置为您想要的任何语言,但在您的语言环境/文件夹中,您需要具有相应的yml转换。
#2
26
It is possible: See section 4.1.2 Defaults at Rails Internationalization (I18n) API.
有可能:请参见Rails国际化(I18n)API中的4.1.2默认值。
I18n.t :missing, :default => 'Not here'
# => 'Not here'
#3
7
On rails 4 you can change the exception handler.
在rails 4上,您可以更改异常处理程序。
Add the following to config/initializers/i18n.rb
:
将以下内容添加到config / initializers / i18n.rb:
module I18n
class MissingTranslationExceptionHandler < ExceptionHandler
def call(exception, locale, key, options)
if exception.is_a?(MissingTranslation)
key
else
super
end
end
end
end
I18n.exception_handler = I18n::MissingTranslationExceptionHandler.new
Now on you views you can just do:
现在您可以查看:
<p><%= t "Not translated!" %></p>
Guide on the subject: http://guides.rubyonrails.org/i18n.html#using-different-exception-handlers
有关该主题的指南:http://guides.rubyonrails.org/i18n.html#using-different-exception-handlers
#4
1
side-note: this might help figuring out what Rails thinks the current scope is (e.g. when using ".something")
旁注:这可能有助于弄清楚Rails认为当前范围是什么(例如,当使用“.something”时)
http://unixgods.org/~tilo/Rails/which_l10n_strings_is_rails_trying_to_lookup.html
http://unixgods.org/~tilo/Rails/which_l10n_strings_is_rails_trying_to_lookup.html
this way you can better avoid having missing translations because of incorrect placing of translations strings in the L10n-file / incorrect keys
通过这种方式,您可以更好地避免因为L10n文件/错误键中的翻译字符串放置不正确而导致缺少翻译
#5
1
David's answer is the right solution to the question, another (more verbose) way to do it is to rescue and return the key:
大卫的答案是问题的正确解决方案,另一个(更详细)的方法是拯救并返回密钥:
def translate_nicely(key)
begin
I18n.translate!(key)
rescue
key
end
end
#1
-8
nope, not possible. If you use I18 you need to have a file that corresponds to the language otherwise I18n will complain.
不,不可能。如果您使用I18,您需要有一个与该语言相对应的文件,否则I18n会抱怨。
Of course you can set the default language in your environment.rb file. Should be near the bottom and you can set this for whatever language you want but in your locales/
folder you will need to have a corresponding yml
translation.
当然,您可以在environment.rb文件中设置默认语言。应该在底部附近,您可以将其设置为您想要的任何语言,但在您的语言环境/文件夹中,您需要具有相应的yml转换。
#2
26
It is possible: See section 4.1.2 Defaults at Rails Internationalization (I18n) API.
有可能:请参见Rails国际化(I18n)API中的4.1.2默认值。
I18n.t :missing, :default => 'Not here'
# => 'Not here'
#3
7
On rails 4 you can change the exception handler.
在rails 4上,您可以更改异常处理程序。
Add the following to config/initializers/i18n.rb
:
将以下内容添加到config / initializers / i18n.rb:
module I18n
class MissingTranslationExceptionHandler < ExceptionHandler
def call(exception, locale, key, options)
if exception.is_a?(MissingTranslation)
key
else
super
end
end
end
end
I18n.exception_handler = I18n::MissingTranslationExceptionHandler.new
Now on you views you can just do:
现在您可以查看:
<p><%= t "Not translated!" %></p>
Guide on the subject: http://guides.rubyonrails.org/i18n.html#using-different-exception-handlers
有关该主题的指南:http://guides.rubyonrails.org/i18n.html#using-different-exception-handlers
#4
1
side-note: this might help figuring out what Rails thinks the current scope is (e.g. when using ".something")
旁注:这可能有助于弄清楚Rails认为当前范围是什么(例如,当使用“.something”时)
http://unixgods.org/~tilo/Rails/which_l10n_strings_is_rails_trying_to_lookup.html
http://unixgods.org/~tilo/Rails/which_l10n_strings_is_rails_trying_to_lookup.html
this way you can better avoid having missing translations because of incorrect placing of translations strings in the L10n-file / incorrect keys
通过这种方式,您可以更好地避免因为L10n文件/错误键中的翻译字符串放置不正确而导致缺少翻译
#5
1
David's answer is the right solution to the question, another (more verbose) way to do it is to rescue and return the key:
大卫的答案是问题的正确解决方案,另一个(更详细)的方法是拯救并返回密钥:
def translate_nicely(key)
begin
I18n.translate!(key)
rescue
key
end
end