ffi-icu requires Ruby 3.2 or newer and the ICU shared libraries.
On Linux, use the ICU packages supplied by a major distribution, such as Debian,
Ubuntu, or Fedora. Install ICU from your distribution’s supported repositories
before installing the gem.
Debian and Ubuntu
sudo apt update
sudo apt install libicu-dev
The libicu-dev package installs the matching ICU runtime libraries as dependencies.
See the official Debian package and Ubuntu package pages.
The gem is published on RubyGems. Load it before running the examples:
require('ffi-icu')
ICU in a custom directory
ffi-icu normally discovers the distribution’s ICU libraries automatically.
If you need to override discovery, set FFI_ICU_LIB to the directory containing
the shared libraries before loading the gem:
export FFI_ICU_LIB=/opt/icu/lib
ruby my_script.rb
The library detects ICU’s versioned function names automatically.
Features
Localized output, including punctuation and spaces, can vary with the installed
ICU and CLDR versions. Duration and list formatting require ICU 67 or newer.
Character Encoding Detection
Detect an encoding and inspect the confidence score:
validate raises ICU::MessageFormatting::InvalidPatternError for an invalid
pattern. The error exposes the pattern, locale, line, and offset. Use valid?
when you want a boolean result.
Arguments must be an array of numbered values. Supported types are String,
Integer, Float, and BigDecimal. Use strings for simple and select arguments,
and numbers for numeric arguments. Named arguments and rich-text tags are not supported.
Time Formatting and Parsing
Set the time zone explicitly for predictable output:
ffi-icu
Ruby FFI wrappers for International Components for Unicode (ICU). ICU provides Unicode text processing, character encoding detection, and locale-aware formatting. Its locale data comes from the Common Locale Data Repository (CLDR).
Installation
ffi-icu requires Ruby 3.2 or newer and the ICU shared libraries. On Linux, use the ICU packages supplied by a major distribution, such as Debian, Ubuntu, or Fedora. Install ICU from your distribution’s supported repositories before installing the gem.
Debian and Ubuntu
The
libicu-devpackage installs the matching ICU runtime libraries as dependencies. See the official Debian package and Ubuntu package pages.Fedora
See the official Fedora libicu package page.
Install the gem
The gem is published on RubyGems. Load it before running the examples:
ICU in a custom directory
ffi-icu normally discovers the distribution’s ICU libraries automatically. If you need to override discovery, set
FFI_ICU_LIBto the directory containing the shared libraries before loading the gem:The library detects ICU’s versioned function names automatically.
Features
Localized output, including punctuation and spaces, can vary with the installed ICU and CLDR versions. Duration and list formatting require ICU 67 or newer.
Character Encoding Detection
Detect an encoding and inspect the confidence score:
Reuse a detector for multiple inputs:
Locale-Aware Collation
Sort strings using a locale’s collation rules:
Reuse a collator to compare and sort strings:
Text Boundary Analysis
Find word boundaries or extract the substrings between them:
Boundary offsets use UTF-16 code units. Use
substringsto extract text without converting those offsets to Ruby string indexes.Number and Currency Formatting
Format numbers, percentages, and currency amounts:
Reuse a formatter for repeated calls. The French Canadian example uses a non-breaking space (
\u00A0) as its thousands separator:Currency Metadata
Look up ISO 4217 codes, localized names, symbols, and decimal digits:
narrow_symbol(locale)requires ICU 61 or newer.numeric_coderequires ICU 49 or newer.Message Formatting
Use numbered arguments for ICU MessageFormat patterns, including plurals and selects:
Validate a pattern before formatting:
validateraisesICU::MessageFormatting::InvalidPatternErrorfor an invalid pattern. The error exposes the pattern, locale, line, and offset. Usevalid?when you want a boolean result.Arguments must be an array of numbered values. Supported types are
String,Integer,Float, andBigDecimal. Use strings for simple and select arguments, and numbers for numeric arguments. Named arguments and rich-text tags are not supported.Time Formatting and Parsing
Set the time zone explicitly for predictable output:
Reuse a formatter for formatting and parsing:
Use a skeleton to request date fields and let ICU choose the locale’s pattern. See the Unicode date field symbol table for supported symbols:
Choose an hour cycle with
h11,h12,h23, orh24, or use:localefor the locale’s default:Duration Formatting
Duration formatting requires ICU 67 or newer. Choose
:long,:short,:narrow, or:digital:Reuse a formatter when the locale and style stay the same. Larger units remain outside the digital clock, and missing or zero fields are omitted:
Digital formatting folds milliseconds, microseconds, and nanoseconds into seconds. Supplying a smaller unit preserves its trailing zeros:
Fractional parts are truncated except on the smallest supplied unit, which must be seconds or smaller to retain a fraction:
Unit names and separators follow the requested locale:
Transliteration
Convert between scripts or reuse a transliterator for multiple inputs:
Apply a sequence of custom transliteration rules:
Unicode Normalization
Compose canonically equivalent text into NFC, or check whether it is already normalized:
Use
:decomposewith'nfc'for NFD. Use'nfkc'with:composeor:decomposefor compatibility normalization (NFKC or NFKD):A convenience method also supports one-off normalization:
List Formatting
List formatting requires ICU 67 or newer. Join strings with locale-aware separators and conjunctions:
The default style is
:and. Styles must be symbols, and all items must be strings. The formatter uses ICU’s wide list width.Locale
Look up localized names and choose display-name contexts:
Convert BCP 47 language tags to ICU locale IDs or add likely subtags:
Benchmarks
Benchmarks live in
benchmark/with a separate Gemfile.