module I18nData::LiveDataProvider

fetches data online from debian svn

Constants

ROOT
TRANSLATIONS
XML_CODES

Public Instance Methods

codes(type, language_code) click to toggle source
# File lib/i18n_data/live_data_provider.rb, line 23
def codes(type, language_code)
  language_code = language_code.upcase
  if language_code == 'EN'
    send("english_#{type}")
  else
    translated(type, language_code)
  end
end

Private Instance Methods

english_countries() click to toggle source
# File lib/i18n_data/live_data_provider.rb, line 73
def english_countries
  codes = {}
  xml(:countries).elements.each('*/iso_3166_entry') do |entry|
    name = entry.attributes['name'].to_s.gsub("'", "\\'")
    code = entry.attributes['alpha_2_code'].to_s.upcase
    codes[code] = name
  end
  codes
end
english_languages() click to toggle source
# File lib/i18n_data/live_data_provider.rb, line 61
def english_languages
  codes = {}
  xml(:languages).elements.each('*/iso_639_entry') do |entry|
    name = entry.attributes['name'].to_s.gsub("'", "\\'")
    code = entry.attributes['iso_639_1_code'].to_s.upcase
    next if code.empty? or name.empty?
    codes[code] = name
  end
  codes
end
po_to_hash(data) click to toggle source
# File lib/i18n_data/live_data_provider.rb, line 84
def po_to_hash(data)
  names = data.select{|l| l =~ /^msgid/ }.map{|line| line.match(/^msgid "(.*?)"/)[1] }
  translations = data.select{|l| l =~ /^msgstr/ }.map{|line| line.match(/^msgstr "(.*?)"/)[1] }

  translated = {}
  names.each_with_index do |name,index|
    translated[name]=translations[index]
  end
  translated
end
translate(type, language, to_language_code) click to toggle source
# File lib/i18n_data/live_data_provider.rb, line 34
def translate(type, language, to_language_code)
  translated = translations(type, to_language_code)[language]
  translated.to_s.empty? ? nil : translated
end
translated(type, language_code) click to toggle source
# File lib/i18n_data/live_data_provider.rb, line 39
def translated(type, language_code)
  translations = {}
  send("english_#{type}").each do |code,name|
    translation = translate(type, name, language_code)
    translations[code] = translation || name
  end
  translations
end
translations(type, language_code) click to toggle source
# File lib/i18n_data/live_data_provider.rb, line 49
def translations(type, language_code)
  begin
    url = TRANSLATIONS[type]+"#{language_code.downcase}.po"
    data = open(url).readlines
  rescue
    raise NoTranslationAvailable, "for #{type} and language code = #{language_code}"
  end

  po_to_hash data
end
xml(type) click to toggle source
# File lib/i18n_data/live_data_provider.rb, line 95
def xml(type)
  xml = open(XML_CODES[type]).read
  REXML::Document.new(xml)
end