简体   繁体   English

Globalize2,禁用特定字段的回退

[英]Globalize2, Disable fallback for a specific field

I use globalize2, on a Rails2 project, to handle internationalization. 我在Rails2项目上使用globalize2来处理国际化。 I have a Page model with title, description, and so on. 我有一个带有标题,描述等的Page模型。

I use a field called " url_redirect " that's used to redirect the page to another URL, based on the language I'm currently using. 我使用一个名为“ url_redirect ”的字段,该字段用于根据当前使用的语言将页面重定向到另一个URL。 For eg: 例如:

Page.find(1)

title    description locale  url_redirect
test        ....       it
my_test     ....       en    www.google.it

On the controller side I check the presence of url_redirect and make a simple redirect_to unless blank. 在控制器方面,我检查是否存在url_redirect并进行简单的redirect_to,除非为空白。 Unfortunately, I use the globalize2 fallbacks... so even though I'm on the italian site, the field url_redirect returns www.google.it.. These are the fallbacks: 不幸的是,我使用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 can I avoid fallback only for that specific field? 如何避免仅针对该特定领域的备用广告? I would like something like this How to avoid Globalize3 from returning fallback translations for an attribute into a specific context? 我想要这样的事情如何避免Globalize3将属性的后备转换返回到特定上下文中? for globalize2. 为全球化2。

Thanks in advance. 提前致谢。

Not a well-designed solution, but finally I solved my problem with this trick: 这不是一个设计合理的解决方案,但最终我用以下技巧解决了我的问题:

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

using a simple initializer. 使用简单的初始化程序。 Still looking for a better solution. 仍在寻找更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM