stock_strings.dart 5.08 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
// THE FOLLOWING FILES WERE GENERATED BY `flutter gen-l10n`.
6

7 8
import 'dart:async';

9
import 'package:flutter/foundation.dart';
10 11
import 'package:flutter/widgets.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
12
import 'package:intl/intl.dart' as intl;
13

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

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/// 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(),
/// );
/// ```
///
34 35 36 37 38 39 40
/// ## Update pubspec.yaml
///
/// Please make sure to update your pubspec.yaml to include the following
/// packages:
///
/// ```
/// dependencies:
41
///   # Internationalization support.
42 43
///   flutter_localizations:
///     sdk: flutter
44
///   intl: any # Use the pinned version from flutter_localizations
45 46 47 48
///
///   # rest of dependencies
/// ```
///
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
/// ## 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.
68
abstract class StockStrings {
69
  StockStrings(String locale) : localeName = intl.Intl.canonicalizedLocale(locale);
70

71
  final String localeName;
72 73

  static StockStrings of(BuildContext context) {
74
    return Localizations.of<StockStrings>(context, StockStrings)!;
75 76 77 78 79 80 81 82 83 84
  }

  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.
85 86 87 88
  ///
  /// 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.
89 90 91 92 93 94 95 96 97
  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>[
98
    Locale('en'),
99
    Locale('en', 'US'),
100
    Locale('es'),
101 102
  ];

103 104 105 106
  /// Title for the Stocks application
  ///
  /// In en, this message translates to:
  /// **'Stocks'**
107
  String get title;
108

109 110 111 112
  /// Label for the Market tab
  ///
  /// In en, this message translates to:
  /// **'MARKET'**
113
  String get market;
114

115 116 117 118
  /// Label for the Portfolio tab
  ///
  /// In en, this message translates to:
  /// **'PORTFOLIO'**
119
  String get portfolio;
120 121 122 123 124 125
}

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

  @override
126 127 128
  Future<StockStrings> load(Locale locale) {
    return SynchronousFuture<StockStrings>(_lookupStockStrings(locale));
  }
129 130

  @override
131
  bool isSupported(Locale locale) => <String>['en', 'es'].contains(locale.languageCode);
132 133 134 135

  @override
  bool shouldReload(_StockStringsDelegate old) => false;
}
136 137

StockStrings _lookupStockStrings(Locale locale) {
138

139 140 141 142 143 144 145 146
  // Lookup logic when language+country codes are specified.
  switch (locale.languageCode) {
    case 'en': {
      switch (locale.countryCode) {
        case 'US': return StockStringsEnUs();
      }
      break;
    }
147 148
  }

149 150 151
  // Lookup logic when only language code is specified.
  switch (locale.languageCode) {
    case 'en': return StockStringsEn();
152
    case 'es': return StockStringsEs();
153
  }
154

155 156 157 158 159 160
  throw FlutterError(
    'StockStrings.delegate failed to load unsupported locale "$locale". This is likely '
    'an issue with the localizations generation tool. Please file an issue '
    'on GitHub with a reproducible sample app and the gen-l10n configuration '
    'that was used.'
  );
161
}