init method

Future<void> init({
  1. LocaleType? localeType = LocaleType.asDefined,
  2. String? languageCode,
  3. List<String> languagesList = const ['en'],
  4. String? assetsDirectory = 'lang/',
  5. Map<String, String>? valuesAsMap,
})

init NyLocalization

Implementation

Future<void> init({
  LocaleType? localeType = LocaleType.asDefined,
  String? languageCode,
  List<String> languagesList = const ['en'],
  String? assetsDirectory = 'lang/',
  Map<String, String>? valuesAsMap,
}) async {
  // --- assets directory --- //
  if (assetsDirectory != null && !assetsDirectory.endsWith('/')) {
    assetsDirectory = '$assetsDirectory/';
  }
  _assetsDir = assetsDirectory;

  // --- locale type --- //
  _localeType = localeType ?? LocaleType.device;

  // --- language list --- //showToastNotification
  _langList = languagesList;

  if (languageCode != null) {
    _locale = Locale(languageCode);
  } else if (_localeType == LocaleType.device) {
    if ('${window.locale}'.contains(RegExp('[-_]'))) {
      _locale = Locale('${window.locale}'.split(RegExp('[-_]'))[0]);
    } else {
      _locale = Locale('${window.locale}');
    }
  } else {
    _locale = Locale(_langList[0]);
  }

  if (_assetsDir == null && valuesAsMap == null) {
    assert(
      _assetsDir != null || valuesAsMap != null,
      'You must define assetsDirectory or valuesAsMap',
    );
    return null;
  }

  if (_assetsDir != null) {
    _assetsDir = assetsDirectory;
    _values = await _initLanguage(_locale!.languageCode);
  } else {
    _values = valuesAsMap;
  }
}