Permalink
Browse files

adding more translations. adding rake import task. bumping version

  • Loading branch information...
1 parent 76d848a commit 89f0b9fe1c2568c7c9612aea1a4dbf4698cdfdcc @onomojo committed Mar 31, 2012
View
@@ -40,6 +40,7 @@ GEM
diff-lcs (1.1.3)
erubis (2.7.0)
hike (1.2.1)
+ hpricot (0.8.6)
i18n (0.6.0)
i18n-spec (0.1.2)
journey (1.0.3)
@@ -105,6 +106,7 @@ PLATFORMS
ruby
DEPENDENCIES
+ hpricot
i18n-country-translations!
i18n-spec (>= 0.1.1)
rspec-rails (>= 2.7.0)
View
@@ -4,6 +4,11 @@ I18n Country Translations - The purpose of this gem is to simply provide country
If you're doing anything with country names and translations, there's no need to reinvent the wheel and add your own translations. Just use this gem's country translations and skip the hassle of having to add and manage each country translation for each locale.
+== Supported Locales
+There are translations for all of the following locales: de, en, es, fr, js, pt, zh
+
+Of those, we have only confirmed the translations for the following locales: en, es, jp
+
== Installation
Add to your Gemfile:
@@ -20,12 +25,15 @@ or
== Contributing
-You can easily start adding country translations to the locales with help from this script: https://gist.github.com/2128684
-It will generate a new yml file that contains the country translations for the locale specified. Take the contents of this file and move it into the correct locale file.
-So far only en, es, and jp have been added. Please help us add more.
+You can easily start adding country translations to the locales with help of the following rake task:
+
+ IMPORT_LOCALE=en rake import:country_translation
+
+It will generate a new yml file that contains the country translations for the locale specified. Please note that some of the country translations may still be missing.
You can also use these standard translations to help if you don't want to use the script above: http://unicode.org/repos/cldr-tmp/trunk/diff/summary/root.html
+If you find an error in a translation, please let us know.
== Contributors
Brian McQuay - http://www.onomojo.com
View
@@ -29,6 +29,6 @@ RSpec::Core::RakeTask.new(:rcov) do |spec|
spec.rcov = true
end
-#require 'i18n-spec/tasks' # needs to be loaded after rspec
+Dir.glob('lib/tasks/*.rake').each { |r| import r }
task :default => :spec
Binary file not shown.
@@ -24,4 +24,5 @@ Gem::Specification.new do |s|
s.add_development_dependency "rspec-rails", ">= 2.7.0"
s.add_development_dependency "i18n-spec", ">= 0.1.1"
s.add_development_dependency "spork", "~> 1.0rc"
+ s.add_development_dependency "hpricot"
end
@@ -1,3 +1,3 @@
module I18nCountryTranslations
- VERSION = "0.0.3"
+ VERSION = "0.0.4"
end
@@ -1,4 +1,73 @@
-# desc "Explaining what the task does"
-# task :i18n-country-translations do
-# # Task goes here
-# end
+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
+#
+
+namespace :import do
+
+ desc "Import country codes and names for various languages from the Unicode.org CLDR archive. Depends on Hpricot gem."
+ task :country_translation do
+ begin
+ require 'hpricot'
+ rescue LoadError
+ puts "Error: Hpricot library required to use this task (import:country_select)"
+ exit
+ end
+
+ # Setup variables
+ locale = ENV['IMPORT_LOCALE']
+ unless locale
+ puts "\n[!] Usage: IMPORT_LOCALE=de rake import:country_translation\n\n"
+ exit 0
+ end
+
+ # ----- Get the CLDR HTML --------------------------------------------------
+ begin
+ 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 ----------------------------------------
+ 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 }
+ 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__), '..', '..', 'rails', 'locale', "#{locale}.yml")
+ 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
Oops, something went wrong.

0 comments on commit 89f0b9f

Please sign in to comment.