common_locale_data 2.0.0 copy "common_locale_data: ^2.0.0" to clipboard
common_locale_data: ^2.0.0 copied to clipboard

A type-safe and tree-shakable API to access translated common data (measurement units, date fields, country names etc...)

common_locale_data #

This packages provides a type-safe and tree-shakable way to access translated common data.
The translations are extracted from the Common Locale Data Repository (CLDR).

Available data #

  • Translations for measurement units in full and abbreviated forms including singular/plural modifications.
  • Translations for language names.
  • Translations for territory and country names.
  • Translations for currency names, including singular/plural modifications.
  • Translations for weekday, month, era, period of day, in full and abbreviated forms.
  • Translations for time zones and example cities (or similar) for time zones.
  • Translations for calendar fields.
  • Translations for relative time fields.

Tree-shaking #

All the data and translations are stored as literal strings in the code. The APIs are designed to be tree-shakeable. The final application's binaries won't include the translations for languages you don't use.

Compilation #

To improve compilation speed locales need to be imported individually when using [common_locale_data]. For example for English: import 'package:common_locale_data/en.dart'; or for French: import 'package:common_locale_data/fr.dart';

You can import all locales via [common_locale_data_all]. However this will increase compilation time significantly (10x over including just a couple of locales). As long as you don't use the [CommonLocaleDataAll.locales] member to dynamically get locales the compiled file size is not affected. If you do use the [locales] member, all locales will be compiled in and the files size will become several 10's of MBs.

Source #

The main CLDR data is extracted from this repository: https://github.com/unicode-org/cldr-json. Additional timezone related data is extracted from https://github.com/unicode-org/cldr and https://github.com/unicode-org/icu.

  • CLDR version 46.0.0, variant: modern
  • Unicode version 16.0.0
  • TZDB version 2024b

Example #

import 'package:common_locale_data/ar_eg.dart';
import 'package:common_locale_data/en.dart';
import 'package:common_locale_data/en_gb.dart';
import 'package:common_locale_data/fr.dart';

void main() {
  // To preserve tree-shaking, you should explicitly choose the language your want
  // The compiler will only retain the languages that are explicitly referenced in your program
  // and discard all the others languages. It will make your deployed program smaller.

  // If your app support several languages, dynamically choose the language you want
  // from a map you create yourself.
  var currentLanguage = 'en-GB';

  var locales = const {
    'en-GB': CommonLocaleDataEnGB(),
    'en': CommonLocaleDataEn(),
    'fr': CommonLocaleDataFr(),
    'ar-EG': CommonLocaleDataArEG(),
  };

  var cld = locales[currentLanguage]!;

  print('CLDR version: ${CommonLocaleData.cldrVersion}');
  print('Unicode version: ${CommonLocaleData.unicodeVersion}');
  print('ICU version: ${CommonLocaleData.icuVersion}');
  print('Timezone DB version: ${CommonLocaleData.tzdbVersion}');

  print('');
  print('Current language: ${cld.locale} (${cld.languages[cld.locale]!.name})');
  print('');

  // Units
  print(cld.units.lengthMeter); // meters
  print(cld.units.lengthMeter.long(3)); // 3 meters
  print(cld.units.lengthMeter.long(1)); // 1 meter

  print(cld.units.areaSquareMeter.long(3)); // 3 square meters
  print(cld.units.areaSquareMeter.short(3)); // 3 m²
  print(cld.units.areaSquareMeter.narrow(3)); // 3m²

  // Date fields
  print('');
  print(cld.date.year.future.long(2)); // in 2 years
  print(cld.date.year.past.long(2)); // 2 years ago
  print(cld.date.year.next.long); // next year
  print(cld.date.year.previous.long); // last year

  // Territories
  print('');
  print(cld.territories.africa); // Africa
  print(cld.territories.countries['US']); // United States

  // Languages
  print('');
  print(cld.languages['en']!.name); // English

  // Timezones
  print('');
  var zone =
      cld.timeZones.get('America/Los_Angeles', dateTime: DateTime(2017))!;

  print('code: ${zone.code}');
  print('canonicalCode: ${zone.canonicalCode}');
  print('ianaCode: ${zone.iana}');
  print('shortCode: ${zone.short}');
  print('exemplarCity: ${zone.exemplarCity}');
  print('country: ${zone.country}');
  print('isPrimaryOrSingle: ${zone.isPrimaryOrSingle}');
  print('dateRange: ${zone.dateRange}');

  // Different formats for timezone
  print('');
  for (var style in TimeZoneStyle.values) {
    print(
        '${style.name} in own: ${zone.format(style, Duration(hours: -7, minutes: 0))}');

    // also depend on the current country
    print(
        '${style.name} in CA: ${zone.format(style, Duration(hours: -7, minutes: 0), country: 'CA')}');
  }

  // Demonstrate different timezone names at different times
  print('');
  print(cld.timeZones['America/Buenos_Aires']);
  print(cld.timeZones.get('America/Buenos_Aires', dateTime: DateTime(2008)));
  print(cld.timeZones
      .get('America/Buenos_Aires', dateTime: DateTime(2004, 6, 2)));
}

Supported languages/locales #

Locale Description Constant Class Import

To change the included locales modify the tool\config.dart file and rerun the generate_code.dart and generate_readme.dart files.

2
likes
0
points
4.2k
downloads

Publisher

unverified uploader

Weekly Downloads

A type-safe and tree-shakable API to access translated common data (measurement units, date fields, country names etc...)

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

collection, intl

More

Packages that depend on common_locale_data