date_localizations.dart 2.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:intl/date_symbols.dart' as intl;
import 'package:intl/date_symbol_data_custom.dart' as date_symbol_data_custom;
import '../l10n/generated_date_localizations.dart' as date_localizations;

/// Tracks if date i18n data has been loaded.
bool _dateIntlDataInitialized = false;

/// Loads i18n data for dates if it hasn't be loaded yet.
///
/// Only the first invocation of this function has the effect of loading the
/// data. Subsequent invocations have no effect.
void loadDateIntlDataIfNotLoaded() {
  if (!_dateIntlDataInitialized) {
    // TODO(garyq): Add support for scriptCodes. Do not strip scriptCode from string.

    // Keep track of initialzed locales, or will fail on attempted double init.
    // This can only happen if a locale with a stripped scriptCode has already
    // been initialzed. This should be removed when scriptCode stripping is removed.
    final Set<String> initializedLocales = <String>{};
    date_localizations.dateSymbols.forEach((String locale, dynamic data) {
      // Strip scriptCode from the locale, as we do not distinguish between scripts
      // for dates.
      final List<String> codes = locale.split('_');
      String countryCode;
      if (codes.length == 2) {
        countryCode = codes[1].length < 4 ? codes[1] : null;
      } else if (codes.length == 3) {
        countryCode = codes[1].length < codes[2].length ? codes[1] : codes[2];
      }
      locale = codes[0] + (countryCode != null ? '_' + countryCode : '');
      if (initializedLocales.contains(locale))
        return;
      initializedLocales.add(locale);
      // Perform initialization.
      assert(date_localizations.datePatterns.containsKey(locale));
      final intl.DateSymbols symbols = intl.DateSymbols.deserializeFromMap(data);
      date_symbol_data_custom.initializeDateFormattingCustom(
        locale: locale,
        symbols: symbols,
        patterns: date_localizations.datePatterns[locale],
      );
    });
    _dateIntlDataInitialized = true;
  }
Dan Field's avatar
Dan Field committed
49
}