Class: ICU::Locale
- Inherits:
-
Object
- Object
- ICU::Locale
- Defined in:
- lib/ffi-icu/locale.rb
Constant Summary collapse
- DISPLAY_CONTEXT =
{ length_full: 512, # UDISPCTX_LENGTH_FULL = (UDISPCTX_TYPE_DISPLAY_LENGTH<<8) + 0 length_short: 513 # UDISPCTX_LENGTH_SHORT = (UDISPCTX_TYPE_DISPLAY_LENGTH<<8) + 1 }
Instance Attribute Summary collapse
-
#id ⇒ Object
(also: #to_s)
readonly
Returns the value of attribute id.
Class Method Summary collapse
- .available ⇒ Object
- .default ⇒ Object
- .default=(locale) ⇒ Object
- .for_language_tag(tag) ⇒ Object
- .for_lcid(id) ⇒ Object
- .iso_countries ⇒ Object
- .iso_languages ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #base_name ⇒ Object
- #canonical ⇒ Object
- #character_orientation ⇒ Object
- #country ⇒ Object
- #display_country(locale = nil) ⇒ Object
- #display_language(locale = nil) ⇒ Object
- #display_name(locale = nil) ⇒ Object
- #display_name_with_context(locale, contexts = []) ⇒ Object
- #display_script(locale = nil) ⇒ Object
- #display_variant(locale = nil) ⇒ Object
-
#initialize(id) ⇒ Locale
constructor
A new instance of Locale.
- #iso_country ⇒ Object
- #iso_language ⇒ Object
- #keyword(keyword) ⇒ Object
- #keywords ⇒ Object
- #language ⇒ Object
- #lcid ⇒ Object
- #line_orientation ⇒ Object
- #name ⇒ Object
- #parent ⇒ Object
- #script ⇒ Object
- #to_language_tag(strict = false) ⇒ Object
- #variant ⇒ Object
- #with_keyword(keyword, value) ⇒ Object
- #with_keywords(hash) ⇒ Object
- #with_likely_subtags ⇒ Object
- #with_locale_display_name(locale, contexts) ⇒ Object
- #with_minimized_subtags ⇒ Object
Constructor Details
#initialize(id) ⇒ Locale
Returns a new instance of Locale.
50 51 52 |
# File 'lib/ffi-icu/locale.rb', line 50 def initialize(id) @id = id.to_s end |
Instance Attribute Details
#id ⇒ Object (readonly) Also known as: to_s
Returns the value of attribute id.
43 44 45 |
# File 'lib/ffi-icu/locale.rb', line 43 def id @id end |
Class Method Details
.available ⇒ Object
4 5 6 7 8 |
# File 'lib/ffi-icu/locale.rb', line 4 def available (0...Lib.uloc_countAvailable).map do |idx| Locale.new(Lib.uloc_getAvailable(idx)) end end |
.default ⇒ Object
10 11 12 |
# File 'lib/ffi-icu/locale.rb', line 10 def default Locale.new(Lib.uloc_getDefault) end |
.default=(locale) ⇒ Object
14 15 16 |
# File 'lib/ffi-icu/locale.rb', line 14 def default=(locale) Lib.check_error { |status| Lib.uloc_setDefault(locale.to_s, status) } end |
.for_language_tag(tag) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/ffi-icu/locale.rb', line 18 def for_language_tag(tag) result = Lib::Util.read_string_buffer(64) do |buffer, status| Lib.uloc_forLanguageTag(tag, buffer, buffer.size, nil, status) end Locale.new(result) end |
.for_lcid(id) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/ffi-icu/locale.rb', line 26 def for_lcid(id) result = Lib::Util.read_string_buffer(64) do |buffer, status| Lib.uloc_getLocaleForLCID(id, buffer, buffer.size, status) end Locale.new(result) end |
.iso_countries ⇒ Object
34 35 36 |
# File 'lib/ffi-icu/locale.rb', line 34 def iso_countries Lib::Util.read_null_terminated_array_of_strings(Lib.uloc_getISOCountries) end |
.iso_languages ⇒ Object
38 39 40 |
# File 'lib/ffi-icu/locale.rb', line 38 def iso_languages Lib::Util.read_null_terminated_array_of_strings(Lib.uloc_getISOLanguages) end |
Instance Method Details
#==(other) ⇒ Object
54 55 56 |
# File 'lib/ffi-icu/locale.rb', line 54 def ==(other) other.is_a?(self.class) && other.id == self.id end |
#base_name ⇒ Object
58 59 60 61 62 |
# File 'lib/ffi-icu/locale.rb', line 58 def base_name Lib::Util.read_string_buffer(64) do |buffer, status| Lib.uloc_getBaseName(@id, buffer, buffer.size, status) end end |
#canonical ⇒ Object
64 65 66 67 68 |
# File 'lib/ffi-icu/locale.rb', line 64 def canonical Lib::Util.read_string_buffer(64) do |buffer, status| Lib.uloc_canonicalize(@id, buffer, buffer.size, status) end end |
#character_orientation ⇒ Object
70 71 72 |
# File 'lib/ffi-icu/locale.rb', line 70 def character_orientation Lib.check_error { |status| Lib.uloc_getCharacterOrientation(@id, status) } end |
#country ⇒ Object
74 75 76 77 78 |
# File 'lib/ffi-icu/locale.rb', line 74 def country Lib::Util.read_string_buffer(64) do |buffer, status| Lib.uloc_getCountry(@id, buffer, buffer.size, status) end end |
#display_country(locale = nil) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/ffi-icu/locale.rb', line 80 def display_country(locale = nil) locale = locale.to_s unless locale.nil? Lib::Util.read_uchar_buffer(256) do |buffer, status| Lib.uloc_getDisplayCountry(@id, locale, buffer, buffer.size, status) end end |
#display_language(locale = nil) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/ffi-icu/locale.rb', line 88 def display_language(locale = nil) locale = locale.to_s unless locale.nil? Lib::Util.read_uchar_buffer(192) do |buffer, status| Lib.uloc_getDisplayLanguage(@id, locale, buffer, buffer.size, status) end end |
#display_name(locale = nil) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/ffi-icu/locale.rb', line 96 def display_name(locale = nil) locale = locale.to_s unless locale.nil? Lib::Util.read_uchar_buffer(256) do |buffer, status| Lib.uloc_getDisplayName(@id, locale, buffer, buffer.size, status) end end |
#display_name_with_context(locale, contexts = []) ⇒ Object
104 105 106 107 108 109 110 111 112 |
# File 'lib/ffi-icu/locale.rb', line 104 def display_name_with_context(locale, contexts = []) contexts = DISPLAY_CONTEXT.select { |context| contexts.include?(context) }.values with_locale_display_name(locale, contexts) do |locale_display_names| Lib::Util.read_uchar_buffer(256) do |buffer, status| Lib.uldn_localeDisplayName(locale_display_names, @id, buffer, buffer.size, status) end end end |
#display_script(locale = nil) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/ffi-icu/locale.rb', line 114 def display_script(locale = nil) locale = locale.to_s unless locale.nil? Lib::Util.read_uchar_buffer(128) do |buffer, status| Lib.uloc_getDisplayScript(@id, locale, buffer, buffer.size, status) end end |
#display_variant(locale = nil) ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/ffi-icu/locale.rb', line 122 def display_variant(locale = nil) locale = locale.to_s unless locale.nil? Lib::Util.read_uchar_buffer(64) do |buffer, status| Lib.uloc_getDisplayVariant(@id, locale, buffer, buffer.size, status) end end |
#iso_country ⇒ Object
130 131 132 |
# File 'lib/ffi-icu/locale.rb', line 130 def iso_country Lib.uloc_getISO3Country(@id) end |
#iso_language ⇒ Object
134 135 136 |
# File 'lib/ffi-icu/locale.rb', line 134 def iso_language Lib.uloc_getISO3Language(@id) end |
#keyword(keyword) ⇒ Object
138 139 140 141 142 |
# File 'lib/ffi-icu/locale.rb', line 138 def keyword(keyword) Lib::Util.read_string_buffer(64) do |buffer, status| Lib.uloc_getKeywordValue(@id, keyword.to_s, buffer, buffer.size, status) end end |
#keywords ⇒ Object
144 145 146 147 148 149 150 151 152 |
# File 'lib/ffi-icu/locale.rb', line 144 def keywords enum_ptr = Lib.check_error { |status| Lib.uloc_openKeywords(@id, status) } begin Lib.enum_ptr_to_array(enum_ptr) ensure Lib.uenum_close(enum_ptr) end end |
#language ⇒ Object
154 155 156 157 158 |
# File 'lib/ffi-icu/locale.rb', line 154 def language Lib::Util.read_string_buffer(64) do |buffer, status| Lib.uloc_getLanguage(@id, buffer, buffer.size, status) end end |
#lcid ⇒ Object
160 161 162 |
# File 'lib/ffi-icu/locale.rb', line 160 def lcid Lib.uloc_getLCID(@id) end |
#line_orientation ⇒ Object
164 165 166 |
# File 'lib/ffi-icu/locale.rb', line 164 def line_orientation Lib.check_error { |status| Lib.uloc_getLineOrientation(@id, status) } end |
#name ⇒ Object
168 169 170 171 172 |
# File 'lib/ffi-icu/locale.rb', line 168 def name Lib::Util.read_string_buffer(64) do |buffer, status| Lib.uloc_getName(@id, buffer, buffer.size, status) end end |
#parent ⇒ Object
174 175 176 177 178 |
# File 'lib/ffi-icu/locale.rb', line 174 def parent Lib::Util.read_string_buffer(64) do |buffer, status| Lib.uloc_getParent(@id, buffer, buffer.size, status) end end |
#script ⇒ Object
180 181 182 183 184 |
# File 'lib/ffi-icu/locale.rb', line 180 def script Lib::Util.read_string_buffer(64) do |buffer, status| Lib.uloc_getScript(@id, buffer, buffer.size, status) end end |
#to_language_tag(strict = false) ⇒ Object
186 187 188 189 190 |
# File 'lib/ffi-icu/locale.rb', line 186 def to_language_tag(strict = false) Lib::Util.read_string_buffer(64) do |buffer, status| Lib.uloc_toLanguageTag(@id, buffer, buffer.size, strict ? 1 : 0, status) end end |
#variant ⇒ Object
194 195 196 197 198 |
# File 'lib/ffi-icu/locale.rb', line 194 def variant Lib::Util.read_string_buffer(64) do |buffer, status| Lib.uloc_getVariant(@id, buffer, buffer.size, status) end end |
#with_keyword(keyword, value) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/ffi-icu/locale.rb', line 200 def with_keyword(keyword, value) keyword = keyword.to_s length = @id.length + keyword.length + 64 unless value.nil? value = value.to_s length += value.length end result = Lib::Util.read_string_buffer(length) do |buffer, status| buffer.write_string(@id) Lib.uloc_setKeywordValue(keyword, value, buffer, buffer.size, status) end Locale.new(result) end |
#with_keywords(hash) ⇒ Object
217 218 219 220 221 |
# File 'lib/ffi-icu/locale.rb', line 217 def with_keywords(hash) hash.reduce(self) do |locale, (keyword, value)| locale.with_keyword(keyword, value) end end |
#with_likely_subtags ⇒ Object
223 224 225 226 227 228 229 |
# File 'lib/ffi-icu/locale.rb', line 223 def result = Lib::Util.read_string_buffer(64) do |buffer, status| Lib.(@id, buffer, buffer.size, status) end Locale.new(result) end |
#with_locale_display_name(locale, contexts) ⇒ Object
239 240 241 242 243 244 245 246 |
# File 'lib/ffi-icu/locale.rb', line 239 def with_locale_display_name(locale, contexts) pointer = FFI::MemoryPointer.new(:int, contexts.length).write_array_of_int(contexts) locale_display_names = ICU::Lib.check_error { |status| ICU::Lib.uldn_openForContext(locale, pointer, contexts.length, status) } yield locale_display_names ensure Lib.uldn_close(locale_display_names) if locale_display_names end |