bcp47 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -6
- data/Gemfile.lock +17 -0
- data/Guardfile +24 -0
- data/VERSION +1 -1
- data/bcp47.gemspec +14 -2
- data/data/iso-3166-1.yml +492 -0
- data/data/iso-639-1.yml +484 -0
- data/lib/bcp47.rb +3 -0
- data/lib/bcp47/subtag/base.rb +32 -0
- data/lib/bcp47/subtag/language.rb +19 -0
- data/lib/bcp47/subtag/region.rb +7 -0
- data/spec/fixtures/base.yml +6 -0
- data/spec/lib/bcp47/subtag/base_spec.rb +48 -0
- data/spec/lib/bcp47/subtag/language_spec.rb +57 -0
- data/spec/lib/bcp47/subtag/region_spec.rb +17 -0
- metadata +31 -11
- data/spec/bcp47_spec.rb +0 -7
data/lib/bcp47.rb
CHANGED
@@ -0,0 +1,32 @@
|
|
1
|
+
module BCP47
|
2
|
+
module Subtag
|
3
|
+
class Base
|
4
|
+
attr_reader :code, :name
|
5
|
+
|
6
|
+
def initialize(code, options={})
|
7
|
+
@code = code
|
8
|
+
@name = options[:name]
|
9
|
+
end
|
10
|
+
|
11
|
+
def full_name
|
12
|
+
"#{code} - #{name}"
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.all
|
16
|
+
YAML.load_file(self::DEFINITIONS_FILE).map do |code, options|
|
17
|
+
symbolized_options = {}
|
18
|
+
options.keys.each { |key| symbolized_options[key.to_sym] = options[key] }
|
19
|
+
new(code, symbolized_options)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.find(code)
|
24
|
+
all.find {|subtag| subtag.code == code }
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.default
|
28
|
+
find(self::DEFAULT_CODE)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class BCP47::Subtag::Language < BCP47::Subtag::Base
|
2
|
+
DEFINITIONS_FILE = "data/iso-639-1.yml"
|
3
|
+
DEFAULT_PLURAL_RULE_NAMES = %w(one other)
|
4
|
+
DEFAULT_DIRECTION = 'ltr'
|
5
|
+
DEFAULT_CODE = 'en'
|
6
|
+
PLURAL_RULE_NAMES = %w(zero one two few many other)
|
7
|
+
|
8
|
+
attr_reader :plural_rule_names, :direction
|
9
|
+
|
10
|
+
def initialize(code, options={})
|
11
|
+
@plural_rule_names = options[:plural_rule_names] || DEFAULT_PLURAL_RULE_NAMES
|
12
|
+
@direction = options[:direction] || DEFAULT_DIRECTION
|
13
|
+
super(code, options)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.identify(full_code)
|
17
|
+
full_code =~ /^([a-z]{2})[-_]?/ ? find($1) : nil
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Subtag < BCP47::Subtag::Base
|
4
|
+
DEFINITIONS_FILE = "spec/fixtures/base.yml"
|
5
|
+
DEFAULT_CODE = "fr"
|
6
|
+
end
|
7
|
+
|
8
|
+
describe BCP47::Subtag::Base do
|
9
|
+
describe "full_name" do
|
10
|
+
it "is composed of the code and the name" do
|
11
|
+
full_name = Subtag.find('fr').full_name
|
12
|
+
full_name.should match(/fr/)
|
13
|
+
full_name.should match(/French/)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe ".all" do
|
18
|
+
it "gets its definition from the DEFINITIONS_FILE" do
|
19
|
+
subtags = Subtag.all
|
20
|
+
subtags[0].code.should == 'en'
|
21
|
+
subtags[1].code.should == 'fr'
|
22
|
+
subtags[2].code.should == 'de'
|
23
|
+
subtags[0].name.should == 'English'
|
24
|
+
subtags[1].name.should == 'French'
|
25
|
+
subtags[2].name.should == 'German'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe ".find(code)" do
|
30
|
+
it "finds a subtag by code" do
|
31
|
+
subtag = Subtag.find('de')
|
32
|
+
subtag.code.should == 'de'
|
33
|
+
subtag.name.should == 'German'
|
34
|
+
end
|
35
|
+
|
36
|
+
it "returns nil when no language can be found" do
|
37
|
+
Subtag.find('xxxx').should be_nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe ".default" do
|
42
|
+
it "finds from DEFAULT_CODE" do
|
43
|
+
default = Subtag.default
|
44
|
+
default.code.should == 'fr'
|
45
|
+
default.name.should == 'French'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe BCP47::Subtag::Language do
|
4
|
+
let(:language) { BCP47::Subtag::Language.new('de', name: 'German') }
|
5
|
+
|
6
|
+
it "is a BCP47::Subtag" do
|
7
|
+
language.should be_kind_of(BCP47::Subtag::Base)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "has a code" do
|
11
|
+
language.code.should == 'de'
|
12
|
+
end
|
13
|
+
|
14
|
+
it "has a name" do
|
15
|
+
language.name.should == 'German'
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "#plural_rule_names" do
|
19
|
+
it "defaults to %w(one other)" do
|
20
|
+
language.plural_rule_names.should == BCP47::Subtag::Language::DEFAULT_PLURAL_RULE_NAMES
|
21
|
+
end
|
22
|
+
|
23
|
+
it "is overwriteable" do
|
24
|
+
language = BCP47::Subtag::Language.new('ja', plural_rule_names: ['other'])
|
25
|
+
language.plural_rule_names.should == ['other']
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#direction" do
|
30
|
+
it "defaults to 'ltr'" do
|
31
|
+
language.direction.should == 'ltr'
|
32
|
+
end
|
33
|
+
|
34
|
+
it "is overwriteable" do
|
35
|
+
language = BCP47::Subtag::Language.new('ar', direction: :rtl)
|
36
|
+
language.direction.should == :rtl
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe ".identify(full_code)" do
|
41
|
+
it "identifies from 'de'" do
|
42
|
+
BCP47::Subtag::Language.identify('de').code.should == 'de'
|
43
|
+
end
|
44
|
+
|
45
|
+
it "identifies from 'fr-CH'" do
|
46
|
+
BCP47::Subtag::Language.identify('fr').code.should == 'fr'
|
47
|
+
end
|
48
|
+
|
49
|
+
it "identifies from 'es_MX" do
|
50
|
+
BCP47::Subtag::Language.identify('es_MX').code.should == 'es'
|
51
|
+
end
|
52
|
+
|
53
|
+
it "returns nil when it can't identify" do
|
54
|
+
BCP47::Subtag::Language.identify('gsw').should be_nil
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe BCP47::Subtag::Region do
|
4
|
+
describe ".identify(full_code)" do
|
5
|
+
it "identifies from 'fr-CH'" do
|
6
|
+
BCP47::Subtag::Region.identify('fr-CH').code.should == 'CH'
|
7
|
+
end
|
8
|
+
|
9
|
+
it "identifies from 'es_MX" do
|
10
|
+
BCP47::Subtag::Region.identify('es_MX').code.should == 'MX'
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns nil when it can't identify" do
|
14
|
+
BCP47::Subtag::Region.identify('gsw').should be_nil
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bcp47
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-08-23 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70154835719580 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 2.8.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70154835719580
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rdoc
|
27
|
-
requirement: &
|
27
|
+
requirement: &70154835719100 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '3.12'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70154835719100
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: bundler
|
38
|
-
requirement: &
|
38
|
+
requirement: &70154835718620 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 1.1.0
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70154835718620
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: jeweler
|
49
|
-
requirement: &
|
49
|
+
requirement: &70154835718140 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,7 +54,18 @@ dependencies:
|
|
54
54
|
version: 1.8.4
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70154835718140
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: guard-rspec
|
60
|
+
requirement: &70154835717660 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70154835717660
|
58
69
|
description: A subset of the BCP47 spec implemented in ruby
|
59
70
|
email: chris@tigrish.com
|
60
71
|
executables: []
|
@@ -67,13 +78,22 @@ files:
|
|
67
78
|
- .rspec
|
68
79
|
- Gemfile
|
69
80
|
- Gemfile.lock
|
81
|
+
- Guardfile
|
70
82
|
- LICENSE.txt
|
71
83
|
- README.rdoc
|
72
84
|
- Rakefile
|
73
85
|
- VERSION
|
74
86
|
- bcp47.gemspec
|
87
|
+
- data/iso-3166-1.yml
|
88
|
+
- data/iso-639-1.yml
|
75
89
|
- lib/bcp47.rb
|
76
|
-
-
|
90
|
+
- lib/bcp47/subtag/base.rb
|
91
|
+
- lib/bcp47/subtag/language.rb
|
92
|
+
- lib/bcp47/subtag/region.rb
|
93
|
+
- spec/fixtures/base.yml
|
94
|
+
- spec/lib/bcp47/subtag/base_spec.rb
|
95
|
+
- spec/lib/bcp47/subtag/language_spec.rb
|
96
|
+
- spec/lib/bcp47/subtag/region_spec.rb
|
77
97
|
- spec/spec_helper.rb
|
78
98
|
homepage: http://github.com/tigrish/bcp47
|
79
99
|
licenses:
|
@@ -90,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
90
110
|
version: '0'
|
91
111
|
segments:
|
92
112
|
- 0
|
93
|
-
hash:
|
113
|
+
hash: 4505930821952222917
|
94
114
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
115
|
none: false
|
96
116
|
requirements:
|