Merge pull request #67 from erickguan/dnelson/add_display_names

Display locale name with alternative territory name
This commit is contained in:
Damian Nelson 2023-11-13 15:00:49 -08:00 committed by GitHub
commit 8f343bb540
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 4 deletions

View File

@ -100,7 +100,7 @@ curf.format(1234.56, 'USD') #=> "$1,234.56"
```
Time Formatting/Parsing
--------------------------
-----------------------
Examples:
@ -130,9 +130,8 @@ formatter = ICU::TimeFormatting.create(:locale => 'cs_CZ', :date => :pattern, :t
formatter.format(Time.now) #=> "2015"
```
Duration Formatting
---------------
-------------------
```ruby
# What the various styles look like
@ -177,7 +176,7 @@ formatter.format({hours: 5, minutes: 7, seconds: 23, milliseconds: 400, microsec
formatter = ICU::DurationFormatting::DurationFormatter.new(locale: 'en-AU', style: :long)
formatter.format({days: 2, hours: 7.3, minutes: 40.9, seconds:0.43}) #=> "2 days, 7 hours, 40 minutes, 0.43 seconds"
# With RU locale
# With RU locale
formatter = ICU::DurationFormatting::DurationFormatter.new(locale: 'ru', style: :long)
formatter.format({hours: 1, minutes: 2, seconds: 3}) #=> "1 час 2 минуты 3 секунды"
formatter = ICU::DurationFormatting::DurationFormatter.new(locale: 'ru', style: :long)
@ -195,7 +194,20 @@ Example:
```ruby
ICU::Transliteration.transliterate('Traditional-Simplified', '沈從文') # => "沈从文"
```
Locale
------
Examples:
```ruby
locale = ICU::Locale.new('en-US')
locale.display_country('en-US') #=> "United States"
locale.display_language('es') #=> "inglés"
locale.display_name('es') #=> "inglés (Estados Unidos)"
locale.display_name_with_context('en-US', [:length_short]) #=> "English (US)"
locale.display_name_with_context('en-US', [:length_long]) #=> "English (United States)"
```
TODO:

View File

@ -500,5 +500,9 @@ module ICU
attach_function :ucal_setDefaultTimeZone, "ucal_setDefaultTimeZone#{suffix}", [:pointer, :pointer], :int32_t
attach_function :ucal_getDefaultTimeZone, "ucal_getDefaultTimeZone#{suffix}", [:pointer, :int32_t, :pointer], :int32_t
# ULocaleDisplayNames
attach_function :uldn_openForContext, "uldn_openForContext#{suffix}", [:string, :pointer, :int32_t, :pointer], :pointer
attach_function :uldn_localeDisplayName, "uldn_localeDisplayName#{suffix}", [:pointer, :string, :pointer, :int32_t, :pointer], :int32_t
attach_function :uldn_close, "uldn_close#{suffix}", [:pointer], :void
end # Lib
end # ICU

View File

@ -42,6 +42,11 @@ module ICU
attr_reader :id
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
}
def initialize(id)
@id = id.to_s
end
@ -96,6 +101,16 @@ module ICU
end
end
def display_name_with_context(locale, contexts = [])
contexts = DISPLAY_CONTEXT.select { |context| contexts.include?(context) }.values
with_locale_display_name(@id, contexts) do |locale_display_names|
Lib::Util.read_uchar_buffer(256) do |buffer, status|
Lib.uldn_localeDisplayName(locale_display_names, locale, buffer, buffer.size, status)
end
end
end
def display_script(locale = nil)
locale = locale.to_s unless locale.nil?
@ -220,5 +235,14 @@ module ICU
Locale.new(result)
end
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
end
end

View File

@ -123,6 +123,11 @@ module ICU
expect(Locale.new('zh_CH').display_name('fr')).to eq('chinois (Suisse)')
end
it 'returns the name using display context' do
expect(Locale.new('en_US').display_name_with_context('en_HK', [:length_full])).to eq('English (Hong Kong SAR China)')
expect(Locale.new('en_US').display_name_with_context('en_HK', [:length_short])).to eq('English (Hong Kong)')
end
it 'returns the script' do
expect(Locale.new('ja_Hira_JP').display_script('en')).to eq('Hiragana')
expect(Locale.new('ja_Hira_JP').display_script('ru')).to eq('хирагана')
@ -140,6 +145,7 @@ module ICU
expect(Locale.new('en_VI').display_country('ccp')).to_not be_nil
expect(Locale.new('yue_Hant').display_language('ccp')).to_not be_nil
expect(Locale.new('en_VI').display_name('ccp')).to_not be_nil
expect(Locale.new('ccp').display_name_with_context('en_VI')).to_not be_nil
expect(Locale.new('yue_Hant').display_script('ccp')).to_not be_nil
expect(Locale.new('en_US_POSIX').display_variant('sl')).to_not be_nil
end