country_to_locales_mapping 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ require "country_to_locales_mapping/version"
2
+ require "country_to_locales_mapping/mapping"
3
+
4
+ module CountryToLocalesMapping
5
+
6
+ module_function
7
+
8
+ def locale_country_codes(l)
9
+ Mapping::instance.locale_country_codes(l)
10
+ end
11
+
12
+ def country_code_locales(c)
13
+ Mapping::instance.country_code_locales(c)
14
+ end
15
+ end
@@ -0,0 +1,105 @@
1
+ # (c) Copyright 2017 Ribose Inc.
2
+ #
3
+
4
+ require "json"
5
+ require "singleton"
6
+
7
+ module CountryToLocalesMapping
8
+ #
9
+ # Provides a mapping between ISO3166 country codes and RFC4646 locale
10
+ # codes. Locale codes are placed in order of usage in the region
11
+ # (e.g. de-CH > fr-CH > it-CH).
12
+ #
13
+ # Country-Locale data is stored in:
14
+ # root/config/i18n/country_locale_map.csv
15
+ #
16
+ # Country-Locale csv columns:
17
+ #
18
+ # ISO3166 Country Code,
19
+ # Country name, (not used in processing)
20
+ # RFC4646 Locale code,
21
+ # (all columns after are RFC4646 Locale codes)
22
+ #
23
+ # RFC4646 Locale codes are ordered to indicate preference.
24
+ # i.e. locale code in column 3 is more important than the locale code
25
+ # in column 5 for a country.
26
+ #
27
+ # First Locale code always reflects the official language of country
28
+ # (but might not be only official language, e.g. CH).
29
+ #
30
+ #
31
+ # Sources considered:
32
+ # http://www.i18nguy.com/unicode/language-identifiers.html
33
+ # http://www.infoplease.com/ipa/A0855611.html
34
+ #
35
+
36
+ #
37
+ # TODO: Give each locale a preference value: e.g. de-CH 0.6, fr-CH 0.3
38
+ #
39
+ class Mapping
40
+ include Singleton
41
+ attr_accessor :cc, :ll
42
+
43
+ def initialize
44
+ @cc = {}
45
+ @ll = {}
46
+ import_locale_map
47
+ end
48
+
49
+ # Imports country-locale map to memory for upcoming
50
+ # queries
51
+ def import_locale_map
52
+ json = JSON.parse(File.read(path_to_json))
53
+
54
+ json["countries"].each_pair do |country_code, country_data|
55
+ country_name = country_data["name"]
56
+ languages = country_data["locales"]
57
+
58
+ associate_country_with_locales(country_name, country_code, languages)
59
+ end
60
+ end
61
+
62
+ # Returns an array of country codes that match the given locale code.
63
+ #
64
+ # Note: Since in our current mapping, "en" is not assigned to any
65
+ # country, so "en" will not match anything while "en-us" will return
66
+ # "us".
67
+ #
68
+ def locale_country_codes(l)
69
+ if l.nil? || !@ll.has_key?(l)
70
+ raise ArgumentError, "Locale not recognized: #{l}"
71
+ end
72
+
73
+ @ll[l][:ccodes]
74
+ end
75
+
76
+ # Returns an array of locale ids that match the given country code.
77
+ #
78
+ def country_code_locales(c)
79
+ if c.nil? || !@cc.has_key?(c)
80
+ raise ArgumentError, "Country code not recognized: #{c}"
81
+ end
82
+
83
+ @cc[c][:locales]
84
+ end
85
+
86
+ private
87
+
88
+ def associate_country_with_locales(country_name, country_code, languages)
89
+ @cc[country_code] = {
90
+ name: country_name,
91
+ locales: languages,
92
+ }
93
+
94
+ languages.each do |l|
95
+ @ll[l] ||= {}
96
+ @ll[l][:ccodes] ||= []
97
+ @ll[l][:ccodes].push country_code
98
+ end
99
+ end
100
+
101
+ def path_to_json
102
+ File.expand_path("../../../data/country_locale_map.json", __FILE__)
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,3 @@
1
+ module CountryToLocalesMapping
2
+ VERSION = "0.1.0".freeze
3
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: country_to_locales_mapping
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ribose Inc.
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - open.source@ribose.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".editorconfig"
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".rubocop.ribose.yml"
66
+ - ".rubocop.tb.yml"
67
+ - ".rubocop.yml"
68
+ - ".travis.yml"
69
+ - Gemfile
70
+ - LICENSE.txt
71
+ - README.adoc
72
+ - Rakefile
73
+ - bin/console
74
+ - bin/setup
75
+ - country_to_locales_mapping.gemspec
76
+ - data/country_locale_map.json
77
+ - lib/country_to_locales_mapping.rb
78
+ - lib/country_to_locales_mapping/mapping.rb
79
+ - lib/country_to_locales_mapping/version.rb
80
+ homepage: https://github.com/riboseinc/country_to_locales_mapping
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.6.14
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Languages spoken in each country, distributed as gem
104
+ test_files: []