2011-02-17 4 views
0

나는 mongo_mapper를 사용하여 번역을 저장/표시하기위한 LocalizedString 맞춤 데이터 유형을 만들었습니다.mongo_mapper 현지화를위한 맞춤 데이터 유형

이것은 하나의 필드에서 작동하지만 다른 필드를 소개하자마자 각 필드에 대해 기록되고 두 필드에 대해 하나의 값만 표시됩니다. to_mongo와 from_mongo는 제대로 작동하지 않는 것 같습니다. 어느 누구도이 문제에 도움이 될 수 있습니까? 그녀는 코드입니다 :

class LocalizedString 

    attr_accessor :translations 

    def self.from_mongo(value) 

    puts self.inspect 
    @translations ||= if value.is_a?(Hash) 
     value 
     elsif value.nil? 
     {} 
     else 
     { I18n.locale.to_s => value } 
    end 

    @translations[I18n.locale.to_s] 
    end 

    def self.to_mongo(value) 
    puts self.inspect 
    if value.is_a?(Hash) 
     @translations = value 
    else 
     @translations[I18n.locale.to_s] = value 
    end 

    @translations 
    end 
end 

감사합니다 많이 릭

답변

1

문제는 당신 내에서이다 [하기 |에서] _mongo 방법, @translations는 클래스 변수가 아니라 당신이 기대하는 인스턴스 변수를 의미합니다. 그래서 일어나는 일은 from_mongo가 호출 될 때마다 값을 덮어 쓰는 것입니다. - 내가 EmbeddedDocument에 LocalizedString를 사용하는 경우 번역이 발견되지 않았 음을 얻을 것

class LocalizedString 
    attr_accessor :translations 

    def initialize(translations = {}) 
    @translations = translations 
    end 

    def self.from_mongo(value) 
    if value.is_a?(Hash) 
     LocalizedString.new(value) 
    elsif value.nil? 
     LocalizedString.new() 
    else 
     LocalizedString.new({ I18n.locale.to_s => value }) 
    end 
    end 

    def self.to_mongo(value) 
    value.translations if value.present? 
    end 

end 
+0

은 내가 아래에 보여 해시 방식보다 훨씬 더 많은이 방법을 좋아하지만, 나는 그것이 동작하지 않습니다 - 나는 to_mongo와 alwaysend '정의되지 않은 메서드'번역 ' 어떤 생각을? – adamnickerson

1

내가 제라드의 반응은 나를 위해 작동하지 않았다는 것을 발견

고정 버전은 다음과 같이 될 것이다.

임베디드 문서를 사용할 때 번역이 0 인 비슷한 문제가 발생합니다. 작동하는 솔루션을 얻으려면 Rick의 솔루션을 사용하여 번역 변수를 인스턴스 변수로 변경하여 LocalizedString을 사용하는 각각의 새 필드를 덮어 쓰지 않고 번역이 0이 아닌지 확인하도록 추가했습니다. 그럴 경우 새로운 해시를 만드십시오).

모든 LocalizedString 솔루션 중에서, 내가 EmbeddedDocuments에서 작동하고 덮어 쓰기 문제없이 작업 할 수 있었던 것은 이번이 처음입니다. - 여전히 다른 문제가있을 수 있습니다! :)

class LocalizedString 
    attr_accessor :translations 

    def self.from_mongo(value) 

     puts self.inspect 
     translations ||= if value.is_a?(Hash) 
      value 
      elsif value.nil? 
      {} 
      else 
      { I18n.locale.to_s => value } 
     end 

     translations[I18n.locale.to_s] 
     end 

     def self.to_mongo(value) 
     puts self.inspect 
     if value.is_a?(Hash) 
      translations = value 
     else 
      if translations.nil? 
      translations = Hash.new() 
      end 
      translations[I18n.locale.to_s] = value 
     end 

     translations 
     end 

    end 
+0

나는 이것을 게시하는 포대를 뛰어 넘었다. 물론, 이런 방식으로, 하나의 LocalizedString은 모든 번역을 가지지 않고 모두 분리되어있다. 그래서 당신이 Mongo에 머물러있을 때, 당신은 단 하나의 번역을 얻습니다. 다시 드로잉 보드로 돌아갑니다! – adamnickerson

+0

OK -이 모든 것을 무시하십시오 ... 다음 응답을보십시오. – adamnickerson

0

내가 찾은 this post : 매우 도움이되었다. HashWithIndifferentAccess를 확장하여 LocalizedString으로 작업했습니다. 내가 좋아하지 않는 유일한 점은 매번 설정할 때 로케일을 명시 적으로 지정해야한다는 것이 었습니다. 더 많은 문자열처럼 작동하기를 원했습니다. 물론, = 연산자를 오버로드 할 수는 없습니다 (최소한 나는 여러분이 할 수 있다고 생각하지 않습니다) 그래서 < <을 사용하고 현재 로케일의 문자열을 출력하는 to_s 메서드를 추가했습니다 ....

class LocalizedString < HashWithIndifferentAccess 
    def self.from_mongo(value) 
    LocalizedString.new(value || {}) 
    end 

    def available_locales 
    symbolize_keys.keys 
    end 

    def to_s 
    self[I18n.locale] 
    end 

    def in_current_locale=(value) 
    self[I18n.locale] = value 
    end 

    def << (value) 
    self[I18n.locale] = value 
    end 
다음 끝

와 나는 같은 클래스가 :

class SimpleModel 
    include MongoMapper::Document 

    key :test, LocalizedString 
end 

과 같은 일을 할 수

I18n.locale = :en 
    a = SimpleModel.new 
    a.test << "English" 
    I18n.locale = :de 
    a.test << "German" 
    puts a.test # access the translation for the current locale 
    I18n.locale = :en 
    puts a.test # access the translation for the current locale 
    puts a.test[:de] # access a translation explicitly 
    puts a.test[:en] 
    puts a.test.inspect 

얻을은

German 
English 
German 
English 
{"en"=>"English", "de"=>"German"} 

그래서 거기에 우리가 간다 -이 하나의 사실은 나를 위해 작동하는 것 같다. 의견 환영, 그리고 이것이 누군가를 도울 수 있기를 바랍니다!