Skip to content

Commit

Permalink
adding more translations. adding rake import task. bumping version
Browse files Browse the repository at this point in the history
  • Loading branch information
onomojo committed Mar 31, 2012
1 parent 76d848a commit 89f0b9f
Show file tree
Hide file tree
Showing 11 changed files with 1,171 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -40,6 +40,7 @@ GEM
diff-lcs (1.1.3) diff-lcs (1.1.3)
erubis (2.7.0) erubis (2.7.0)
hike (1.2.1) hike (1.2.1)
hpricot (0.8.6)
i18n (0.6.0) i18n (0.6.0)
i18n-spec (0.1.2) i18n-spec (0.1.2)
journey (1.0.3) journey (1.0.3)
Expand Down Expand Up @@ -105,6 +106,7 @@ PLATFORMS
ruby ruby


DEPENDENCIES DEPENDENCIES
hpricot
i18n-country-translations! i18n-country-translations!
i18n-spec (>= 0.1.1) i18n-spec (>= 0.1.1)
rspec-rails (>= 2.7.0) rspec-rails (>= 2.7.0)
Expand Down
14 changes: 11 additions & 3 deletions README.rdoc
Expand Up @@ -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. 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 == Installation


Add to your Gemfile: Add to your Gemfile:
Expand All @@ -20,12 +25,15 @@ or


== Contributing == Contributing


You can easily start adding country translations to the locales with help from this script: https://gist.github.com/2128684 You can easily start adding country translations to the locales with help of the following rake task:
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. 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 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 == Contributors
Brian McQuay - http://www.onomojo.com Brian McQuay - http://www.onomojo.com
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -29,6 +29,6 @@ RSpec::Core::RakeTask.new(:rcov) do |spec|
spec.rcov = true spec.rcov = true
end end


#require 'i18n-spec/tasks' # needs to be loaded after rspec Dir.glob('lib/tasks/*.rake').each { |r| import r }


task :default => :spec task :default => :spec
Binary file added i18n-country-translations-0.0.4.gem
Binary file not shown.
1 change: 1 addition & 0 deletions i18n-country-translations.gemspec
Expand Up @@ -24,4 +24,5 @@ Gem::Specification.new do |s|
s.add_development_dependency "rspec-rails", ">= 2.7.0" s.add_development_dependency "rspec-rails", ">= 2.7.0"
s.add_development_dependency "i18n-spec", ">= 0.1.1" s.add_development_dependency "i18n-spec", ">= 0.1.1"
s.add_development_dependency "spork", "~> 1.0rc" s.add_development_dependency "spork", "~> 1.0rc"
s.add_development_dependency "hpricot"
end end
2 changes: 1 addition & 1 deletion lib/i18n_country_translations/version.rb
@@ -1,3 +1,3 @@
module I18nCountryTranslations module I18nCountryTranslations
VERSION = "0.0.3" VERSION = "0.0.4"
end end
77 changes: 73 additions & 4 deletions lib/tasks/i18n-country-translations_tasks.rake
@@ -1,4 +1,73 @@
# desc "Explaining what the task does" require 'rubygems'
# task :i18n-country-translations do require 'open-uri'
# # Task goes here
# end # 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

0 comments on commit 89f0b9f

Please sign in to comment.