0

国際化を処理するために、Rails2 プロジェクトで globalize2 を使用しています。タイトル、説明などを持つ Page モデルがあります。

現在使用している言語に基づいて、ページを別の URL にリダイレクトするために使用される「 url_redirect 」というフィールドを使用します。例:

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

その特定のフィールドのみのフォールバックを回避するにはどうすればよいですか? Globalize3 が属性のフォールバック翻訳を特定のコンテキストに返すのを回避する方法は? globalize2 の場合。

前もって感謝します。

4

1 に答える 1

0

適切に設計されたソリューションではありませんが、最終的にこのトリックで問題を解決しました:

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

単純な初期化子を使用します。まだより良い解決策を探しています。

于 2014-11-13T10:49:10.897 に答える