ffi-icu 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6deabc8837b1848e66d57dee14d70aff6a9cf5d1a8be8f1fbdaed7a1365b2325
4
- data.tar.gz: 7290816f535dcbceacf437599c5aa57adbee535f5bee695c8a31cb71be98fd2d
3
+ metadata.gz: 2075e91bd27944e5725d4379471db7bda9787d7b4a0622e92b71f15d03ed9ef3
4
+ data.tar.gz: 145eddff12e1d703ed29d01f3661e674439b39ff11c3882fc52633af50e6ca09
5
5
  SHA512:
6
- metadata.gz: 7b39630132b823d043607a0b4d68462b92fb1a8f521487003888c5c5cb1a558cc83c4d0471717daea83cfaf7b4390245cc12c278dae436819dfde6504f777f98
7
- data.tar.gz: a9538800ca6ba2a261c5ed279664176b95cd2a407854c74ef1c6b9947054f6865cdc6e86cd40bcf2e7045bf048f641c1e0a0c4e03d70e73d9651583d56ccc61b
6
+ metadata.gz: 6d63a1b1f93622d80abbc93889fa0e8eea90fa81025de1b4c5bf672a65d8922809100fa55ccc55a3c42599d2b2dfd3c2d69774601f512308783b66c26e9b6a71
7
+ data.tar.gz: 99fa27ab152f6f52a62d71a850e201ac61149c2c86b3fbb92fd2ff86f1ec942f19394af43e4068338d6a11121cdaadab9d53509da0d93750efeb1d54b960556d
data/README.md CHANGED
@@ -100,7 +100,7 @@ curf.format(1234.56, 'USD') #=> "$1,234.56"
100
100
  ```
101
101
 
102
102
  Time Formatting/Parsing
103
- --------------------------
103
+ -----------------------
104
104
 
105
105
  Examples:
106
106
 
@@ -130,9 +130,8 @@ formatter = ICU::TimeFormatting.create(:locale => 'cs_CZ', :date => :pattern, :t
130
130
  formatter.format(Time.now) #=> "2015"
131
131
  ```
132
132
 
133
-
134
133
  Duration Formatting
135
- ---------------
134
+ -------------------
136
135
 
137
136
  ```ruby
138
137
  # What the various styles look like
@@ -177,7 +176,7 @@ formatter.format({hours: 5, minutes: 7, seconds: 23, milliseconds: 400, microsec
177
176
  formatter = ICU::DurationFormatting::DurationFormatter.new(locale: 'en-AU', style: :long)
178
177
  formatter.format({days: 2, hours: 7.3, minutes: 40.9, seconds:0.43}) #=> "2 days, 7 hours, 40 minutes, 0.43 seconds"
179
178
 
180
- # With RU locale
179
+ # With RU locale
181
180
  formatter = ICU::DurationFormatting::DurationFormatter.new(locale: 'ru', style: :long)
182
181
  formatter.format({hours: 1, minutes: 2, seconds: 3}) #=> "1 час 2 минуты 3 секунды"
183
182
  formatter = ICU::DurationFormatting::DurationFormatter.new(locale: 'ru', style: :long)
@@ -195,7 +194,20 @@ Example:
195
194
 
196
195
  ```ruby
197
196
  ICU::Transliteration.transliterate('Traditional-Simplified', '沈從文') # => "沈从文"
197
+ ```
198
198
 
199
+ Locale
200
+ ------
201
+
202
+ Examples:
203
+
204
+ ```ruby
205
+ locale = ICU::Locale.new('en-US')
206
+ locale.display_country('en-US') #=> "United States"
207
+ locale.display_language('es') #=> "inglés"
208
+ locale.display_name('es') #=> "inglés (Estados Unidos)"
209
+ locale.display_name_with_context('en-US', [:length_short]) #=> "English (US)"
210
+ locale.display_name_with_context('en-US', [:length_long]) #=> "English (United States)"
199
211
  ```
200
212
 
201
213
  TODO:
data/lib/ffi-icu/lib.rb CHANGED
@@ -500,5 +500,9 @@ module ICU
500
500
  attach_function :ucal_setDefaultTimeZone, "ucal_setDefaultTimeZone#{suffix}", [:pointer, :pointer], :int32_t
501
501
  attach_function :ucal_getDefaultTimeZone, "ucal_getDefaultTimeZone#{suffix}", [:pointer, :int32_t, :pointer], :int32_t
502
502
 
503
+ # ULocaleDisplayNames
504
+ attach_function :uldn_openForContext, "uldn_openForContext#{suffix}", [:string, :pointer, :int32_t, :pointer], :pointer
505
+ attach_function :uldn_localeDisplayName, "uldn_localeDisplayName#{suffix}", [:pointer, :string, :pointer, :int32_t, :pointer], :int32_t
506
+ attach_function :uldn_close, "uldn_close#{suffix}", [:pointer], :void
503
507
  end # Lib
504
508
  end # ICU
@@ -42,6 +42,11 @@ module ICU
42
42
 
43
43
  attr_reader :id
44
44
 
45
+ DISPLAY_CONTEXT = {
46
+ length_full: 512, # UDISPCTX_LENGTH_FULL = (UDISPCTX_TYPE_DISPLAY_LENGTH<<8) + 0
47
+ length_short: 513 # UDISPCTX_LENGTH_SHORT = (UDISPCTX_TYPE_DISPLAY_LENGTH<<8) + 1
48
+ }
49
+
45
50
  def initialize(id)
46
51
  @id = id.to_s
47
52
  end
@@ -96,6 +101,16 @@ module ICU
96
101
  end
97
102
  end
98
103
 
104
+ def display_name_with_context(locale, contexts = [])
105
+ contexts = DISPLAY_CONTEXT.select { |context| contexts.include?(context) }.values
106
+
107
+ with_locale_display_name(@id, contexts) do |locale_display_names|
108
+ Lib::Util.read_uchar_buffer(256) do |buffer, status|
109
+ Lib.uldn_localeDisplayName(locale_display_names, locale, buffer, buffer.size, status)
110
+ end
111
+ end
112
+ end
113
+
99
114
  def display_script(locale = nil)
100
115
  locale = locale.to_s unless locale.nil?
101
116
 
@@ -220,5 +235,14 @@ module ICU
220
235
 
221
236
  Locale.new(result)
222
237
  end
238
+
239
+ def with_locale_display_name(locale, contexts)
240
+ pointer = FFI::MemoryPointer.new(:int, contexts.length).write_array_of_int(contexts)
241
+ locale_display_names = ICU::Lib.check_error { |status| ICU::Lib.uldn_openForContext(locale, pointer, contexts.length, status) }
242
+
243
+ yield locale_display_names
244
+ ensure
245
+ Lib.uldn_close(locale_display_names) if locale_display_names
246
+ end
223
247
  end
224
248
  end
@@ -1,3 +1,3 @@
1
1
  module ICU
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.2"
3
3
  end
data/spec/locale_spec.rb CHANGED
@@ -123,6 +123,11 @@ module ICU
123
123
  expect(Locale.new('zh_CH').display_name('fr')).to eq('chinois (Suisse)')
124
124
  end
125
125
 
126
+ it 'returns the name using display context' do
127
+ expect(Locale.new('en_US').display_name_with_context('en_HK', [:length_full])).to eq('English (Hong Kong SAR China)')
128
+ expect(Locale.new('en_US').display_name_with_context('en_HK', [:length_short])).to eq('English (Hong Kong)')
129
+ end
130
+
126
131
  it 'returns the script' do
127
132
  expect(Locale.new('ja_Hira_JP').display_script('en')).to eq('Hiragana')
128
133
  expect(Locale.new('ja_Hira_JP').display_script('ru')).to eq('хирагана')
@@ -140,6 +145,7 @@ module ICU
140
145
  expect(Locale.new('en_VI').display_country('ccp')).to_not be_nil
141
146
  expect(Locale.new('yue_Hant').display_language('ccp')).to_not be_nil
142
147
  expect(Locale.new('en_VI').display_name('ccp')).to_not be_nil
148
+ expect(Locale.new('ccp').display_name_with_context('en_VI')).to_not be_nil
143
149
  expect(Locale.new('yue_Hant').display_script('ccp')).to_not be_nil
144
150
  expect(Locale.new('en_US_POSIX').display_variant('sl')).to_not be_nil
145
151
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ffi-icu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jari Bakken
@@ -130,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
130
  - !ruby/object:Gem::Version
131
131
  version: '0'
132
132
  requirements: []
133
- rubygems_version: 3.1.6
133
+ rubygems_version: 3.4.10
134
134
  signing_key:
135
135
  specification_version: 4
136
136
  summary: Simple Ruby FFI wrappers for things I need from ICU.