0

I use globalize2, on a Rails2 project, to handle internationalization. I have a Page model with title, description, and so on.

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. 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. 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:

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? for globalize2.

Thanks in advance.

Community
  • 1
  • 1
Mich Dart
  • 2,352
  • 5
  • 26
  • 44

1 Answers1

0

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.

Mich Dart
  • 2,352
  • 5
  • 26
  • 44