tree: 4512a42f1a2c5776321e55d3657b73cde37318ec [search]
  1. example/
  2. lib/
  3. test/
  4. analysis_options.yaml
  5. AUTHORS
  6. CHANGELOG.md
  7. dart_test.yaml
  8. LICENSE
  9. pubspec.yaml
  10. README.md
pkgs/intl4x/README.md

package:intl4x Pub package publisher

intl4x

A lightweight, modular library for internationalization (i18n) in Dart, providing locale-sensitive formatting, featuring:

  • Formatting for dates, numbers, and lists.
  • Collation.
  • Display names.
  • Plural rules.

Status - preview

This package has now moved from experimental to preview. We will not ship any breaking changes, but are still iterating on some components. Please provide feedback via our issue tracker!

Backends

This library uses different backends on different platforms:

  • ICU4X: Wraps ICU4X to provide i18n functionality on both native platforms.
  • ECMA / Browser: Wraps the browser's built-in Intl functionalities for web-only applications, reducing bundle size.

Installation

Add intl4x to your pubspec.yaml:

dart pub add intl4x

Limitations

There are still some features missing:

  • Compact, percent, unit, and currency formatting.
  • Display names for calendar, currency, date time fields, and scripts.

Features

Date & Time Formatting

Format DateTime objects into locale-sensitive strings for dates, times, and time zones.

import 'package:intl4x/datetime_format.dart';

void main() {
  final dateTime = DateTime(2024, 7, 1, 8, 50, 07);
  final germanLocale = Locale.parse('de-DE');

  final formatter = DateTimeFormat.yearMonthDayTime(
    locale: germanLocale,
    length: DateTimeLength.long,
  ).withTimeZoneLong();

  // Format with a specific timezone
  print(formatter.format(dateTime, 'Europe/Berlin')); 
  // prints "1. Juli 2024 um 08:50:07 Mitteleuropäische Sommerzeit"
}

Number Formatting

Format numbers as decimals, percentages, or in compact form with locale-specific rules.

import 'package:intl4x/number_format.dart';

void main() {
  final locale = Locale.parse('en-US');

  final compactFormat = NumberFormat.compact(locale: locale);
  print(compactFormat.format(12345)); // "12K"

  final percentFormat = NumberFormat.percent(locale: locale);
  print(percentFormat.format(0.987)); // "99%"
}

Collation (String Sorting)

Sort lists of strings with locale-aware collation.

import 'package:intl4x/collation.dart';

void main() {
  final list = ['Z', 'a', 'z', 'ä'];
  final german = Collation(locale: Locale.parse('de-DE'));

  list.sort(german.compare);

  print(list); // prints "[a, ä, z, Z]"
}

Display Names

Get localized display names for locales, regions, scripts, and currencies.

import 'package:intl4x/display_names.dart';

void main() {
  final displayNames = DisplayNames(locale: Locale.parse('en'));

  print(displayNames.ofLocale(Locale.parse('fr-CA'))); // "Canadian French"
  print(displayNames.ofRegion('419')); // "Latin America"
}

List Formatting

Join lists of items into a grammatically correct, locale-sensitive string.

import 'package:intl4x/list_format.dart';

void main() {
  final list = ['apples', 'bananas', 'oranges'];
  final english = Locale.parse('en');
  
  print(list.joinAnd(locale: english)); // "apples, bananas, and oranges"
  print(list.joinOr(locale: english));  // "apples, bananas, or oranges"
}

Plural Rules

Select the correct plural form for a given number based on locale rules.

import 'package:intl4x/plural_rules.dart';

void main() {
  final rules = PluralRules(
    locale: Locale.parse('en-US'),
    type: PluralType.ordinal,
  );

  String numberPlural(int i) => rules.select(i, one: 'st', two: 'nd', few: 'rd', other: 'th');

  print(numberPlural(1)); // 'st'
  print(numberPlural(2)); // 'nd'
  print(numberPlural(3)); // 'rd'
  print(numberPlural(4)); // 'th'
}

Case Mapping

Change the case of strings according to locale-specific rules.

import 'package:intl4x/case_mapping.dart';

void main() {
  final tr = Locale.parse('tr');
  final en = Locale.parse('en');

  final upper = 'TICKET';
  print(upper.toLocaleLowerCase(en)); // ticket
  print(upper.toLocaleLowerCase(tr)); // tıcket
  
  final lower = 'i';
  print(lower.toLocaleUpperCase(en)); // I
  print(lower.toLocaleUpperCase(tr)); // İ
}