locale-data-import 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,6 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ tasks/*
4
+ -
5
+ features/**/*.feature
6
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.1"
12
+ gem "rcov", ">= 0"
13
+ end
14
+
15
+ gem "hpricot"
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Camille Roux
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,35 @@
1
+ = LocaleDataImport
2
+
3
+ Rails plugin to easily generate locale files (.yml) from unicode.org locale data like territories or languages.
4
+
5
+ == Usage
6
+ To generate a locale you just have to run the task 'import:locale' with two parameters:
7
+ * locale
8
+ * type : the type of data you want ('territories' or 'languages')
9
+
10
+ rake import:locale locale=fr type=territories
11
+
12
+ == Install
13
+ Before you can use the rake task, add the necessary gem to your project's Gemfile as follows:
14
+
15
+ gem 'locale-data-import'
16
+
17
+ Then install the gem by running:
18
+
19
+ bundle install
20
+
21
+ == Contributing to locale-data-import
22
+
23
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
24
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
25
+ * Fork the project
26
+ * Start a feature/bugfix branch
27
+ * Commit and push until you are happy with your contribution
28
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
29
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
30
+
31
+ == Copyright
32
+
33
+ Copyright (c) 2010 Camille Roux. See LICENSE.txt for
34
+ further details.
35
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "locale-data-import"
16
+ gem.homepage = "http://github.com/camilleroux/locale-data-import"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Generate locales from Unicode.org data}
19
+ gem.description = %Q{Rails plugin to easily generate locale files (.yml) from unicode.org locale data like territories or languages.}
20
+ gem.email = "contact@camilleroux.com"
21
+ gem.authors = ["Camille Roux"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "locale-data-import #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,9 @@
1
+ require 'locale-data-import'
2
+ require 'rails'
3
+ module MyLocaleDataImport
4
+ class Railtie < Rails::Railtie
5
+ rake_tasks do
6
+ load "tasks/locale-data-import.rake"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module LocaleDataImport
2
+ require 'locale-data-import/railtie' if defined?(Rails)
3
+ end
@@ -0,0 +1,98 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'open-uri'
4
+
5
+ # Rake task for importing country names from Unicode.org's CLDR repository
6
+ # (http://www.unicode.org/cldr/data/charts/summary/root.html).
7
+ #
8
+ # It parses a HTML file from Unicode.org for given locale and saves the
9
+ # Rails' I18n hash in the plugin +locale+ directory
10
+ #
11
+ # Don't forget to restart the application when you add new locale to load it into Rails!
12
+ #
13
+ # == Example
14
+ # rake import:locale locale=fr type=territories
15
+ #
16
+ # The code is deliberately procedural and simple, so it's easily
17
+ # understandable by beginners as an introduction to Rake tasks power.
18
+ # See http://github.com/joshmh/cldr/tree/master/converter.rb for much more robust solution
19
+
20
+ namespace :import do
21
+
22
+ desc "Import locale data (code and name) for various languages from the Unicode.org CLDR archive. Depends on Hpricot gem."
23
+ task :locale do
24
+ begin
25
+ require 'hpricot'
26
+ rescue LoadError
27
+ puts "Error: Hpricot library required to use this task (import:locale)"
28
+ exit
29
+ end
30
+
31
+ # TODO : Implement locale import chooser from CLDR root via Highline
32
+
33
+ # Setup variables
34
+ locale = ENV['locale']
35
+ type = ENV['type']
36
+ unless locale && type
37
+ puts "\n[!] Usage: rake import:locale locale=fr type=territories\n\n"
38
+ exit 0
39
+ end
40
+ types = {'territories' => /^namesterritory$/, 'languages' => /^nameslanguage$/}
41
+ unless types.include? type
42
+ puts "\n[!] type must be 'territories' or 'languages'\n\n"
43
+ exit 0
44
+ end
45
+
46
+ # ----- Get the CLDR HTML --------------------------------------------------
47
+ begin
48
+ puts "... getting the HTML file for locale '#{locale}'"
49
+ doc = Hpricot( open("http://www.unicode.org/cldr/data/charts/summary/#{locale}.html") )
50
+ rescue => e
51
+ puts "[!] Invalid locale name '#{locale}'! Not found in CLDR (#{e})"
52
+ exit 0
53
+ end
54
+
55
+
56
+ # ----- Parse the HTML with Hpricot ----------------------------------------
57
+ puts "... parsing the HTML file"
58
+ translations = []
59
+ doc.search("//tr").each do |row|
60
+ if row.search("td[@class='n']") &&
61
+ row.search("td[@class='n']").inner_html =~ types[type] &&
62
+ (type != "territories" || row.search("td[@class='g']").inner_html =~ /^[A-Z]{2}$/) &&
63
+ (type != "languages" || row.search("td[@class='g']").inner_html =~ /^[a-zA-Z_]+$/)
64
+ code = row.search("td[@class='g']").inner_text
65
+ #code = code[-code.size, 2]
66
+ name = row.search("td[@class='v']").inner_text
67
+ translations << { :code => code.to_sym, :name => name.to_s }
68
+ print " ... #{name}"
69
+ end
70
+ end
71
+
72
+
73
+ # ----- Prepare the output format ------------------------------------------
74
+ output =<<HEAD
75
+ { :#{locale} => {
76
+
77
+ :translation => {
78
+ HEAD
79
+ translations.each do |translation|
80
+ output << "\t\t\t:#{translation[:code]} => \"#{translation[:name]}\",\n"
81
+ end
82
+ output <<<<TAIL
83
+ }
84
+
85
+ }
86
+ }
87
+ TAIL
88
+
89
+
90
+ # ----- Write the parsed values into file ---------------------------------
91
+ puts "\n... writing the output"
92
+ filename = File.join(Rails.root, 'config', 'locales', "#{type}.#{locale}.rb")
93
+ File.open(filename, 'w+') { |f| f << output }
94
+ puts "\n---\nWritten values for the '#{locale}' into file: #{filename}\n"
95
+ # ------------------------------------------------------------------------------
96
+ end
97
+
98
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'locale-data-import'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestLocaleDataImport < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: locale-data-import
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Camille Roux
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-30 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: hpricot
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ prerelease: false
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: shoulda
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 0
42
+ version: "0"
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: &id003 !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 1
55
+ - 0
56
+ - 0
57
+ version: 1.0.0
58
+ type: :development
59
+ prerelease: false
60
+ version_requirements: *id003
61
+ - !ruby/object:Gem::Dependency
62
+ name: jeweler
63
+ requirement: &id004 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 1
70
+ - 5
71
+ - 1
72
+ version: 1.5.1
73
+ type: :development
74
+ prerelease: false
75
+ version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ name: rcov
78
+ requirement: &id005 !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: *id005
89
+ description: Rails plugin to easily generate locale files (.yml) from unicode.org locale data like territories or languages.
90
+ email: contact@camilleroux.com
91
+ executables: []
92
+
93
+ extensions: []
94
+
95
+ extra_rdoc_files:
96
+ - LICENSE.txt
97
+ - README.rdoc
98
+ files:
99
+ - .document
100
+ - Gemfile
101
+ - LICENSE.txt
102
+ - README.rdoc
103
+ - Rakefile
104
+ - VERSION
105
+ - lib/locale-data-import.rb
106
+ - lib/locale-data-import/railtie.rb
107
+ - lib/tasks/locale-data-import.rake
108
+ - test/helper.rb
109
+ - test/test_locale-data-import.rb
110
+ has_rdoc: true
111
+ homepage: http://github.com/camilleroux/locale-data-import
112
+ licenses:
113
+ - MIT
114
+ post_install_message:
115
+ rdoc_options: []
116
+
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ hash: -149191227839144588
125
+ segments:
126
+ - 0
127
+ version: "0"
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ segments:
134
+ - 0
135
+ version: "0"
136
+ requirements: []
137
+
138
+ rubyforge_project:
139
+ rubygems_version: 1.3.7
140
+ signing_key:
141
+ specification_version: 3
142
+ summary: Generate locales from Unicode.org data
143
+ test_files:
144
+ - test/helper.rb
145
+ - test/test_locale-data-import.rb