Sha256: d035eeddbfea108965c278a7add6f34e5b9831d864902476a1bfdf5c0d60bf70

Contents?: true

Size: 1.93 KB

Versions: 68

Compression:

Stored size: 1.93 KB

Contents

module Spaceship
  module Tunes
    class LanguageConverter
      class << self
        # Converts the iTC format (English_CA, Brazilian Portuguese) to language short codes: (en-US, de-DE)
        def from_itc_to_standard(from)
          result = mapping.find { |a| a['name'] == from }
          (result || {}).fetch('locale', nil)
        end

        # Converts the language short codes: (en-US, de-DE) to the iTC format (English_CA, Brazilian Portuguese)
        def from_standard_to_itc(from)
          result = mapping.find { |a| a['locale'] == from || (a['alternatives'] || []).include?(from) }
          (result || {}).fetch('name', nil)
        end

        # Converts the langauge "UK English" (user facing) to "English_UK" (value)
        def from_itc_readable_to_itc(from)
          readable_mapping.each do |key, value|
            return key if value == from
          end
          nil
        end

        # Converts the langauge "English_UK" (value) to "UK English" (user facing)
        def from_itc_to_itc_readable(from)
          readable_mapping[from]
        end

        private

        # Path to the gem to fetch resoures
        def spaceship_gem_path
          if Gem::Specification.find_all_by_name('spaceship').any?
            return Gem::Specification.find_by_name('spaceship').gem_dir
          else
            return './'
          end
        end

        # Get the mapping JSON parsed
        def mapping
          @languages ||= JSON.parse(File.read(File.join(spaceship_gem_path, "lib", "assets", "languageMapping.json")))
        end

        def readable_mapping
          @readable ||= JSON.parse(File.read(File.join(spaceship_gem_path, "lib", "assets", "languageMappingReadable.json")))
        end
      end
    end
  end
end

class String
  def to_language_code
    Spaceship::Tunes::LanguageConverter.from_itc_to_standard(self)
  end

  def to_full_language
    Spaceship::Tunes::LanguageConverter.from_standard_to_itc(self)
  end
end

Version data entries

68 entries across 68 versions & 1 rubygems

Version Path
spaceship-0.31.7 lib/spaceship/tunes/language_converter.rb
spaceship-0.31.6 lib/spaceship/tunes/language_converter.rb
spaceship-0.31.5 lib/spaceship/tunes/language_converter.rb
spaceship-0.31.4 lib/spaceship/tunes/language_converter.rb
spaceship-0.31.3 lib/spaceship/tunes/language_converter.rb
spaceship-0.31.2 lib/spaceship/tunes/language_converter.rb
spaceship-0.31.1 lib/spaceship/tunes/language_converter.rb
spaceship-0.31.0 lib/spaceship/tunes/language_converter.rb
spaceship-0.30.0 lib/spaceship/tunes/language_converter.rb
spaceship-0.29.1 lib/spaceship/tunes/language_converter.rb
spaceship-0.29.0 lib/spaceship/tunes/language_converter.rb
spaceship-0.28.0 lib/spaceship/tunes/language_converter.rb
spaceship-0.27.2 lib/spaceship/tunes/language_converter.rb
spaceship-0.27.1 lib/spaceship/tunes/language_converter.rb
spaceship-0.27.0 lib/spaceship/tunes/language_converter.rb
spaceship-0.26.3 lib/spaceship/tunes/language_converter.rb
spaceship-0.26.2 lib/spaceship/tunes/language_converter.rb
spaceship-0.26.1 lib/spaceship/tunes/language_converter.rb
spaceship-0.26.0 lib/spaceship/tunes/language_converter.rb
spaceship-0.25.1 lib/spaceship/tunes/language_converter.rb