• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2019 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'package:intl/date_symbols.dart' as intl;
6import 'package:intl/date_symbol_data_custom.dart' as date_symbol_data_custom;
7import '../l10n/generated_date_localizations.dart' as date_localizations;
8
9/// Tracks if date i18n data has been loaded.
10bool _dateIntlDataInitialized = false;
11
12/// Loads i18n data for dates if it hasn't be loaded yet.
13///
14/// Only the first invocation of this function has the effect of loading the
15/// data. Subsequent invocations have no effect.
16void loadDateIntlDataIfNotLoaded() {
17  if (!_dateIntlDataInitialized) {
18    // TODO(garyq): Add support for scriptCodes. Do not strip scriptCode from string.
19
20    // Keep track of initialzed locales, or will fail on attempted double init.
21    // This can only happen if a locale with a stripped scriptCode has already
22    // been initialzed. This should be removed when scriptCode stripping is removed.
23    final Set<String> initializedLocales = <String>{};
24    date_localizations.dateSymbols.forEach((String locale, dynamic data) {
25      // Strip scriptCode from the locale, as we do not distinguish between scripts
26      // for dates.
27      final List<String> codes = locale.split('_');
28      String countryCode;
29      if (codes.length == 2) {
30        countryCode = codes[1].length < 4 ? codes[1] : null;
31      } else if (codes.length == 3) {
32        countryCode = codes[1].length < codes[2].length ? codes[1] : codes[2];
33      }
34      locale = codes[0] + (countryCode != null ? '_' + countryCode : '');
35      if (initializedLocales.contains(locale))
36        return;
37      initializedLocales.add(locale);
38      // Perform initialization.
39      assert(date_localizations.datePatterns.containsKey(locale));
40      final intl.DateSymbols symbols = intl.DateSymbols.deserializeFromMap(data);
41      date_symbol_data_custom.initializeDateFormattingCustom(
42        locale: locale,
43        symbols: symbols,
44        patterns: date_localizations.datePatterns[locale],
45      );
46    });
47    _dateIntlDataInitialized = true;
48  }
49}
50