lib/ffi-icu/locale.rb in ffi-icu-0.5.1 vs lib/ffi-icu/locale.rb in ffi-icu-0.5.2

- old
+ new

@@ -40,10 +40,15 @@ end end 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 def ==(other) @@ -94,10 +99,20 @@ Lib::Util.read_uchar_buffer(256) do |buffer, status| Lib.uloc_getDisplayName(@id, locale, buffer, buffer.size, status) 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? Lib::Util.read_uchar_buffer(128) do |buffer, status| Lib.uloc_getDisplayScript(@id, locale, buffer, buffer.size, status) @@ -217,8 +232,17 @@ result = Lib::Util.read_string_buffer(64) do |buffer, status| Lib.uloc_minimizeSubtags(@id, buffer, buffer.size, status) end 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