Permalink
Browse files

initial

  • Loading branch information...
0 parents commit 7dd1e5d7995ccedecd770468eae90b5d41d08b81 @kristianmandrup committed May 14, 2012
@@ -0,0 +1,5 @@
+lib/**/*.rb
+bin/*
+-
+features/**/*.feature
+LICENSE.txt
@@ -0,0 +1,49 @@
+# rcov generated
+coverage
+coverage.data
+
+# rdoc generated
+rdoc
+
+# yard generated
+doc
+.yardoc
+
+# bundler
+.bundle
+
+# jeweler generated
+pkg
+
+# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
+#
+# * Create a file at ~/.gitignore
+# * Include files you want ignored
+# * Run: git config --global core.excludesfile ~/.gitignore
+#
+# After doing this, these files will be ignored in all your git projects,
+# saving you from having to 'pollute' every project you touch with them
+#
+# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
+#
+# For MacOS:
+#
+.DS_Store
+
+# For TextMate
+#*.tmproj
+#tmtags
+
+# For emacs:
+#*~
+#\#*
+#.\#*
+
+# For vim:
+#*.swp
+
+# For redcar:
+#.redcar
+
+# For rubinius:
+#*.rbc
1 .rspec
@@ -0,0 +1 @@
+--color
11 Gemfile
@@ -0,0 +1,11 @@
+source :rubygems
+
+gem 'hpricot', '~> 0.8'
+
+group :development do
+ gem "rspec", "~> 2.8.0"
+ gem "rdoc", "~> 3.12"
+ gem "bundler", "~> 1.1.0"
+ gem "jeweler", "~> 1.8.3"
+ gem "simplecov", ">= 0"
+end
@@ -0,0 +1,20 @@
+Copyright (c) 2012 Kristian Mandrup
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,49 @@
+## i18n generators
+
+Rails gem to provide support generation of localized language and country names for use in various localization contexts. This gem was extracted from from the gems *localized_countrues_select* and *localized_languages_select*
+
+Please all see the following:
+
+* [countries and languages](https://github.com/grosser/countries_and_languages)
+* [i18n data](https://github.com/grosser/i18n_data)
+
+## Installation
+
+in Gemfile:
+
+`gem "i18n-gen", "~> 0.2.0"`
+
+Then run `bundle` to install.
+
+## Usage
+
+### Generators
+
+`rails g`
+
+* localized:languages_import
+* localized:locale_languages
+
+Use `-h` option for help on usage
+
+`rails g localized:languages_import -h`
+
+### Rake tasks
+
+`rake -T`
+
+#### localized_countries tasks
+
+localized_countries:generate
+
+`rake localized_countries:generate locale=de`
+
+#### localized_languages tasks
+
+localized_languages:generate
+
+`rake localized_languages:generate locale=de`
+
+## Copyright
+
+(c) 2012 Kristian Mandrup
@@ -0,0 +1,49 @@
+# encoding: utf-8
+
+require 'rubygems'
+require 'bundler'
+begin
+ Bundler.setup(:default, :development)
+rescue Bundler::BundlerError => e
+ $stderr.puts e.message
+ $stderr.puts "Run `bundle install` to install missing gems"
+ exit e.status_code
+end
+require 'rake'
+
+require 'jeweler'
+Jeweler::Tasks.new do |gem|
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
+ gem.name = "i18n-gen"
+ gem.homepage = "http://github.com/kristianmandrup/countries_and_languages"
+ gem.license = "MIT"
+ gem.summary = %Q{Generate i18n data for countries and languages}
+ gem.description = %Q{Generators and rake tasks for i18n data}
+ gem.email = "kmandrup@gmail.com"
+ gem.authors = ["Kristian Mandrup"]
+ # dependencies defined in Gemfile
+end
+Jeweler::RubygemsDotOrgTasks.new
+
+require 'rspec/core'
+require 'rspec/core/rake_task'
+RSpec::Core::RakeTask.new(:spec) do |spec|
+ spec.pattern = FileList['spec/**/*_spec.rb']
+end
+
+RSpec::Core::RakeTask.new(:rcov) do |spec|
+ spec.pattern = 'spec/**/*_spec.rb'
+ spec.rcov = true
+end
+
+task :default => :spec
+
+require 'rdoc/task'
+Rake::RDocTask.new do |rdoc|
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
+
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = "countries_and_languages #{version}"
+ rdoc.rdoc_files.include('README*')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1 @@
+0.1.0
@@ -0,0 +1,41 @@
+module Import
+ module ContentHelper
+ def languages_yaml_content
+ languages.inject([]) do |output, language|
+ output << " #{language[:code]}: \"#{language[:name]}\""
+ output
+ end.join("\n")
+ end
+
+ def languages_hash_content
+ languages.inject([]) do |output, language|
+ output << " :#{language[:code]} => \"#{language[:name]}\","
+ output
+ end.join("\n")
+ end
+
+ def get_output
+ send :"#{file_ext}_output"
+ end
+
+ def yaml_output
+ %Q{#{locale}:
+ languages:
+#{languages_yaml_content}
+}
+ end
+ alias_method :yml_output, :yaml_output
+
+ def hash_output
+ output = <<HASH
+{
+ :#{lang} => {
+ :languages => {
+#{languages_hash_content}
+ }
+ }
+}
+HASH
+ end
+ end
+end
@@ -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,0 +1,96 @@
+require 'generators/localized/import/parse_helper'
+require 'generators/localized/import/content_helper'
+
+module Localized
+ module Generators
+ class LanguagesImportGenerator < Rails::Generators::Base
+ desc "Import language locale files"
+
+ argument :locales, :type => :array, :default => [], :desc => 'locales to generate for'
+ class_option :file_ext, :type => :string, :default => 'yml', :desc => 'file extension for locale files to import'
+
+ source_root File.dirname(__FILE__)
+
+ def main_flow
+ raise ArgumentError, "File extension must be yml or rb" if ![:rb, :yml, :yaml].include?(file_ext.to_sym)
+ check_hpricot
+ import_languages
+ end
+
+ protected
+
+ include Import::ParseHelper
+ include Import::ContentHelper
+
+ attr_accessor :languages, :locale, :doc
+
+ def file_ext
+ options[:file_ext]
+ end
+
+ def import_languages
+ # Check lang variable
+ locales.each do |locale|
+ @locale = locale
+ import_for_locale
+ end
+ end
+
+ 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") )
+ rescue => e
+ puts "[!] Invalid locale name '#{locale}'! Not found in CLDR (#{e})"
+ exit 0
+ end
+ # require 'ruby-debug'
+ parse_html_file
+ end
+
+ def parse_html_file
+ # ----- Parse the HTML with Hpricot ----------------------------------------
+ puts "... parsing the HTML file"
+
+ @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
+ write_locale_file
+ end
+
+ def write_locale_file
+ # ----- Write the parsed values into file ---------------------------------
+ puts "\n... writing the output"
+
+ 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
+
+ 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
Oops, something went wrong.

0 comments on commit 7dd1e5d

Please sign in to comment.