Unverified Commit e2b6a3a7 authored by xster's avatar xster Committed by GitHub

Cupertino localization step 6: add a GlobalCupertinoLocalizations base class...

Cupertino localization step 6: add a GlobalCupertinoLocalizations base class with date time formatting (#29767)
parent 9baffb97
......@@ -8,10 +8,9 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart' as intl;
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;
import 'l10n/generated_material_localizations.dart';
import 'utils/date_localizations.dart' as util;
import 'widgets_localizations.dart';
/// Implementation of localized strings for the material widgets using the
......@@ -559,57 +558,20 @@ class _MaterialLocalizationsDelegate extends LocalizationsDelegate<MaterialLocal
@override
bool isSupported(Locale locale) => kSupportedLanguages.contains(locale.languageCode);
/// Tracks if date i18n data has been loaded.
static 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.
static 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;
}
}
static final Map<Locale, Future<MaterialLocalizations>> _loadedTranslations = <Locale, Future<MaterialLocalizations>>{};
@override
Future<MaterialLocalizations> load(Locale locale) {
assert(isSupported(locale));
return _loadedTranslations.putIfAbsent(locale, () {
_loadDateIntlDataIfNotLoaded();
util.loadDateIntlDataIfNotLoaded();
final String localeName = intl.Intl.canonicalizedLocale(locale.toString());
assert(
locale.toString() == localeName,
'Flutter does not support the non-standard locale form $locale (which '
'might be $localeName',
);
intl.DateFormat fullYearFormat;
intl.DateFormat mediumDateFormat;
......@@ -645,8 +607,6 @@ class _MaterialLocalizationsDelegate extends LocalizationsDelegate<MaterialLocal
twoDigitZeroPaddedFormat = intl.NumberFormat('00');
}
assert(locale.toString() == localeName, 'comparing "$locale" to "$localeName"');
return SynchronousFuture<MaterialLocalizations>(getMaterialTranslation(
locale,
fullYearFormat,
......
// 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;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment