Class: ICU::Locale

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#idObject (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

.availableObject



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

.defaultObject



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_countriesObject



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_languagesObject



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_nameObject



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

#canonicalObject



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_orientationObject



70
71
72
# File 'lib/ffi-icu/locale.rb', line 70

def character_orientation
  Lib.check_error { |status| Lib.uloc_getCharacterOrientation(@id, status) }
end

#countryObject



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_countryObject



130
131
132
# File 'lib/ffi-icu/locale.rb', line 130

def iso_country
  Lib.uloc_getISO3Country(@id)
end

#iso_languageObject



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

#keywordsObject



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

#languageObject



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

#lcidObject



160
161
162
# File 'lib/ffi-icu/locale.rb', line 160

def lcid
  Lib.uloc_getLCID(@id)
end

#line_orientationObject



164
165
166
# File 'lib/ffi-icu/locale.rb', line 164

def line_orientation
  Lib.check_error { |status| Lib.uloc_getLineOrientation(@id, status) }
end

#nameObject



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

#parentObject



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

#scriptObject



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

#variantObject



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_subtagsObject



223
224
225
226
227
228
229
# File 'lib/ffi-icu/locale.rb', line 223

def with_likely_subtags
  result = Lib::Util.read_string_buffer(64) do |buffer, status|
    Lib.uloc_addLikelySubtags(@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

#with_minimized_subtagsObject



231
232
233
234
235
236
237
# File 'lib/ffi-icu/locale.rb', line 231

def with_minimized_subtags
  result = Lib::Util.read_string_buffer(64) do |buffer, status|
    Lib.uloc_minimizeSubtags(@id, buffer, buffer.size, status)
  end

  Locale.new(result)
end