module I18nData::LiveDataProvider

fetches data online from debian git

Constants

CLONE_DEST
JSON_CODES
REPO
TRANSLATIONS

Public Instance Methods

clear_cache() click to toggle source
# File lib/i18n_data/live_data_provider.rb, line 32
def clear_cache
  `rm -rf #{CLONE_DEST}`
  raise unless $?.success?
end
codes(type, language_code) click to toggle source
# File lib/i18n_data/live_data_provider.rb, line 21
def codes(type, language_code)
  ensure_checkout

  language_code = language_code.upcase
  if language_code == 'EN'
    send("english_#{type}")
  else
    translated(type, language_code)
  end
end

Private Instance Methods

alpha_codes_for_countries() click to toggle source
# File lib/i18n_data/live_data_provider.rb, line 99
def alpha_codes_for_countries
  @alpha_codes_for_countries ||= json(:countries)['3166-1'].each_with_object({}) do |entry, codes|
    alpha2 = entry['alpha_2']&.upcase
    alpha3 = entry['alpha_3']&.upcase
    codes[alpha2] = alpha3 unless alpha2.nil?
  end
end
alpha_codes_for_languages() click to toggle source
# File lib/i18n_data/live_data_provider.rb, line 107
def alpha_codes_for_languages
  @alpha_codes_for_languages ||= json(:languages)['639-2'].each_with_object({}) do |entry, codes|
    alpha2 = entry['alpha_2']&.upcase
    alpha3 = entry['alpha_3']&.upcase
    codes[alpha2] = alpha3 unless alpha2.nil?
  end
end
english_countries() click to toggle source
# File lib/i18n_data/live_data_provider.rb, line 128
def english_countries
  @english_countries ||= begin
    codes = {}
    json(:countries)['3166-1'].each do |entry|
      name = (entry['common_name'] || entry['name']).to_s
      code = entry['alpha_2'].to_s.upcase
      codes[code] = name
    end
    codes
  end
end
english_languages() click to toggle source
# File lib/i18n_data/live_data_provider.rb, line 115
def english_languages
  @english_languages ||= begin
    codes = {}
    json(:languages)['639-2'].each do |entry|
      name = entry['name'].to_s
      code = entry['alpha_2'].to_s.upcase
      next if code.empty? or name.empty?
      codes[code] = name
    end
    codes
  end
end
ensure_checkout() click to toggle source
# File lib/i18n_data/live_data_provider.rb, line 39
def ensure_checkout
  unless File.exist?(CLONE_DEST)
    `git clone #{REPO} #{CLONE_DEST}`
    raise unless $?.success?
  end
end
fallback_name(type, alpha3) click to toggle source
# File lib/i18n_data/live_data_provider.rb, line 140
def fallback_name(type, alpha3)
  send("english_#{type}")[send("alpha_codes_for_#{type}").invert[alpha3]]
end
get(url) click to toggle source
# File lib/i18n_data/live_data_provider.rb, line 144
def get(url)
  File.read("#{CLONE_DEST}/#{url}")
end
get_po_data(data, extracted_comment_string) click to toggle source
# File lib/i18n_data/live_data_provider.rb, line 84
def get_po_data(data, extracted_comment_string)
  # Ignores the 'fuzzy' entries
  po_entries = data.select do |t|
    t[:extracted_comment].start_with?(extracted_comment_string) && t[:flag] != 'fuzzy'
  end

  # Maps over the alpha3 country code in the 'extracted_comment'
  # Eg: "Name for GBR"
  po_entries.map.with_object({}) do |t, translations|
    alpha3 = t[:extracted_comment][-3..-1].upcase
    translation = t[:msgstr]
    translations[alpha3] = translation.is_a?(Array) ? translation.join : translation
  end
end
json(type) click to toggle source
# File lib/i18n_data/live_data_provider.rb, line 148
def json(type)
  JSON.parse(get(JSON_CODES[type]))
end
translate(type, alpha3, to_language_code) click to toggle source
# File lib/i18n_data/live_data_provider.rb, line 56
def translate(type, alpha3, to_language_code)
  translated = translations(type, to_language_code)[alpha3]
  translated.to_s.empty? ? nil : translated
end
translated(type, language_code) click to toggle source
# File lib/i18n_data/live_data_provider.rb, line 46
def translated(type, language_code)
  @translated ||= {}

  @translated["#{type}_#{language_code}"] ||= begin
    Hash[send("alpha_codes_for_#{type}").map do |alpha2, alpha3|
      [alpha2, translate(type, alpha3, language_code) || fallback_name(type, alpha3)]
    end]
  end
end
translations(type, language_code) click to toggle source
# File lib/i18n_data/live_data_provider.rb, line 61
def translations(type, language_code)
  @translations ||= {}
  @translations["#{type}_#{language_code}"] ||= begin
    code = language_code.split("_")
    code[0].downcase!
    code = code.join("_")

    begin
      file_path = "#{CLONE_DEST}/#{TRANSLATIONS[type]}#{code}.po"
      data = SimplePoParser.parse(file_path)
      data = data[1..-1] # Remove the initial info block in the .po file

      # Prefer the "Common name for" blocks, but fallback to "Name for" blocks
      common_names = get_po_data(data, 'Common name for')
      fallback_names = get_po_data(data, 'Name for')

      fallback_names.merge(common_names)
    rescue Errno::ENOENT
      raise NoTranslationAvailable, "for #{type} and language code = #{code} (#{$!})"
    end
  end
end