Sha256: aef4d4a9c0fb81bc4e4c66fcae47cdbc701c2663c6b7ac8837586124c056f514

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 KB

Contents

module TripAdvisor
  class Translation
    # Returns a dictionary that specifies how locale identifiers map to BCP 47 Language Identifiers
    def self.locale_identifier_to_language_codes_mapping
      {  "zh_TW" => "zh-Hant", "zh_CN"=> "zh-Hans", "zh_CN"=> "zh",
         "no"=> "nb", "in"=> "id", "en_UK"=> "en-GB" }
    end
    
    # Supports keyed lookup by locale identifier
    class LocalizationsArray < Array
      def [](locale_identifier)
        if locale_identifier.is_a?(String)
          self.detect { |localization| localization.locale_identifier == locale_identifier.to_s }
        else
          super
        end
      end
    end
  
    class Localization
      attr_accessor :locale_identifier, :language_name, :string, :status
  
      def initialize(attributes = {})
        attributes.each { |k, v| self.send("#{k}=", v) }
      end
      
      def locale_identifier=(locale_identifier)
        @locale_identifier = (Translation.locale_identifier_to_language_codes_mapping[locale_identifier] || locale_identifier)
      end
      
      def string=(string)
        @string = string.strip
      end
  
      def to_s
        string
      end
    
      def active?
        status == 'Active'
      end
    end
  
    attr_accessor :id, :key, :note, :localizations
  
    def initialize(attributes = {})
      @localizations = LocalizationsArray.new
      attributes.each { |k, v| self.send("#{k}=", v) }
    end
    
    def key=(key)
      @key = key.strip
    end
  
    def localizations=(localizations)      
      @localizations.concat(localizations)
    end
    
    def [](locale_identifier)
      localizations[locale_identifier]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
trip_advisor-0.0.21 lib/trip_advisor/translation.rb
trip_advisor-0.0.15 lib/trip_advisor/translation.rb