Class: IANA::LanguageSubtagRegistry
- Inherits:
-
Object
- Object
- IANA::LanguageSubtagRegistry
- Defined in:
- lib/iana/lsr.rb
Overview
Get the Language Subtag Registry data from the IANA site or a text file and load it into an array of hashes for further processing.
IANA’s Language Subtag Registry incorporates multiple standards for the definition of language- and locale-defining strings, including: ISO 3166: English country names and code elements ( www.iso.org/iso/english_country_names_and_code_elements ) ISO 639.2: Codes for the Representation of Names of Languages ( www.loc.gov/standards/iso639-2/php/code_list.php ) The registry’s format is specified in RFC 4646 ( www.ietf.org/rfc/rfc4646.txt )
Constant Summary collapse
- Keys =
Entries always have these keys: Type, [Tag or Subtag], Description, Added All keys used in the IANA format:
[ "Type", "Tag", "Subtag", "Description", "Added", "Preferred-Value", "Deprecated", "Suppress-Script", "Prefix", "Comments" ]
- IANAHost =
'www.iana.org'- IANAPath =
'/assignments/language-subtag-registry'- IANAFile =
File.dirname(__FILE__) + "/../../example-data/lsr.txt"
[]
- @@file_date =
""
Class Method Summary collapse
-
.dump(format = "iana") ⇒ Object
Dump data to: IANA’s flat text format (String) YAML (String) A hash of hashes, with the keys being the tags (Hash).
- .file_date ⇒ Object
-
.get(path = IANAPath, host = IANAHost) ⇒ Object
Load data directly from IANA site.
-
.load(text) ⇒ Object
Load data from text string.
-
.open(filename = IANAFile) ⇒ Object
Load data from a text file.
- .tags ⇒ Object
Class Method Details
.dump(format = "iana") ⇒ Object
Dump data to: IANA’s flat text format (String) YAML (String) A hash of hashes, with the keys being the tags (Hash)
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/iana/lsr.rb', line 103 def self.dump(format = "iana") begin format = format.strip.downcase rescue raise ArgumentError, "Invalid argument type; expected String" end case format when "iana" new_text = "File-Date: " + @@file_date + $/ + "%%" + $/ new_text << @@tags.map do |item| Keys.map do |key| val = item[key] if val if val.kind_of? Array val.map { |i| key + ": " + i.to_s }.join($/) else key + ": " + val.to_s end else nil end end.compact.join($/) + $/ end.join('%%' + $/) when "yaml" require 'yaml' YAML::dump([@@file_date, @@tags]) when "hash" taghash = {} @@tags.each do |tag| tag = tag.clone val = tag.delete("Tag") || tag.delete("Subtag") taghash[val.to_s] = tag end taghash else raise ArgumentError, "Invalid format option" end end |
.file_date ⇒ Object
45 46 47 |
# File 'lib/iana/lsr.rb', line 45 def self.file_date @@file_date end |
.get(path = IANAPath, host = IANAHost) ⇒ Object
Load data directly from IANA site
50 51 52 53 54 55 |
# File 'lib/iana/lsr.rb', line 50 def self.get(path=IANAPath, host=IANAHost) require 'net/http' site = Net::HTTP.new host response = site.request_get path self.load(response.body) end |
.load(text) ⇒ Object
Load data from text string
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/iana/lsr.rb', line 64 def self.load(text) @@tags = [] @@file_date = "" item = {} prev = [] text.each do |line| line.chomp! case line when /^%%$/ (@@tags << item; item = {}) unless item.empty? # separator; append last entry, if present when /^File-Date: .*/ @@file_date = line.gsub('File-Date: ','') # File-date entry when /^ [^ ].*/ item[prev].kind_of?(Array) ? item[prev].last += " " + line.strip : item[prev] += " " + line.strip # continuation line else # everything else (the actual key: value pairs) key, val = line.split(':', 2) # the main pair (ie, "Subtag: en") if /\.\./.match val # value specifies a range, not simply a string start, finish = val.strip.split('..', 2) val = Range.new(start, finish) else # otherwise it's just a string val.strip! end if item.has_key?(key) # append to array if this key already exists item[key] = [item[key]] unless item[key].kind_of? Array item[key] << val else # otherwise simply assign the item item[key] = val end prev = key # in case of continuation (wrapped text) end end @@tags << item nil end |
.open(filename = IANAFile) ⇒ Object
Load data from a text file
58 59 60 61 |
# File 'lib/iana/lsr.rb', line 58 def self.open(filename=IANAFile) file = File.open(filename, 'r') self.load(file.read) end |
.tags ⇒ Object
41 42 43 |
# File 'lib/iana/lsr.rb', line 41 def self. @@tags end |