init method

Future<void> init({
  1. LocaleType localeType = LocaleType.asDefined,
  2. required String languageCode,
  3. String assetsDirectory = 'lang/',
})

init NyLocalization

Implementation

Future<void> init(
    {LocaleType localeType = LocaleType.asDefined,
    required String languageCode,
    String assetsDirectory = 'lang/'}) async {
  if (!assetsDirectory.endsWith('/')) {
    assetsDirectory = '$assetsDirectory/';
  }
  _assetsDir = assetsDirectory;
  assert(_assetsDir != null, 'You must define assetsDirectory');
  _localeType = localeType;

  _locale = Locale(languageCode);

  if (_localeType == LocaleType.device) {
    Locale locale = PlatformDispatcher.instance.locale;
    _locale = locale;
    if (locale.toString().contains(RegExp('[-_]'))) {
      _locale = Locale(locale.toString().split(RegExp('[-_]'))[0]);
    }
  }

  /// Get the current language from the language switcher
  /// if it's available.
  Map<String, dynamic>? nyLangSwitcherLanguage =
      await NyLanguageSwitcher.currentLanguage();
  if (nyLangSwitcherLanguage != null) {
    _locale = Locale(nyLangSwitcherLanguage.entries.first.key);
  }

  if (_assetsDir == null) {
    return;
  }

  if (_assetsDir == null) {
    return;
  }
  _assetsDir = assetsDirectory;
  _values = await _initLanguage(_locale!.languageCode,
      defaultLanguageCode: languageCode);
}