stock_strings.dart 4.57 KB
Newer Older
1 2 3 4
// Copyright 2014 The Flutter 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 6
import 'dart:async';

7
import 'package:flutter/foundation.dart';
8 9
import 'package:flutter/widgets.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
10 11
// ignore: unused_import
import 'package:intl/intl.dart' as intl;
12

13 14 15
import 'stock_strings_en.dart';
import 'stock_strings_es.dart';

16
// ignore_for_file: unnecessary_brace_in_string_interps
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

/// Callers can lookup localized strings with an instance of StockStrings returned
/// by `StockStrings.of(context)`.
///
/// Applications need to include `StockStrings.delegate()` in their app's
/// localizationDelegates list, and the locales they support in the app's
/// supportedLocales list. For example:
///
/// ```
/// import 'i18n/stock_strings.dart';
///
/// return MaterialApp(
///   localizationsDelegates: StockStrings.localizationsDelegates,
///   supportedLocales: StockStrings.supportedLocales,
///   home: MyApplicationHome(),
/// );
/// ```
///
35 36 37 38 39 40 41
/// ## Update pubspec.yaml
///
/// Please make sure to update your pubspec.yaml to include the following
/// packages:
///
/// ```
/// dependencies:
42
///   # Internationalization support.
43 44 45 46 47 48 49 50
///   flutter_localizations:
///     sdk: flutter
///   intl: 0.16.0
///   intl_translation: 0.17.7
///
///   # rest of dependencies
/// ```
///
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
/// ## iOS Applications
///
/// iOS applications define key application metadata, including supported
/// locales, in an Info.plist file that is built into the application bundle.
/// To configure the locales supported by your app, you’ll need to edit this
/// file.
///
/// First, open your project’s ios/Runner.xcworkspace Xcode workspace file.
/// Then, in the Project Navigator, open the Info.plist file under the Runner
/// project’s Runner folder.
///
/// Next, select the Information Property List item, select Add Item from the
/// Editor menu, then select Localizations from the pop-up menu.
///
/// Select and expand the newly-created Localizations item then, for each
/// locale your application supports, add a new item and select the locale
/// you wish to add from the pop-up menu in the Value field. This list should
/// be consistent with the languages listed in the StockStrings.supportedLocales
/// property.
70
abstract class StockStrings {
71
  StockStrings(String locale) : assert(locale != null), localeName = intl.Intl.canonicalizedLocale(locale.toString());
72

73
  // ignore: unused_field
74
  final String localeName;
75 76 77 78 79 80 81 82 83 84 85 86 87

  static StockStrings of(BuildContext context) {
    return Localizations.of<StockStrings>(context, StockStrings);
  }

  static const LocalizationsDelegate<StockStrings> delegate = _StockStringsDelegate();

  /// A list of this localizations delegate along with the default localizations
  /// delegates.
  ///
  /// Returns a list of localizations delegates containing this delegate along with
  /// GlobalMaterialLocalizations.delegate, GlobalCupertinoLocalizations.delegate,
  /// and GlobalWidgetsLocalizations.delegate.
88 89 90 91
  ///
  /// Additional delegates can be added by appending to this list in
  /// MaterialApp. This list does not have to be used at all if a custom list
  /// of delegates is preferred or required.
92 93 94 95 96 97 98 99 100
  static const List<LocalizationsDelegate<dynamic>> localizationsDelegates = <LocalizationsDelegate<dynamic>>[
    delegate,
    GlobalMaterialLocalizations.delegate,
    GlobalCupertinoLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
  ];

  /// A list of this localizations delegate's supported locales.
  static const List<Locale> supportedLocales = <Locale>[
101 102 103
    Locale('en'),
    Locale('en, US'),
    Locale('es')
104 105
  ];

106 107
  // Title for the Stocks application
  String get title;
108

109 110
  // Label for the Market tab
  String get market;
111

112 113
  // Label for the Portfolio tab
  String get portfolio;
114 115 116 117 118 119
}

class _StockStringsDelegate extends LocalizationsDelegate<StockStrings> {
  const _StockStringsDelegate();

  @override
120 121 122
  Future<StockStrings> load(Locale locale) {
    return SynchronousFuture<StockStrings>(_lookupStockStrings(locale));
  }
123 124

  @override
125
  bool isSupported(Locale locale) => <String>['en', 'es'].contains(locale.languageCode);
126 127 128 129

  @override
  bool shouldReload(_StockStringsDelegate old) => false;
}
130 131 132 133 134 135 136 137 138 139 140 141 142 143

StockStrings _lookupStockStrings(Locale locale) {
  switch(locale.languageCode) {
    case 'en': {
      switch (locale.countryCode) {
        case 'US': return StockStringsEnUs();
      }
      return StockStringsEn();
    }
    case 'es': return StockStringsEs();
  }
  assert(false, 'StockStrings.delegate failed to load unsupported locale "$locale"');
  return null;
}