Sha256: ea9a0828c146b91af2745ae29cfa2ece9ec452ef27f39cd9bbb161b50f9288b5

Contents?: true

Size: 1.99 KB

Versions: 359

Compression:

Stored size: 1.99 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 "UK English" to itc locale en-GB
        def from_standard_to_itc_locale(from)
          result = mapping.find { |a| a['name'] == from } || {}
          return result['itc_locale'] || result['locale']
        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 language "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 language "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_itc_locale
    Spaceship::Tunes::LanguageConverter.from_standard_to_itc_locale(self)
  end

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

Version data entries

359 entries across 359 versions & 1 rubygems

Version Path
fastlane-2.74.0.beta.20180106010004 spaceship/lib/spaceship/tunes/language_converter.rb
fastlane-2.73.0 spaceship/lib/spaceship/tunes/language_converter.rb
fastlane-2.73.0.beta.20180105010003 spaceship/lib/spaceship/tunes/language_converter.rb
fastlane-2.73.0.beta.20180104010004 spaceship/lib/spaceship/tunes/language_converter.rb
fastlane-2.73.0.beta.20180103010003 spaceship/lib/spaceship/tunes/language_converter.rb
fastlane-2.72.0 spaceship/lib/spaceship/tunes/language_converter.rb
fastlane-2.72.0.beta.20180102010003 spaceship/lib/spaceship/tunes/language_converter.rb
fastlane-2.72.0.beta.20180101010003 spaceship/lib/spaceship/tunes/language_converter.rb
fastlane-2.72.0.beta.20171231010003 spaceship/lib/spaceship/tunes/language_converter.rb
fastlane-2.71.1 spaceship/lib/spaceship/tunes/language_converter.rb
fastlane-2.72.0.beta.20171230010003 spaceship/lib/spaceship/tunes/language_converter.rb
fastlane-2.72.0.beta.20171229010003 spaceship/lib/spaceship/tunes/language_converter.rb
fastlane-2.72.0.beta.20171228010004 spaceship/lib/spaceship/tunes/language_converter.rb
fastlane-2.71.0 spaceship/lib/spaceship/tunes/language_converter.rb
fastlane-2.71.0.beta.20171227010004 spaceship/lib/spaceship/tunes/language_converter.rb
fastlane-2.71.0.beta.20171226010004 spaceship/lib/spaceship/tunes/language_converter.rb
fastlane-2.71.0.beta.20171225010003 spaceship/lib/spaceship/tunes/language_converter.rb
fastlane-2.71.0.beta.20171224010003 spaceship/lib/spaceship/tunes/language_converter.rb
fastlane-2.71.0.beta.20171223010003 spaceship/lib/spaceship/tunes/language_converter.rb
fastlane-2.71.0.beta.20171222010003 spaceship/lib/spaceship/tunes/language_converter.rb