sealed_language_subtags 1.202608.8 copy "sealed_language_subtags: ^1.202608.8" to clipboard
sealed_language_subtags: ^1.202608.8 copied to clipboard

IANA Language Subtag Registry (BCP 47) as compile-time constant, tree-shakable, sealed Dart classes: languages, scripts, regions and variants.

example/sealed_language_subtags_example.dart

import 'package:sealed_language_subtags/sealed_language_subtags.dart';

void main() {
  // Total records and per-type counts.
  print('Total subtags: ${LanguageSubtag.list.length}');
  print('Languages: ${LanguageSubtag.languages.length}');
  print('Scripts: ${LanguageSubtag.scripts.length}');
  print('Generated from registry dated ${LanguageSubtag.fileDate}');

  // Case-insensitive lookups (nullable and throwing variants).
  final english = LanguageSubtag.maybeFromLanguage('en');
  print('en -> ${english?.description}'); // en -> English
  print('en suppress-script -> ${english?.suppressScript}'); // Latn

  // Exhaustive pattern matching over the sealed hierarchy.
  final LanguageSubtag subtag = LanguageSubtag.fromScript('Latn');
  final summary = switch (subtag) {
    Language(:final description) => 'Language: $description',
    ExtendedLanguage(:final description) => 'Ext. language: $description',
    Script(:final description) => 'Script: $description',
    Region(:final description) => 'Region: $description',
    Variant(:final description) => 'Variant: $description',
    Grandfathered(:final description) => 'Grandfathered: $description',
    Redundant(:final description) => 'Redundant: $description',
  };
  print(summary); // Script: Latin

  // Round-trip serialization.
  final restored = LanguageSubtag.fromJson(subtag.toJson());
  print('round-trip equal: ${restored == subtag}'); // true

  // copyWith and a verbose toString.
  final custom = LanguageSubtag.fromLanguage('en').copyWith(subtag: 'xx');
  print(custom.toString(short: false));

  // Filter, for example every macrolanguage.
  final macrolanguages = LanguageSubtag.languages
      .where((l) => l.scope == LanguageScope.macrolanguage)
      .length;
  print('Macrolanguages: $macrolanguages');
}
1
likes
160
points
59
downloads

Documentation

API reference

Publisher

verified publishertigerc10.dev

Weekly Downloads

IANA Language Subtag Registry (BCP 47) as compile-time constant, tree-shakable, sealed Dart classes: languages, scripts, regions and variants.

Homepage
Repository (GitHub)
View/report issues

Topics

#i18n #l10n #bcp47 #iso-639 #languages

License

MIT (license)

More

Packages that depend on sealed_language_subtags