Permalink
Browse files

updated generator and README and copied tasks dir to lib for rails 3

  • Loading branch information...
1 parent 860ab70 commit 7291f43017b6d9a05c551e8ed80dd26c01e59da5 @kristianmandrup kristianmandrup committed Jan 15, 2011
View
@@ -14,12 +14,18 @@ You can easily translate language codes in your application like this:
<%= I18n.t @user.language, :scope => 'languages' %>
</pre>
-Comes with a Rake task <tt>rake import:language_select locale=de</tt> for importing language names
-from Unicode.org's CLDR repository (http://www.unicode.org/cldr/data/charts/summary/root.html)
+h2. Locale file generation
+
+The following Rake tasks can be used for importing language names from Unicode.org's CLDR repository (http://www.unicode.org/cldr/data/charts/summary/root.html)
+
+* <tt>rake import:language_select locale=de</tt>
+* <tt>rake yaml_locales:language_select locale=de</tt>
+
+The _import_ one generates ruby files, the _yaml_locales_ task generates yml files.
+
Don't forget to restart the application when you add new locale.
-The code borrows heavily from the LocalizedCountrySelect plugin.
-See "localized_country_select":http://github.com/karmi/localized_country_select
+The code borrows heavily from the LocalizedCountrySelect plugin - "localized_country_select":http://github.com/karmi/localized_country_select
h2. Install & Usage
@@ -1,15 +1,12 @@
-# require 'rails/generators/base'
-# require 'sugar-high/array'
-# require 'active_support/inflector'
-# require 'rails3_artifactor'
-# require 'logging_assist'
+# require 'rake'
module LocalizedLanguageSelect
module Generators
class LocaleGenerator < Rails::Generators::Base
- desc "Copy locale files"
+ desc "Copy language locale files"
class_option :locales, :type => :array, :default => ['all'], :desc => 'locales to generate for'
+ class_option :file_ext, :type => :string, :default => 'rb', :desc => 'file extension for locale files to copy'
source_root File.dirname(__FILE__)
@@ -19,20 +16,34 @@ def main_flow
protected
- def supported_locales
- [:en, :da]
+ def file_ext
+ options[:file_ext]
end
def locales
- return supported_locales if options[:locales].include? 'all'
- options[:locales].map(&:to_sym) & supported_locales
+ return Dir.glob("#{locale_dir}/**.#{file_ext}") if options[:locales].include? 'all'
+ options[:locales].map(&:to_sym)
+ end
+
+ def locale_dir
+ File.expand_path("../../../../locale", File.dirname __FILE__)
+ end
+
+ def locale_ref
+ "../../../../locale"
+ end
+
+ def locale_name
+ :languages
end
- def copy_locales
- locales.each do |locale|
- copy_file "../../../../locale/#{locale}.yml", "config/locales/languages.#{locale}.yml"
+ def copy_locales
+ locales.each do |locale|
+ locale_file = "#{locale}.#{file_ext}"
+ file = File.join(locale_dir, locale_file)
+ copy_file File.join(locale_ref, locale_file), "config/locales/#{locale_name}.#{locale}.#{file_ext}" if file.exist?
end
- end
+ end
end
end
end
@@ -19,7 +19,7 @@
# See http://github.com/karmi/localized_country_select
#
-require 'localized_language_select/i18n'
+# require 'localized_language_select/i18n'
module LocalizedLanguageSelect
class << self
@@ -0,0 +1,87 @@
+require 'rubygems'
+require 'hpricot'
+require 'open-uri'
+
+# Rake task for importing language 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:language_select locale=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 language codes and names for various languages from the Unicode.org CLDR archive. Depends on Hpricot gem."
+ task :language_select do
+
+ # TODO : Implement locale import chooser from CLDR root via Highline
+
+ # Setup variables
+ locale = ENV['locale']
+ unless locale
+ puts "\n[!] Usage: rake import:language_select locale=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
+
+ require 'ruby-debug'
+
+ # ----- Parse the HTML with Hpricot ----------------------------------------
+ puts "... parsing the HTML file"
+ languages = []
+ doc.search("//tr").each do |row|
+ if row.search("td[@class='n']") &&
+ row.search("td[@class='n']").inner_html =~ /^nameslanguage$/ &&
+ row.search("td[@class='g']").inner_html =~ /^[a-z]{2,3}(?:_([A-Z][a-z]{3}))?(?:_([A-Z]{2}))?$/
+ code = row.search("td[@class='g']").inner_text
+ code.sub!('_','-')
+ #debugger if code =~ /-/
+ name = row.search("td[@class='v']").first.inner_text
+ languages << { :code => code.to_sym, :name => name.to_s }
+ print " ... #{name}"
+ end
+ end
+
+
+ # ----- Prepare the output format ------------------------------------------
+ output =<<HEAD
+{
+ :#{locale} => {
+ :languages => {
+HEAD
+ languages.each do |language|
+ output << " #{language[:code].inspect} => \"#{language[: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
@@ -0,0 +1,83 @@
+require 'rubygems'
+require 'hpricot'
+require 'open-uri'
+
+# Rake task for importing language 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 yaml_locales:language_select locale=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 :yaml_locales do
+
+ desc "Import language codes and names for various languages from the Unicode.org CLDR archive. Depends on Hpricot gem."
+ task :language_select do
+
+ # TODO : Implement locale import chooser from CLDR root via Highline
+
+ # Setup variables
+ locale = ENV['locale']
+ unless locale
+ puts "\n[!] Usage: rake yaml_locales:language_select locale=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
+
+ require 'ruby-debug'
+
+ # ----- Parse the HTML with Hpricot ----------------------------------------
+ puts "... parsing the HTML file"
+ languages = []
+ doc.search("//tr").each do |row|
+ if row.search("td[@class='n']") &&
+ row.search("td[@class='n']").inner_html =~ /^nameslanguage$/ &&
+ row.search("td[@class='g']").inner_html =~ /^[a-z]{2,3}(?:_([A-Z][a-z]{3}))?(?:_([A-Z]{2}))?$/
+ code = row.search("td[@class='g']").inner_text
+ code.sub!('_','-')
+ #debugger if code =~ /-/
+ name = row.search("td[@class='v']").first.inner_text
+ languages << { :code => code.to_sym, :name => name.to_s }
+ print " ... #{name}"
+ end
+ end
+
+
+ # ----- Prepare the output format ------------------------------------------
+ output =<<HEAD
+#{locale}:
+ languages:
+HEAD
+ languages.each do |language|
+ output << " #{language[:code].inspect}: \"#{language[:name]}\"\n"
+ end
+ output <<<<TAIL
+TAIL
+
+
+ # ----- Write the parsed values into file ---------------------------------
+ puts "\n... writing the output"
+ filename = File.join(File.dirname(__FILE__), '..', 'locale', "languages.#{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 7291f43

Please sign in to comment.