Class: ICU::Locale

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi-icu/locale.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Locale

Returns a new instance of Locale.



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

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



49
50
51
# File 'lib/ffi-icu/locale.rb', line 49

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

#base_nameObject



53
54
55
56
57
# File 'lib/ffi-icu/locale.rb', line 53

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

#canonicalObject



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

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

#character_orientationObject



65
66
67
# File 'lib/ffi-icu/locale.rb', line 65

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

#countryObject



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

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



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

def display_country(locale = nil)
  locale = locale.to_s unless locale.nil?

  Lib::Util.read_uchar_buffer(64) do |buffer, status|
    Lib.uloc_getDisplayCountry(@id, locale, buffer, buffer.size, status)
  end
end

#display_language(locale = nil) ⇒ Object



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

def display_language(locale = nil)
  locale = locale.to_s unless locale.nil?

  Lib::Util.read_uchar_buffer(64) do |buffer, status|
    Lib.uloc_getDisplayLanguage(@id, locale, buffer, buffer.size, status)
  end
end

#display_name(locale = nil) ⇒ Object



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

def display_name(locale = nil)
  locale = locale.to_s unless locale.nil?

  Lib::Util.read_uchar_buffer(64) do |buffer, status|
    Lib.uloc_getDisplayName(@id, locale, buffer, buffer.size, status)
  end
end

#display_script(locale = nil) ⇒ Object



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

def display_script(locale = nil)
  locale = locale.to_s unless locale.nil?

  Lib::Util.read_uchar_buffer(64) do |buffer, status|
    Lib.uloc_getDisplayScript(@id, locale, buffer, buffer.size, status)
  end
end

#display_variant(locale = nil) ⇒ Object



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

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



115
116
117
# File 'lib/ffi-icu/locale.rb', line 115

def iso_country
  Lib.uloc_getISO3Country(@id)
end

#iso_languageObject



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

def iso_language
  Lib.uloc_getISO3Language(@id)
end

#keyword(keyword) ⇒ Object



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

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



129
130
131
132
133
134
135
136
137
# File 'lib/ffi-icu/locale.rb', line 129

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



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

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

#lcidObject



145
146
147
# File 'lib/ffi-icu/locale.rb', line 145

def lcid
  Lib.uloc_getLCID(@id)
end

#line_orientationObject



149
150
151
# File 'lib/ffi-icu/locale.rb', line 149

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

#nameObject



153
154
155
156
157
# File 'lib/ffi-icu/locale.rb', line 153

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

#parentObject



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

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

#scriptObject



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

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



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

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



179
180
181
182
183
# File 'lib/ffi-icu/locale.rb', line 179

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



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/ffi-icu/locale.rb', line 185

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



202
203
204
205
206
# File 'lib/ffi-icu/locale.rb', line 202

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

#with_likely_subtagsObject



208
209
210
211
212
213
214
# File 'lib/ffi-icu/locale.rb', line 208

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_minimized_subtagsObject



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

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