material_localizations.dart 3.16 KB
Newer Older
1 2 3 4
// Copyright 2017 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.

5
import 'dart:async';
6

7 8
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
9

10
import 'i18n/localizations.dart';
11

12 13 14 15
/// Defines the localized resource values used by the Material widgts.
///
/// See also:
///
16
///  * [DefaultMaterialLocalizations], which implements this interface
17 18
///    and supports a variety of locales.
abstract class MaterialLocalizations {
19
  /// The tooltip for the leading [AppBar] menu (aka 'hamburger') button
20
  String get openAppDrawerTooltip;
21 22

  /// The [BackButton]'s tooltip.
23
  String get backButtonTooltip;
24 25

  /// The [CloseButton]'s tooltip.
26
  String get closeButtonTooltip;
27 28

  /// The tooltip for the [MonthPicker]'s "next month" button.
29
  String get nextMonthTooltip;
30 31

  /// The tooltip for the [MonthPicker]'s "previous month" button.
32
  String get previousMonthTooltip;
33 34 35 36 37 38 39 40 41

  /// The `MaterialLocalizations` from the closest [Localizations] instance
  /// that encloses the given context.
  ///
  /// This method is just a convenient shorthand for:
  /// `Localizations.of<MaterialLocalizations>(context, MaterialLocalizations)`.
  ///
  /// References to the localized resources defined by this class are typically
  /// written in terms of this method. For example:
42
  ///
43 44 45 46 47 48 49
  /// ```dart
  /// tooltip: MaterialLocalizations.of(context).backButtonTooltip,
  /// ```
  static MaterialLocalizations of(BuildContext context) {
    return Localizations.of<MaterialLocalizations>(context, MaterialLocalizations);
  }
}
50 51 52 53 54

/// Localized strings for the material widgets.
class DefaultMaterialLocalizations implements MaterialLocalizations {
  /// Construct an object that defines the material widgets' localized strings
  /// for the given `locale`.
55 56 57
  ///
  /// [LocalizationsDelegate] implementations typically call the static [load]
  /// function, rather than constructing this class directly.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  DefaultMaterialLocalizations(this.locale) {
    assert(locale != null);
    _nameToValue = localizations[locale.toString()]
      ?? localizations[locale.languageCode]
      ?? localizations['en']
      ?? <String, String>{};
  }

  Map<String, String> _nameToValue;

  /// The locale for which the values of this class's localized resources
  /// have been translated.
  final Locale locale;

  @override
  String get openAppDrawerTooltip => _nameToValue["openAppDrawerTooltip"];

  @override
  String get backButtonTooltip => _nameToValue["backButtonTooltip"];

  @override
  String get closeButtonTooltip => _nameToValue["closeButtonTooltip"];

  @override
  String get nextMonthTooltip => _nameToValue["nextMonthTooltip"];

  @override
  String get previousMonthTooltip => _nameToValue["previousMonthTooltip"];

  /// Creates an object that provides localized resource values for the
  /// for the widgets of the material library.
  ///
  /// This method is typically used to create a [LocalizationsDelegate].
  /// The [MaterialApp] does so by default.
  static Future<MaterialLocalizations> load(Locale locale) {
    return new SynchronousFuture<MaterialLocalizations>(new DefaultMaterialLocalizations(locale));
  }
}