countries_and_languages 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source :rubygems
2
+ gemspec
3
+
4
+ group :dev do
5
+ gem 'activesupport'
6
+ gem 'actionpack'
7
+ gem 'rake'
8
+ gem 'rspec', '~>2'
9
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,65 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ countries_and_languages (0.1.0)
5
+ i18n_data
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ actionpack (3.1.1)
11
+ activemodel (= 3.1.1)
12
+ activesupport (= 3.1.1)
13
+ builder (~> 3.0.0)
14
+ erubis (~> 2.7.0)
15
+ i18n (~> 0.6)
16
+ rack (~> 1.3.2)
17
+ rack-cache (~> 1.1)
18
+ rack-mount (~> 0.8.2)
19
+ rack-test (~> 0.6.1)
20
+ sprockets (~> 2.0.2)
21
+ activemodel (3.1.1)
22
+ activesupport (= 3.1.1)
23
+ builder (~> 3.0.0)
24
+ i18n (~> 0.6)
25
+ activesupport (3.1.1)
26
+ multi_json (~> 1.0)
27
+ builder (3.0.0)
28
+ diff-lcs (1.1.3)
29
+ erubis (2.7.0)
30
+ hike (1.2.1)
31
+ i18n (0.6.0)
32
+ i18n_data (0.2.7)
33
+ activesupport
34
+ multi_json (1.0.3)
35
+ rack (1.3.5)
36
+ rack-cache (1.1)
37
+ rack (>= 0.4)
38
+ rack-mount (0.8.3)
39
+ rack (>= 1.0.0)
40
+ rack-test (0.6.1)
41
+ rack (>= 1.0)
42
+ rake (0.9.2)
43
+ rspec (2.6.0)
44
+ rspec-core (~> 2.6.0)
45
+ rspec-expectations (~> 2.6.0)
46
+ rspec-mocks (~> 2.6.0)
47
+ rspec-core (2.6.4)
48
+ rspec-expectations (2.6.0)
49
+ diff-lcs (~> 1.1.2)
50
+ rspec-mocks (2.6.0)
51
+ sprockets (2.0.3)
52
+ hike (~> 1.2)
53
+ rack (~> 1.0)
54
+ tilt (~> 1.1, != 1.3.0)
55
+ tilt (1.3.3)
56
+
57
+ PLATFORMS
58
+ ruby
59
+
60
+ DEPENDENCIES
61
+ actionpack
62
+ activesupport
63
+ countries_and_languages!
64
+ rake
65
+ rspec (~> 2)
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ require "bundler"
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ task :default do
5
+ sh "rspec spec/"
6
+ end
7
+
8
+ rule /^version:bump:.*/ do |t|
9
+ sh "git status | grep 'nothing to commit'" # ensure we are not dirty
10
+ index = ['major', 'minor','patch'].index(t.name.split(':').last)
11
+ file = 'lib/countries_and_languages/version.rb'
12
+
13
+ version_file = File.read(file)
14
+ old_version, *version_parts = version_file.match(/(\d+)\.(\d+)\.(\d+)/).to_a
15
+ version_parts[index] = version_parts[index].to_i + 1
16
+ new_version = version_parts * '.'
17
+ File.open(file,'w'){|f| f.write(version_file.sub(old_version, new_version)) }
18
+
19
+ sh "bundle && git add #{file} Gemfile.lock && git commit -m 'bump version to #{new_version}'"
20
+ end
data/Readme.md ADDED
@@ -0,0 +1,42 @@
1
+ Countries and languages in I18n.locale
2
+ ======================================
3
+ In currently 85 languages supplied by [I18nData](http://github.com/grosser/i18n_data).<br/>
4
+ Languages and countries are stored as iso 639/iso 3166 - 2 letter code.<br/>
5
+ Methods are available in views.
6
+
7
+ @user.country == 'de'
8
+ @user.language == 'de'
9
+
10
+ User is from <%=country(@user.country)%> and speaks <%=language(@user.language)%>
11
+ User is from Germany and speaks German.
12
+
13
+ Please select you language <%=f.select_tag(:language,options_for_select(languages))%>
14
+ Select box with all 185 localized languages.
15
+
16
+ If you have special needs like renaming countries or only displaying a subset,
17
+ have a look at the code and overwrite as necessary.
18
+
19
+ DEMO can be found in [Gettext example application](http://github.com/grosser/gettext_i18n_rails_example)
20
+
21
+ Install
22
+ =====
23
+
24
+ As Gem
25
+
26
+ gem install countries_and_languages
27
+
28
+ As Rails Plugin
29
+
30
+ rails plugin install git://github.com/grosser/countries_and_languages.git
31
+
32
+ # Gemfile
33
+ gem 'i18n_data'
34
+
35
+
36
+ Author
37
+ ======
38
+ [Michael Grosser](http://grosser.it)<br/>
39
+ michael@grosser.it<br/>
40
+ Hereby placed under public domain, do what you want, just do not hold me accountable...<br/>
41
+ [![Flattr](http://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=grosser&url=https://github.com/grosser/countries_and_languages&title=countries_and_languages&language=en_US&tags=github&category=software)
42
+ [![Build Status](https://secure.travis-ci.org/grosser/countries_and_languages.png)](http://travis-ci.org/grosser/countries_and_languages)
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
2
+ name = 'countries_and_languages'
3
+ require "#{name}/version"
4
+
5
+ Gem::Specification.new name, CountriesAndLanguages::VERSION do |s|
6
+ s.summary = "Countries and languages in I18n.locale for select_tag or output in 85 languages"
7
+ s.authors = ["Michael Grosser"]
8
+ s.email = "michael@grosser.it"
9
+ s.homepage = "http://github.com/grosser/#{name}"
10
+ s.files = `git ls-files`.split("\n")
11
+ s.add_dependency 'i18n_data'
12
+ end
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'countries_and_languages'
2
+ ActionView::Base.send(:include,CountriesAndLanguages::Helpers)
@@ -0,0 +1,3 @@
1
+ module CountriesAndLanguages
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,49 @@
1
+ # encoding: UTF-8
2
+ module CountriesAndLanguages
3
+ extend self
4
+
5
+ module Helpers
6
+ def countries
7
+ @@countries ||= {}
8
+ @@countries[I18n.locale] ||= CountriesAndLanguages.clean_and_sort(I18nData.countries(I18n.locale.to_s.upcase))
9
+ end
10
+
11
+ def country(country_code)
12
+ countries.rassoc(country_code.to_s.upcase)[0] rescue ''
13
+ end
14
+
15
+ def languages
16
+ @@languages ||= {}
17
+ @@languages[I18n.locale] ||= CountriesAndLanguages.clean_and_sort(I18nData.languages(I18n.locale.to_s.upcase))
18
+ end
19
+
20
+ def language(language_code)
21
+ languages.rassoc(language_code.to_s.upcase)[0] rescue ''
22
+ end
23
+ end
24
+
25
+ def clean_and_sort(data)
26
+ data = data.to_a.sort_by{|code,name| convert_umlaut_to_base(name)}
27
+ data.map!{|code,name|[clean_name(name),code]}
28
+ end
29
+
30
+ def clean_name(name)
31
+ #General fixes
32
+ name = name.sub(/\s*[,;(].*/,'')
33
+
34
+ #German fixes
35
+ name.sub!(/-Sprache$/,'')
36
+ name.sub!(/ Peoples Democratic Republics Democratic Republic/,'')#Lao
37
+ name.sub!(/Demokratische Republik /,'')#Congo
38
+
39
+ name
40
+ end
41
+
42
+ def convert_umlaut_to_base(input)
43
+ $KCODE='u'
44
+ %w(aáä AÁÄÅ oóö OÓÖ ií IÍ uúü UÚÜ eé EÉ sß).inject(input.dup) do |input, set|
45
+ to, *from = set.split('')
46
+ input.gsub(/[#{from}]/, to)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,73 @@
1
+ # encoding: UTF-8
2
+ require File.expand_path("spec_helper", File.dirname(__FILE__))
3
+
4
+ include CountriesAndLanguages::Helpers
5
+
6
+ describe CountriesAndLanguages do
7
+ before do
8
+ I18n.locale = :en
9
+ end
10
+
11
+ {:country=>['Germany','Deutschland'],:language=>['German','Deutsch']}.each do |method,translation|
12
+ it "translates #{method}" do
13
+ send(method,'DE').should == translation[0]
14
+ I18n.locale = :de
15
+ send(method,'DE').should == translation[1]
16
+ end
17
+
18
+ it "returns blank when not found" do
19
+ send(method,'XC').should be_blank
20
+ end
21
+ end
22
+
23
+ describe 'clean names' do
24
+ it "removes everything in braces" do
25
+ country('va').should == 'Holy See'
26
+ language('IA').should == 'Interlingua'
27
+ end
28
+ it "removes everything behind comma" do
29
+ country('IR').should == 'Iran'
30
+ language('ND').should == 'Ndebele'
31
+ end
32
+ it "removes everything behind semicolon" do
33
+ language('nb').should == 'Bokmål'
34
+ language('ca').should == 'Catalan'
35
+ end
36
+ end
37
+
38
+ it "sorts umlaut-aware" do
39
+ I18n.locale = :de
40
+ countries[1][0].should == 'Ägypten'
41
+ end
42
+
43
+ describe 'misc fixes' do
44
+ describe 'German' do
45
+ before {I18n.locale = :de}
46
+ it "removes -Sprache aditions" do
47
+ language('ZU').should == "Zulu"
48
+ end
49
+ it "knows Kongo" do
50
+ country('CD').should == 'Kongo'
51
+ end
52
+ it "knows Lao" do
53
+ country('LA').should == 'Lao'
54
+ end
55
+ end
56
+ end
57
+
58
+ describe :select_tag do
59
+ def h
60
+ ActionController::Base.helpers
61
+ end
62
+
63
+ it "can use countries for a select tag" do
64
+ select = h.select_tag('x',h.options_for_select(countries))
65
+ select.split("\n").last.should == %Q[<option value=\"ZW\">Zimbabwe</option></select>]
66
+ end
67
+
68
+ it "can use languages for a select tag" do
69
+ select = h.select_tag('x',h.options_for_select(languages))
70
+ select.split("\n").last.should == %Q[<option value=\"ZU\">Zulu</option></select>]
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ $LOAD_PATH << 'lib'
3
+ require 'active_support'
4
+ require 'action_controller'
5
+ require 'i18n_data'
6
+ require 'countries_and_languages'
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: countries_and_languages
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael Grosser
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: i18n_data
16
+ requirement: &76023630 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *76023630
25
+ description:
26
+ email: michael@grosser.it
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files: []
30
+ files:
31
+ - Gemfile
32
+ - Gemfile.lock
33
+ - Rakefile
34
+ - Readme.md
35
+ - countries_and_languages.gemspec
36
+ - init.rb
37
+ - lib/countries_and_languages.rb
38
+ - lib/countries_and_languages/version.rb
39
+ - spec/countries_and_languages_spec.rb
40
+ - spec/spec_helper.rb
41
+ homepage: http://github.com/grosser/countries_and_languages
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ segments:
54
+ - 0
55
+ hash: 199256531
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ segments:
63
+ - 0
64
+ hash: 199256531
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 1.8.10
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Countries and languages in I18n.locale for select_tag or output in 85 languages
71
+ test_files: []