Sha256: 7a2893265f4deef186bb1c7e49e94ad3effdb6e44247b23210b8efa25b9d36da

Contents?: true

Size: 1.65 KB

Versions: 20

Compression:

Stored size: 1.65 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

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

        def readable_mapping
          @readable ||= JSON.parse(File.read(File.join(Spaceship::ROOT, "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

20 entries across 20 versions & 1 rubygems

Version Path
spaceship-0.38.1 lib/spaceship/tunes/language_converter.rb
spaceship-0.38.0 lib/spaceship/tunes/language_converter.rb
spaceship-0.37.0 lib/spaceship/tunes/language_converter.rb
spaceship-0.36.2 lib/spaceship/tunes/language_converter.rb
spaceship-0.36.1 lib/spaceship/tunes/language_converter.rb
spaceship-0.36.0 lib/spaceship/tunes/language_converter.rb
spaceship-0.35.0 lib/spaceship/tunes/language_converter.rb
spaceship-0.34.3 lib/spaceship/tunes/language_converter.rb
spaceship-0.34.2 lib/spaceship/tunes/language_converter.rb
spaceship-0.34.1 lib/spaceship/tunes/language_converter.rb
spaceship-0.34.0 lib/spaceship/tunes/language_converter.rb
spaceship-0.33.0 lib/spaceship/tunes/language_converter.rb
spaceship-0.32.4 lib/spaceship/tunes/language_converter.rb
spaceship-0.32.3 lib/spaceship/tunes/language_converter.rb
spaceship-0.32.2 lib/spaceship/tunes/language_converter.rb
spaceship-0.32.1 lib/spaceship/tunes/language_converter.rb
spaceship-0.32.0 lib/spaceship/tunes/language_converter.rb
spaceship-0.31.10 lib/spaceship/tunes/language_converter.rb
spaceship-0.31.9 lib/spaceship/tunes/language_converter.rb
spaceship-0.31.8 lib/spaceship/tunes/language_converter.rb