首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Globalize2,禁用特定字段的回退。

Globalize2,禁用特定字段的回退。
EN

Stack Overflow用户
提问于 2014-11-13 10:22:58
回答 1查看 142关注 0票数 0

我在一个globalize2项目上使用Rails2来处理国际化。我有一个页面模型,有标题,描述,等等。

我使用一个名为"url_redirect“的字段,该字段用于根据我目前使用的语言将页面重定向到另一个URL。例如:

代码语言:javascript
复制
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。以下是后遗症:

代码语言:javascript
复制
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这样的东西。

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2014-11-13 10:49:10

不是一个精心设计的解决方案,但最后我用这个技巧解决了我的问题:

代码语言:javascript
复制
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

使用简单的初始化器。还在寻找更好的解决方案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26906520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档