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
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Locale

Returns a new instance of Locale.



52
53
54
# File 'lib/ffi-icu/locale.rb', line 52

def initialize(id)
  @id = id.to_s
end

Instance Attribute Details

#idObject (readonly) Also known as: to_s

Returns the value of attribute id.



45
46
47
# File 'lib/ffi-icu/locale.rb', line 45

def id
  @id
end

Class Method Details

.availableObject



6
7
8
9
10
# File 'lib/ffi-icu/locale.rb', line 6

def available
  (0...Lib.uloc_countAvailable).map do |idx|
    Locale.new(Lib.uloc_getAvailable(idx))
  end
end

.defaultObject



12
13
14
# File 'lib/ffi-icu/locale.rb', line 12

def default
  Locale.new(Lib.uloc_getDefault)
end

.default=(locale) ⇒ Object



16
17
18
# File 'lib/ffi-icu/locale.rb', line 16

def default=(locale)
  Lib.check_error { |status| Lib.uloc_setDefault(locale.to_s, status) }
end

.for_language_tag(tag) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/ffi-icu/locale.rb', line 20

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



28
29
30
31
32
33
34
# File 'lib/ffi-icu/locale.rb', line 28

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



36
37
38
# File 'lib/ffi-icu/locale.rb', line 36

def iso_countries
  Lib::Util.read_null_terminated_array_of_strings(Lib.uloc_getISOCountries)
end

.iso_languagesObject



40
41
42
# File 'lib/ffi-icu/locale.rb', line 40

def iso_languages
  Lib::Util.read_null_terminated_array_of_strings(Lib.uloc_getISOLanguages)
end

Instance Method Details

#==(other) ⇒ Object



56
57
58
# File 'lib/ffi-icu/locale.rb', line 56

def ==(other)
  other.is_a?(self.class) && other.id == id
end

#base_nameObject



60
61
62
63
64
# File 'lib/ffi-icu/locale.rb', line 60

def base_name
  Lib::Util.read_string_buffer(64) do |buffer, status|
    Lib.uloc_getBaseName(@id, buffer, buffer.size, status)
  end
end

#canonicalObject



66
67
68
69
70
# File 'lib/ffi-icu/locale.rb', line 66

def canonical
  Lib::Util.read_string_buffer(64) do |buffer, status|
    Lib.uloc_canonicalize(@id, buffer, buffer.size, status)
  end
end

#character_orientationObject



72
73
74
# File 'lib/ffi-icu/locale.rb', line 72

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

#countryObject



76
77
78
79
80
# File 'lib/ffi-icu/locale.rb', line 76

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



82
83
84
85
86
87
88
# File 'lib/ffi-icu/locale.rb', line 82

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



90
91
92
93
94
95
96
# File 'lib/ffi-icu/locale.rb', line 90

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



98
99
100
101
102
103
104
# File 'lib/ffi-icu/locale.rb', line 98

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



106
107
108
109
110
111
112
113
114
# File 'lib/ffi-icu/locale.rb', line 106

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



116
117
118
119
120
121
122
# File 'lib/ffi-icu/locale.rb', line 116

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



124
125
126
127
128
129
130
# File 'lib/ffi-icu/locale.rb', line 124

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



132
133
134
# File 'lib/ffi-icu/locale.rb', line 132

def iso_country
  Lib.uloc_getISO3Country(@id)
end

#iso_languageObject



136
137
138
# File 'lib/ffi-icu/locale.rb', line 136

def iso_language
  Lib.uloc_getISO3Language(@id)
end

#keyword(keyword) ⇒ Object



140
141
142
143
144
# File 'lib/ffi-icu/locale.rb', line 140

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



146
147
148
149
150
151
152
153
154
# File 'lib/ffi-icu/locale.rb', line 146

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



156
157
158
159
160
# File 'lib/ffi-icu/locale.rb', line 156

def language
  Lib::Util.read_string_buffer(64) do |buffer, status|
    Lib.uloc_getLanguage(@id, buffer, buffer.size, status)
  end
end

#lcidObject



162
163
164
# File 'lib/ffi-icu/locale.rb', line 162

def lcid
  Lib.uloc_getLCID(@id)
end

#line_orientationObject



166
167
168
# File 'lib/ffi-icu/locale.rb', line 166

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

#nameObject



170
171
172
173
174
# File 'lib/ffi-icu/locale.rb', line 170

def name
  Lib::Util.read_string_buffer(64) do |buffer, status|
    Lib.uloc_getName(@id, buffer, buffer.size, status)
  end
end

#parentObject



176
177
178
179
180
# File 'lib/ffi-icu/locale.rb', line 176

def parent
  Lib::Util.read_string_buffer(64) do |buffer, status|
    Lib.uloc_getParent(@id, buffer, buffer.size, status)
  end
end

#scriptObject



182
183
184
185
186
# File 'lib/ffi-icu/locale.rb', line 182

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

rubocop:disable Style/OptionalBooleanParameter



188
189
190
191
192
# File 'lib/ffi-icu/locale.rb', line 188

def to_language_tag(strict = false) # rubocop:disable Style/OptionalBooleanParameter
  Lib::Util.read_string_buffer(64) do |buffer, status|
    Lib.uloc_toLanguageTag(@id, buffer, buffer.size, strict ? 1 : 0, status)
  end
end

#variantObject



196
197
198
199
200
# File 'lib/ffi-icu/locale.rb', line 196

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



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/ffi-icu/locale.rb', line 202

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



219
220
221
222
223
# File 'lib/ffi-icu/locale.rb', line 219

def with_keywords(hash)
  hash.reduce(self) do |locale, (keyword, value)|
    locale.with_keyword(keyword, value)
  end
end

#with_likely_subtagsObject



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

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



241
242
243
244
245
246
247
248
249
250
# File 'lib/ffi-icu/locale.rb', line 241

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 do |status|
    ICU::Lib.uldn_openForContext(locale, pointer, contexts.length, status)
  end

  yield(locale_display_names)
ensure
  Lib.uldn_close(locale_display_names) if locale_display_names
end

#with_minimized_subtagsObject



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

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