Skip to content
Browse files

added Rails 3 generator

  • Loading branch information...
1 parent 26a280d commit 3ccb12ab87e88a74078ba36373990de889a36f27 @kristianmandrup kristianmandrup committed Jan 18, 2011
View
12 README.textile
@@ -14,7 +14,17 @@ You can easily translate language codes in your application like this:
<%= I18n.t @user.language, :scope => 'languages' %>
</pre>
-h2. Locale file generation
+h2. Generating language locale files
+
+The project comes with both rake tasks and a generator to generate and import locale files for languages:
+
+h3. Rails 3 generator
+
+The project now comes with a Rails 3 generator to import locale files and place them in config/locales in your Rails 3 app.
+
+@rails g localized_language_select:import de ru --file-ext yml@
+
+h3. Rake import tasks
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)
View
41 lib/generators/localized_language_select/import/content_helper.rb
@@ -0,0 +1,41 @@
+module Import
+ module ContentHelper
+ def languages_yaml_content
+ languages.inject([]) do |output, language|
+ output << "\t\t#{language[:code]}: \"#{language[:name]}\""
+ output
+ end.join("\n")
+ end
+
+ def languages_hash_content
+ languages.inject([]) do |output, language|
+ output << "\t\t:#{language[:code]} => \"#{language[:name]}\","
+ output
+ end.join("\n")
+ end
+
+ def get_output
+ send :"#{file_ext}_output"
+ end
+
+ def yaml_output
+ %Q{#{locale}:
+\tlanguages:
+#{languages_yaml_content}
+}
+ end
+ alias_method :yml_output, :yaml_output
+
+ def hash_output
+ output = <<HASH
+{
+\t:#{lang} => {
+\t\t:languages => {
+#{languages_hash_content}
+\t\t\t}
+\t}
+}
+HASH
+ end
+ end
+end
View
139 lib/generators/localized_language_select/import/import_generator.rb
@@ -1,3 +1,6 @@
+require 'generators/localized_language_select/import/parse_helper'
+require 'generators/localized_language_select/import/content_helper'
+
module LocalizedLanguageSelect
module Generators
class ImportGenerator < Rails::Generators::Base
@@ -16,136 +19,78 @@ def main_flow
protected
- def file_ext
- options[:file_ext]
- end
-
- def languages_yaml_content
- languages.inject([]) do |output, language|
- output << "\t\t\t#{language[:code]}: \"#{language[:name]}\""
- output
- end.join("\n")
- end
+ include Import::ParseHelper
+ include Import::ContentHelper
- def languages_hash_content
- languages.inject([]) do |output, language|
- output << "\t\t\t:#{language[:code]} => \"#{language[:name]}\","
- output
- end.join("\n")
- end
+ attr_accessor :languages, :locale, :doc
- def get_output lang, file_ext
- send :"#{file_ext}_output", lang
- end
-
- def yaml_output lang
- %Q{
-#{lang}:
- languages:
- #{languages_yaml_content}
-}
- end
-
- def hash_output
- output = <<HASH
-{
- :#{lang} => {
- :languages => {
- #{languages_hash_content}
- }
- }
-}
-HASH
+ def file_ext
+ options[:file_ext]
end
- def check_hpricot
- begin
- require 'hpricot'
- rescue LoadError
- puts "Error: Hpricot library required to use this task (localized_language_select:import)"
- exit
- end
- end
-
def import_languages
# Check lang variable
- locales.each do |lang|
- import_language lang
- end
- end
-
- def valid_lang? lang
- if lang == 'lang' || (/\A[a-z]{2}\z/).match(lang) == nil
- puts "\n[!] Usage: rails g localized_language_select:import de ru --file-ext yml\n\n"
- exit 0
+ locales.each do |locale|
+ @locale = locale
+ import_for_locale
end
end
- def import_language lang
- valid_lang? lang
+ def import_for_locale
+ valid_locale?
# ----- 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") )
+ @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_languages doc
+ parse_html_file
end
- def parse_languages doc
+ def parse_html_file
# ----- Parse the HTML with Hpricot ----------------------------------------
puts "... parsing the HTML file"
- languages = []
+
+ @languages = []
doc.search("//tr").each do |row|
next if !language_row?(row)
languages << { :code => get_code(row).to_sym, :name => get_name(row).to_s }
end
- generate_language_locales languages
+ write_locale_file
end
- def get_code row
- row.search("td[@class='g']").inner_text.sub('_','-')
- end
-
- def get_name row
- row.search("td[@class='v']").first.inner_text
- end
-
- def language_row? row
- row.search("td[@class='n']") && n_row?(row) && g_row?(row)
- end
-
- def n_row? row
- row.search("td[@class='n']").inner_html =~ /^nameslanguage$/
- end
-
- def g_row? row
- row.search("td[@class='g']").inner_html =~ /^[a-z]{2,3}(?:_([A-Z][a-z]{3}))?(?:_([A-Z]{2}))?$/
- end
-
- def generate_language_locales languages
- # ----- Prepare the output format ------------------------------------------
- languages.each do |lang|
- write_locale_file lang
- end
- end
-
- def write_locale_file locale
+ def write_locale_file
# ----- Write the parsed values into file ---------------------------------
puts "\n... writing the output"
- write_file locale
+
+ filename = File.join(Rails.root, 'config', 'locales', "languages.#{locale}.#{file_ext}")
+ filename += '.NEW' if File.exists?(filename) # Append 'NEW' if file exists
+ File.open(filename, 'w+') { |f| f << get_output }
+
puts "\n---\nWritten values for the '#{locale}' into file: #{filename}\n"
# ------------------------------------------------------------------------------
end
-
- def write_file locale
- filename = File.join(File.dirname(__FILE__), '..', 'locale', "languages.#{locale}.rb")
- filename += '.NEW' if File.exists?(filename) # Append 'NEW' if file exists
- File.open(filename, 'w+') { |f| f << get_output(lang) }
+
+ private
+
+ def valid_locale?
+ if (/\A[a-z]{2}\z/).match(locale) == nil
+ puts "\n[!] Usage: rails g localized_language_select:import de ru --file-ext yml\n\n"
+ exit 0
+ end
end
+
+ def check_hpricot
+ begin
+ require 'hpricot'
+ rescue LoadError
+ puts "Error: Hpricot library required to use this task (localized_language_select:import)"
+ exit
+ end
+ end
end
end
end
View
23 lib/generators/localized_language_select/import/parse_helper.rb
@@ -0,0 +1,23 @@
+module Import
+ module ParseHelper
+ def get_code row
+ row.search("td[@class='g']").inner_text.sub('_','-')
+ end
+
+ def get_name row
+ row.search("td[@class='v']").first.inner_text
+ end
+
+ def language_row? row
+ row.search("td[@class='n']") && n_row?(row) && g_row?(row)
+ end
+
+ def n_row? row
+ row.search("td[@class='n']").inner_html =~ /^nameslanguage$/
+ end
+
+ def g_row? row
+ row.search("td[@class='g']").inner_html =~ /^[a-z]{2,3}(?:_([A-Z][a-z]{3}))?(?:_([A-Z]{2}))?$/
+ end
+ end
+end

0 comments on commit 3ccb12a

Please sign in to comment.
Something went wrong with that request. Please try again.