Skip to content

Commit

Permalink
fix country import script, closes: mlitwiniuk#14
Browse files Browse the repository at this point in the history
  • Loading branch information
mlitwiniuk committed Oct 26, 2013
1 parent acac06a commit 96e5cfe
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/tasks/localized_country_select_tasks.rake
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
require 'rubygems'
require 'open-uri'
require 'active_support/inflector'

# Rake task for importing country names from Unicode.org's CLDR repository
# (http://www.unicode.org/cldr/data/charts/summary/root.html).
#
# It parses a HTML file from Unicode.org for given locale and saves the
#
# It parses a HTML file from Unicode.org for given locale and saves the
# Rails' I18n hash in the plugin +locale+ directory
#
#
# Don't forget to restart the application when you add new locale to load it into Rails!
#
#
# == Parameters
# LOCALE (required): Sets the locale to use. Output file name will include this.
# FORMAT (optional): Output format, either 'rb' or 'yml'. Defaults to 'rb' if not specified.
Expand All @@ -17,7 +18,7 @@ require 'open-uri'
# == Examples
# rake import:country_select LOCALE=de
# rake import:country_select LOCALE=pt-BR WEB_LOCALE=pt FORMAT=yml
#
#
# The code is deliberately procedural and simple, so it's easily
# understandable by beginners as an introduction to Rake tasks power.
# See https://github.com/svenfuchs/ruby-cldr for much more robust solution
Expand All @@ -32,9 +33,9 @@ namespace :import do
puts "Error: Hpricot library required to use this task (import:country_select)"
exit
end

# TODO : Implement locale import chooser from CLDR root via Highline

# Setup variables
locale = ENV['LOCALE']
unless locale
Expand Down Expand Up @@ -62,16 +63,17 @@ namespace :import do
puts "... parsing the HTML file"
countries = []
doc.search("//tr").each do |row|
if row.search("td[@class='n']") &&
row.search("td[@class='n']").inner_html =~ /^namesterritory$/ &&
row.search("td[@class='g']").inner_html =~ /^[A-Z]{2}/
code = row.search("td[@class='g']").inner_text
n = row.search("td[@class='n']")
g = row.search("td[@class='g']")
if n && n.inner_html =~ /NamesTerritories/ && g.inner_html =~ /^[A-Z]{2}/
code = g.inner_text
code = code[-code.size, 2]
name = row.search("td[@class='v']").inner_text
countries << { :code => code.to_sym, :name => name.to_s }
print " ... #{name}"
print " ... #{code}: #{name}"
end
end
puts "\n\n... imported countries: #{countries.count}"


# ----- Prepare the output format ------------------------------------------
Expand Down Expand Up @@ -104,7 +106,7 @@ HEAD
output << "\t\t\t:#{country[:code]} => \"#{country[:name]}\",\n"
end
output <<<<TAIL
}
}
}
}
Expand Down

0 comments on commit 96e5cfe

Please sign in to comment.