sealed_languages 3.0.1 copy "sealed_languages: ^3.0.1" to clipboard
sealed_languages: ^3.0.1 copied to clipboard

Provides data for world languages in the form of sealed classes.

CodeFactor Codecov CodeRabbit Dart Code Metrics CI checks Pub points Last commit GitHub stars License: MIT Pub package FOSSA Status

This ISO-driven, pure Dart, fully tested and dependency-free package provides information about world languages in the form of compile-time, tree-shakable constant classes with a sealed origin. Contains the all 184 languages with ISO 639-1 codes, also provides ISO 639-2 codes, their English, native names, language family info, language name translations, etc. For Flutter ready widgets (like language picker) please use world_countries package.

Features #

NaturalLanguage class provides the following information about languages:

Field Required Description Example for LangEng
name Yes A non-empty string representing the English name of the natural language. "English"
code Yes A three-letter string representing the ISO 639-2/T Terminological code for the language. "ENG"
codeShort Yes A two-letter string representing the ISO 639-1 code for the language. "EN"
namesNative Yes A list of non-empty strings representing the language's native names. ["English"]
bibliographicCode No A three-letter string representing the ISO 639-2/B Bibliographic code for the language. null
family No The language family to which the language belongs. LanguageFamily(name: "Indo-European")
isRightToLeft No A boolean value specifying whether the language is written right-to-left. false
translations Yes A list of TranslatedNames representing the language name translations. 140+ translations for an English language name
Script class (click to expand)

Script class provides the following information about writing systems:

Field Required Description Example for ScriptLatn
name Yes A non-empty string representing the English name of the script. "Latin"
code Yes A four-letter string representing the ISO 15924 code for the script. "Latn"
codeNumeric Yes A three-digit string representing the ISO 15924 numeric code. "215"
date Yes A string representing the date of addition of the script. "2004-05-01"
pva No A string representing the property value alias for the script. "Latin"

Provides a compile-time constant of all languages accessible via NaturalLanguage.list moreover, the NaturalLanguage class provides the following methods/constructors:

  • maybeFromAnyCode - returns a language instance if the value matches any ISO code, otherwise returns null.
  • maybeFromCode - returns a language instance if the value matches the provided ISO 639-2 code, otherwise returns null.
  • maybeFromCodeShort - returns a language instance if the value matches the provided ISO 639-1 code code, otherwise returns null.
  • fromCode - returns a language instance if the value matches the provided ISO 639-2 code.
  • fromCodeShort - returns a language instance if the value matches the provided ISO 639-1 code code.
  • fromAnyCode - returns a language instance if the value matches the provided ISO 639-1 or ISO 639-2 codes.
  • maybeFromValue - returns a language instance if the value matches the provided value, otherwise returns null.
  • fromName - returns a language instance if the value matches the provided name.

You can also find many common methods you may know from Dart ecosystem - toString overrides, copyWith, toJson, compareTo, etc. Also, a compile-time const, tree-shakable, code maps (for O(1)-time code look-ups), list, and much more.

Tip

Translations: Use maybeCommonNameFor() or commonNameFor() methods to get translations for specific locale.

Getting started #

To use this package, add sealed_languages as a dependency in your pubspec.yaml file.

dependencies:
  sealed_languages: any

Then import the package in your Dart code:

import 'package:sealed_languages/sealed_languages.dart';

Usage #

To get information about languages, use the NaturalLanguage class. Use the class's factory constructors or static methods, or select a language from the NaturalLanguage.list constant.

  const eng = "Eng";
  final fromCode = NaturalLanguage.fromCode(eng);
  /// Equivalent of NaturalLanguage.map[eng];
  print("${fromCode.name}: ${fromCode.codeShort}"); // Prints: "English: EN".

  final script = Script.fromCodeNumeric(215);
  /// Equivalent of Script.codeNumericMap["215"];
  print("${script.name}: ${script.code}"); // Prints: "Latin: Latn".

  final maybeCzech = NaturalLanguage.maybeFromValue(
    "CZE",
    where: (language) => language.bibliographicCode,
  );

  // This will print: "Native name: čeština".
  print("Native name: ${maybeCzech?.namesNative.first}");

  final indoEuropeanLanguages = NaturalLanguage.list.where(
    (language) => language.family is IndoEuropean,
  );
  // Prints a list of Indo-European languages:
  print(indoEuropeanLanguages);
  // (Language(name: Avestan), Language(name: Afrikaans),
  // ...
  // Language(name: Walloon), Language(name: Yiddish).

  // Collects Slovak translations of all available languages.
  final slovakNames = NaturalLanguage.list.commonNamesMap(
    options: const LocaleMappingOptions(mainLocale: BasicLocale(LangSlk())),
  );

  print(
    """Fully translated to Slovak: ${slovakNames.length == NaturalLanguage.list.length}""",
  ); // Prints: "Fully translated to Slovak: true".
  for (final slkTranslation in slovakNames.entries) {
    print("Slovak name of ${slkTranslation.key.name}: ${slkTranslation.value}");
  }

Tip

Also supports Dart 3.10 dot shorthands:

print(<NaturalLanguage>[.zho(), .eng(), .spa(), .deu(), .fra()].length); // 5.

For more usage examples, please see the /example folder.

FAQ #

  • Sealed classes: Unlike enums, you can create your own ISO instances, yet unlike open classes, the sealed hierarchy guarantees exhaustive pattern matching and compile-time safety. You get the immutability and type-safety of enums with the extensibility to define custom values — all while maintaining full switch exhaustiveness checking.
  • No 3rd-party dependencies: This package has no third-party dependencies, ensuring that you won't have any issues or conflicts with other dependencies (no even meta here, because of that).
  • Rich data: This package offers far more data than any other package + tons of translations (all GlobalMaterialLocalizations and GlobalCupertinoLocalizations locales and more).
  • Type-safe: The contracts and types in this package are exceptionally strong, ensuring that your code is strongly typed and well-defined.
  • High code coverage: The code in this package has 100% code coverage, with more than 1.5K tests, providing confidence in its reliability and stability.
  • Comprehensive documentation: This package provides full documentation for every non-code generated public member, usually with examples, ensuring clarity and ease of use.
  • Lightweight: This package keeps under 500 KB, ensuring it fits within the pub cache limit. This leads to quick, low-bandwidth downloads and faster caching, minimizing resource impact.
  • Mirrored Repository: The GitHub repository, including all package tags, is mirrored on GitLab, providing an alternative access point should GitHub become unavailable.
  • Industry adopted: This package is actively used in production by numerous European companies, ensuring its efficacy and robustness in real-world scenarios.
  • MIT license: This package and sources are released under the MIT license, which is a permissive license that allows users to use, modify, and distribute the code with minimal restrictions. The MIT license is considered better than most other open-source licenses because it provides flexibility and allows users to incorporate the code into their projects without worrying about legal implications.

Do you have LLM-agents instructions?

Yes, for sure! You can find them under this spoiler:

LLM-agent instructions

LLM-agent instructions for sealed_languages #

Optimized for high-level models from Google, Anthropic, and OpenAI for use via GitHub Copilot, Claude Code or Cursor.

Context

sealed_languages provides ISO 639-1, 639-2, and 15924-compliant data for languages and scripts in a type-safe, sealed class hierarchy.

Installation

Add to pubspec.yaml:

dependencies:
  sealed_languages: any

Core Data Structures

  • NaturalLanguage: Main class for languages. Access via NaturalLanguage.list or factory constructors like .fromCode("ENG"), .fromCodeShort("EN"), .fromAnyCode("cs").
  • Script: Access via Script.list or .fromCode("Latn"), .fromCodeNumeric("215").
  • LanguageFamily: Accessible via language.family.

Querying & Filtering

  • Find by code: NaturalLanguage.fromAnyCode("EN") or NaturalLanguage.maybeFromAnyCode(code).
  • Filter by properties: NaturalLanguage.list.where((l) => l.isRightToLeft).
  • Functional lookups: NaturalLanguage.maybeFromValue("Czech", where: (l) => l.namesNative.first).

Integration Patterns

  • Dot shorthands (Dart 3.10+): Use .eng(), .deu(), .fra() for concise initialization.
  • Switch Exhaustiveness: Use switch(language) { ... } to ensure all language cases are handled.
  • Localization: Translations are built-in. Use language.commonNameFor(BasicLocale(LangEng())) for localized names. For advanced L10N features, consider the l10n_languages package.

Troubleshooting

  • Use maybeFromAnyCode to safely handle external input.
  • For country-specific language info, refer to sealed_countries documentation.

Additional information #

If you like this package, please give it a star or like. For more information on using this package, check out the API documentation. PRs or ideas are always welcome. If you have any issues or suggestions for the package, please file them in the GitHub repository.

References, credits and sources #

1 Translated JSON data to Dart language (following Effective Dart: Style guidelines), modified, added additional data and functionality.

13
likes
160
points
9.16k
downloads
screenshot

Publisher

verified publishertsin.is

Weekly Downloads

Provides data for world languages in the form of sealed classes.

Repository (GitHub)
View/report issues

Topics

#languages #language #script #locale #iso-639

Documentation

API reference

License

MIT (license)

Dependencies

l10n_languages

More

Packages that depend on sealed_languages