我在一个globalize2项目上使用Rails2来处理国际化。我有一个页面模型,有标题,描述,等等。
我使用一个名为"url_redirect“的字段,该字段用于根据我目前使用的语言将页面重定向到另一个URL。例如:
Page.find(1)
title description locale url_redirect
test .... it
my_test .... en www.google.it在控制器端,我检查url_redirect的存在,并制作一个简单的redirect_to,除非为空。不幸的是,我使用了globalize2的后援.因此,即使我在意大利网站上,字段url_redirect返回www.google.it。以下是后遗症:
require "i18n/backend/fallbacks"
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
module Globalize
class << self
def fallbacks(locale = self.locale)
case locale
when :it then [:it, :en, :es, :fr]
when :en then [:en, :it, :es, :fr]
when :es then [:es, :en, :it, :fr]
when :fr then [:fr, :it, :en, :es]
end
end
end
end如何才能避免仅针对特定字段出现退步?我想要像How to avoid Globalize3 from returning fallback translations for an attribute into a specific context? globalize2这样的东西。
提前谢谢。
发布于 2014-11-13 10:49:10
不是一个精心设计的解决方案,但最后我用这个技巧解决了我的问题:
module Globalize
module ActiveRecord
class Adapter
protected
def fetch_attribute(locale, attr_name)
translations = fetch_translations(locale)
value, requested_locale = nil, locale
Globalize.fallbacks(locale).each do |fallback|
if attr_name == :url_redirect
translation = translations.detect { |t| t.locale == locale }
else
translation = translations.detect { |t| t.locale == fallback }
end
value = translation && translation.send(attr_name)
locale = fallback && break if value
end
set_metadata(value, :locale => locale, :requested_locale => requested_locale)
value
end
end
end
end使用简单的初始化器。还在寻找更好的解决方案。
https://stackoverflow.com/questions/26906520
复制相似问题