widgets_localizations.dart 2.41 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';

8 9
import 'l10n/generated_widgets_localizations.dart';

10 11
/// Localized values for widgets.
///
12 13 14 15 16 17 18 19 20 21
/// ## Supported languages
///
/// This class supports locales with the following [Locale.languageCode]s:
///
/// {@macro flutter.localizations.widgets.languages}
///
/// This list is available programmatically via [kWidgetsSupportedLanguages].
///
/// Besides localized strings, this class also maps [locale] to [textDirection].
/// All locales are [TextDirection.ltr] except for locales with the following
22 23 24 25 26 27 28 29
/// [Locale.languageCode] values, which are [TextDirection.rtl]:
///
///   * ar - Arabic
///   * fa - Farsi
///   * he - Hebrew
///   * ps - Pashto
///   * sd - Sindhi
///   * ur - Urdu
30 31
///
abstract class GlobalWidgetsLocalizations implements WidgetsLocalizations {
32
  /// Construct an object that defines the localized values for the widgets
33 34
  /// library for the given [textDirection].
  const GlobalWidgetsLocalizations(this.textDirection);
35 36

  @override
37
  final TextDirection textDirection;
38

39
  /// A [LocalizationsDelegate] for [WidgetsLocalizations].
40
  ///
41 42 43
  /// Most internationalized apps will use [GlobalMaterialLocalizations.delegates]
  /// as the value of [MaterialApp.localizationsDelegates] to include
  /// the localizations for both the material and widget libraries.
44
  static const LocalizationsDelegate<WidgetsLocalizations> delegate = _WidgetsLocalizationsDelegate();
45 46 47 48 49
}

class _WidgetsLocalizationsDelegate extends LocalizationsDelegate<WidgetsLocalizations> {
  const _WidgetsLocalizationsDelegate();

50
  @override
51 52 53
  bool isSupported(Locale locale) => kWidgetsSupportedLanguages.contains(locale.languageCode);

  static final Map<Locale, Future<WidgetsLocalizations>> _loadedTranslations = <Locale, Future<WidgetsLocalizations>>{};
54

55
  @override
56 57 58 59 60 61 62 63
  Future<WidgetsLocalizations> load(Locale locale) {
    assert(isSupported(locale));
    return _loadedTranslations.putIfAbsent(locale, () {
      return SynchronousFuture<WidgetsLocalizations>(getWidgetsTranslation(
        locale,
      )!);
    });
  }
64 65 66

  @override
  bool shouldReload(_WidgetsLocalizationsDelegate old) => false;
67 68

  @override
69
  String toString() => 'GlobalWidgetsLocalizations.delegate(${kWidgetsSupportedLanguages.length} locales)';
70
}