public
Created

Generate yml for country locales

  • Download Gist
i18n-country-import.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
require 'rubygems'
require 'open-uri'
 
# 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
# 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!
#
# == Example
# rake import:country_select 'de'
#
# The code is deliberately procedural and simple, so it's easily
# understandable by beginners as an introduction to Rake tasks power.
# See http://github.com/joshmh/cldr/tree/master/converter.rb for much more robust solution
 
namespace :import do
 
desc "Import country codes and names for various languages from the Unicode.org CLDR archive. Depends on Hpricot gem."
task :country_select do
begin
require 'hpricot'
rescue LoadError
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 = ARGV[1]
unless locale
puts "\n[!] Usage: rake import:country_select de\n\n"
exit 0
end
 
# ----- Get the CLDR HTML --------------------------------------------------
begin
puts "... getting the HTML file for locale '#{locale}'"
doc = Hpricot( open("http://www.unicode.org/cldr/data/charts/summary/#{locale}.html") )
rescue => e
puts "[!] Invalid locale name '#{locale}'! Not found in CLDR (#{e})"
exit 0
end
 
 
# ----- Parse the HTML with Hpricot ----------------------------------------
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
code = code[-code.size, 2]
name = row.search("td[@class='v']").inner_text
countries << { :code => code.to_sym, :name => name.to_s }
print " ... #{name}"
end
end
 
 
# ----- Prepare the output format ------------------------------------------
output =<<HEAD
#{locale}:
countries:
HEAD
countries.each do |country|
output << " #{country[:code]}: \"#{country[:name]}\"\n"
end
output <<<<TAIL
TAIL
 
# ----- Write the parsed values into file ---------------------------------
puts "\n... writing the output"
filename = File.join(File.dirname(__FILE__), '..', 'locale', "#{locale}.rb")
filename += '.NEW' if File.exists?(filename) # Append 'NEW' if file exists
File.open(filename, 'w+') { |f| f << output }
puts "\n---\nWritten values for the '#{locale}' into file: #{filename}\n"
# ------------------------------------------------------------------------------
end
 
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.