class ICU::Locale

Overview

Locales

This class provides a high-level wrapper around ICU's locale functionality. It allows querying locale components, getting display names, canonicalizing locale identifiers, and accessing locale-specific data like measurement systems and paper sizes.

See also:

Defined in:

icu/locale.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.default : Locale #

Returns the default locale for the system.

ICU::Locale.default # => ICU::Locale.new("en_US") # or your system's default

[View source]
def self.from_language_tag(langtag : String) : Locale #

Creates a Locale from a BCP 47 language tag.

ICU::Locale.from_language_tag("en-US") # => ICU::Locale.new("en_US")

[View source]
def self.new(id : String) #

Creates a new Locale instance.

locale = ICU::Locale.new("en_US")
locale = ICU::Locale.new("fr_FR@collation=phonebook")

[View source]

Class Method Detail

def self.available_locales : Array(Locale) #

Returns an enumerator for all available locale IDs.

ICU::Locale.available_locales.each { |locale| puts locale.name }

[View source]
def self.display_keyword(keyword : String, display_locale : String) : String #

Returns the display name for a keyword in the specified display locale.

ICU::Locale.display_keyword("collation", "en_US") # => "Collation"

[View source]

Instance Method Detail

def add_keyword_value(keyword_name : String, keyword_value : String) : Locale #

Sets the value for a keyword in this locale, returning a new Locale instance.

ICU::Locale.new("fr_FR").set_keyword_value("collation", "phonebook") # => ICU::Locale.new("fr_FR@collation=phonebook")

[View source]
def add_likely_subtags : Locale #

Adds likely subtags to the locale ID.

ICU::Locale.new("en").add_likely_subtags # => ICU::Locale.new("en_Latn_US")

[View source]
def base_name : String #

Returns the base name (language, script, country, variant) for this locale.

ICU::Locale.new("en_US@collation=phonebook").base_name # => "en_US"

[View source]
def canonicalize : Locale #

Canonicalizes the locale ID.

ICU::Locale.new("en-us").canonicalize # => ICU::Locale.new("en_US")

[View source]
def character_orientation : LayoutType #

Returns the character orientation for this locale (Left-to-Right or Right-to-Left).

ICU::Locale.new("en_US").character_orientation # => :LayoutLtr

[View source]
def country : String #

Returns the country code for this locale.

ICU::Locale.new("en_US").country # => "US"

[View source]
def delimiter(type : DelimiterType) : String #

Returns the delimiter string for the specified type.

ICU::Locale.new("en_US").delimiter(:QuotationStart) # => "“"

[View source]
def display_country(display_locale : String) : String #

Returns the display country for this locale in the specified display locale.

ICU::Locale.new("fr_FR").display_country("en_US") # => "France"

[View source]
def display_keyword_value(keyword : String, display_locale : String) : String #

Returns the display name for a keyword value in the specified display locale.

ICU::Locale.new("fr_FR@collation=phonebook").display_keyword_value("collation", "en_US") # => "Phonebook Collation"

[View source]
def display_language(display_locale : String) : String #

Returns the display language for this locale in the specified display locale.

ICU::Locale.new("fr_FR").display_language("en_US") # => "French"

[View source]
def display_name(display_locale : String) : String #

Returns the display name for this locale in the specified display locale.

ICU::Locale.new("fr_FR").display_name("en_US") # => "French (France)"
ICU::Locale.new("en_US").display_name("fr_FR") # => "anglais (États-Unis)"

[View source]
def display_script(display_locale : String) : String #

Returns the display script for this locale in the specified display locale.

ICU::Locale.new("zh-Hans-CN").display_script("en_US") # => "Simplified Han"

[View source]
def display_variant(display_locale : String) : String #

Returns the display variant for this locale in the specified display locale.

ICU::Locale.new("en_US_POSIX").display_variant("en_US") # => "POSIX"

[View source]
def id : String #

Represents a locale identifier string.


[View source]
def keyword_value(keyword_name : String) : String #

Returns the value for a keyword in this locale. Returns an empty string if the keyword is not found.

ICU::Locale.new("fr_FR@collation=phonebook;calendar=gregorian").keyword_value("collation") # => "phonebook"
ICU::Locale.new("en_US").keyword_value("collation") # => ""

[View source]
def keywords : Enumerable(String) #

Returns an enumerator for the keywords in this locale.

ICU::Locale.new("fr_FR@collation=phonebook;calendar=gregorian").keywords.to_a # => ["collation", "calendar"]

[View source]
def language : String #

Returns the language code for this locale.

ICU::Locale.new("en_US").language # => "en"

[View source]
def line_orientation : LayoutType #

Returns the line orientation for this locale (Top-to-Bottom or Bottom-to-Top).

ICU::Locale.new("en_US").line_orientation # => :LayoutTtb

[View source]
def locale_display_pattern : String #

Returns the locale display pattern.

ICU::Locale.new("en_US").locale_display_pattern # => "{0} ({1})"

[View source]
def locale_separator : String #

Returns the locale separator string.

ICU::Locale.new("en_US").locale_separator # => ", "

[View source]
def measurement_system : MeasurementSystem #

Returns the measurement system for this locale.

ICU::Locale.new("en_US").measurement_system # => :Us

[View source]
def minimize_subtags : Locale #

Minimizes the subtags of the locale ID.

ICU::Locale.new("en_Latn_US").minimize_subtags # => ICU::Locale.new("en")

[View source]
def name : String #

Returns the full name (identifier) for this locale. Same as #id.

ICU::Locale.new("en_US").name # => "en_US"

[View source]
def right_to_left? : Bool #

Checks if the character orientation is Right-to-Left.

ICU::Locale.new("en_US").right_to_left? # => false

[View source]
def script : String #

Returns the script code for this locale.

ICU::Locale.new("zh-Hans-CN").script # => "Hans"

[View source]
def to_language_tag(strict : Bool = false) : String #

Converts the locale ID to a BCP 47 language tag.

ICU::Locale.new("en_US").to_language_tag # => "en-US"
ICU::Locale.new("en_US_POSIX").to_language_tag(strict: true) # => "en-US-posix"

[View source]
def variant : String #

Returns the variant code for this locale.

ICU::Locale.new("en_US_POSIX").variant # => "POSIX"

[View source]