Language
Every formatter in nepali_utils can write English or Nepali. The language controls month and weekday names, digits (2081 or २०८१), number words, and the phrases used for relative time and units.
The Language enum
Section titled “The Language enum”enum Language { english, nepali }| Value | languageCode |
countryCode |
localeName |
toLanguageTag() |
|---|---|---|---|---|
Language.english |
en |
US |
en_US |
en-US |
Language.nepali |
ne |
NP |
ne_NP |
ne-NP |
Where the language comes from
Section titled “Where the language comes from”Each formatter picks its language in this order:
- The
languageargument passed to it. - The language set by the innermost
NepaliUtils.withLanguagecall that is running. NepaliUtils.defaultLanguage, which isLanguage.englishunless you change it.
NepaliUtils.currentLanguage returns the result of steps 2 and 3: the language a formatter gets when you don’t pass one.
final date = NepaliDateTime(2081, 1, 15);
NepaliDateFormat.yMMMd().format(date); // Bai 15, 2081NepaliDateFormat.yMMMd(.nepali).format(date); // २०८१ बै १५The default language
Section titled “The default language”Set NepaliUtils.defaultLanguage once, usually at the start of main:
void main() { NepaliUtils.defaultLanguage = Language.nepali;
print(NepaliNumberFormat.decimalPattern().format(1234567)); // १२,३४,५६७}This is optional. If you don’t set it, the default is English.
For part of your code: withLanguage
Section titled “For part of your code: withLanguage”NepaliUtils.withLanguage runs a callback with a different language and returns what the callback returns. It works like Intl.withLocale in package:intl:
final date = NepaliDateTime(2081, 1, 15);
final text = NepaliUtils.withLanguage(.nepali, () { return NepaliDateFormat.yMMMd().format(date);});print(text); // २०८१ बै १५
// Outside the callback, the default applies again.print(NepaliDateFormat.yMMMd().format(date)); // Bai 15, 2081-
defaultLanguagedoesn’t change. -
The override also applies to async work started inside the callback, because it is stored in a
Zone:final language = await NepaliUtils.withLanguage(.nepali, () async {await Future<void>.delayed(Duration.zero);return NepaliUtils.currentLanguage;});print(language); // Language.nepali -
An explicit
languageargument still wins inside the callback:NepaliUtils.withLanguage(.nepali, () {return NepaliDateFormat.yMMMd(.english).format(date); // Bai 15, 2081});
Converting to and from locales
Section titled “Converting to and from locales”Language converts to and from locale identifiers, so you can connect it to package:intl or Flutter’s Locale.
Language.parse('ne_NP'); // Language.nepaliLanguage.tryParse('NE-np'); // Language.nepaliLanguage.tryParse('ne_Deva_NP'); // Language.nepaliLanguage.tryParse('fr'); // nullLanguage.fromLocale('fr'); // Language.english (the fallback)Language.fromLocale('fr', orElse: .nepali); // Language.nepali
Language.nepali.localeName; // ne_NPLanguage.nepali.toLanguageTag(); // ne-NPtryParselooks only at the language subtag. It ignores case and accepts_or-as the separator. It returnsnullfor languages it doesn’t support.parsethrows anArgumentErrorfor languages it doesn’t support.fromLocalenever throws. It falls back toorElse, which defaults toLanguage.english.
Flutter
Section titled “Flutter”Follow the app’s locale:
@overrideWidget build(BuildContext context) { final locale = Localizations.localeOf(context); NepaliUtils.defaultLanguage = Language.fromLocale(locale.toLanguageTag()); // ...}To pass formatters into a widget subtree without changing the global default, create them inside withLanguage, or pass the language to each one:
final language = Language.fromLocale(Localizations.localeOf(context).toLanguageTag());final dateFormat = NepaliDateFormat.yMMMMd(language);And going the other way, from a Language to a Locale:
final locale = Locale(language.languageCode, language.countryCode);