Sha256: 417cef89396a28868bea2c87daab1374e066846ad88575e509a86ee420321c6c

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

# encoding: UTF-8

# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0

module TwitterCldr
  module Shared
    class Languages
      class << self
        def all
          all_for(TwitterCldr::get_locale)
        end

        def all_for(code)
          get_resource(TwitterCldr.convert_locale(code.to_sym))[:languages]
        rescue
          {}
        end

        def from_code(code)
          from_code_for_locale(code, TwitterCldr::get_locale)
        end

        def from_code_for_locale(code, locale = TwitterCldr::get_locale)
          get_resource(TwitterCldr.convert_locale(locale.to_sym))[:languages][TwitterCldr.convert_locale(code.to_sym)]
        rescue
          nil
        end

        def translate_language(language, source_locale = :en, dest_locale = TwitterCldr::get_locale)
          source_locale = TwitterCldr.convert_locale(source_locale.to_sym)
          lang_code = get_resource(source_locale)[:languages].select { |key, val| val.downcase == language.downcase }.flatten.first

          if lang_code
            dest_locale = TwitterCldr.convert_locale(dest_locale.to_sym)
            get_resource(dest_locale)[:languages][lang_code.to_sym]
          else
            nil
          end
        rescue
          nil
        end

        protected

        def get_resource(locale)
          locale = TwitterCldr.convert_locale(locale)
          TwitterCldr.get_resource(locale, "languages")[locale]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
twitter_cldr-1.2.0 lib/twitter_cldr/shared/languages.rb