选择性转义某些 parts/helper 方法的翻译
Selectivley escape translation of certain parts/helper methods
我广泛使用辅助方法 time_ago_in_words
,并且它与 I18n 一起正常工作。
但在应用程序的某些部分,我只想要默认的英语。
是否可以在应用程序的选定 methods/areas 上逃避翻译?
您可以使用 I18n.with_locale
方法在特定语言环境下强制执行代码块,而不管全局配置如何。
I18n.with_locale(:en) do
time_ago_in_words(...)
end
如果您发现自己经常使用这个模式,您可以创建一个特定的助手。
def english_time_ago_in_words(*args)
I18n.with_locale(:en) do
time_ago_in_words(*args)
end
end
您也可以使用 I18n.default_locale
而不是直接引用 :en
。
我广泛使用辅助方法 time_ago_in_words
,并且它与 I18n 一起正常工作。
但在应用程序的某些部分,我只想要默认的英语。
是否可以在应用程序的选定 methods/areas 上逃避翻译?
您可以使用 I18n.with_locale
方法在特定语言环境下强制执行代码块,而不管全局配置如何。
I18n.with_locale(:en) do
time_ago_in_words(...)
end
如果您发现自己经常使用这个模式,您可以创建一个特定的助手。
def english_time_ago_in_words(*args)
I18n.with_locale(:en) do
time_ago_in_words(*args)
end
end
您也可以使用 I18n.default_locale
而不是直接引用 :en
。