stock_strings.dart 4.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
import 'dart:async';

import 'package:flutter/widgets.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:intl/intl.dart';

import 'messages_all.dart';

/// 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(),
/// );
/// ```
///
26 27 28 29 30 31 32
/// ## Update pubspec.yaml
///
/// Please make sure to update your pubspec.yaml to include the following
/// packages:
///
/// ```
/// dependencies:
33
///   # Internationalization support.
34 35 36 37 38 39 40 41
///   flutter_localizations:
///     sdk: flutter
///   intl: 0.16.0
///   intl_translation: 0.17.7
///
///   # rest of dependencies
/// ```
///
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
/// ## 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.
class StockStrings {
62
  StockStrings(Locale locale) : _localeName = Intl.canonicalizedLocale(locale.toString());
63 64 65 66 67

  final String _localeName;

  static Future<StockStrings> load(Locale locale) {
    return initializeMessages(locale.toString())
68
      .then<StockStrings>((_) => StockStrings(locale));
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  }

  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.
  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>[
92
    Locale('en', 'US'),
93
    Locale('es', 'ES'),
94 95
  ];

96
  String get market {
97 98 99 100
    return Intl.message(
      r'MARKET',
      locale: _localeName,
      name: 'market',
101
      desc: r'Label for the Market tab'
102 103 104
    );
  }

105
  String get portfolio {
106 107 108 109
    return Intl.message(
      r'PORTFOLIO',
      locale: _localeName,
      name: 'portfolio',
110
      desc: r'Label for the Portfolio tab'
111 112 113
    );
  }

114 115 116 117 118 119 120 121 122
  String get title {
    return Intl.message(
      r'Stocks',
      locale: _localeName,
      name: 'title',
      desc: r'Title for the Stocks application'
    );
  }

123 124 125 126 127 128 129 130 131
}

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

  @override
  Future<StockStrings> load(Locale locale) => StockStrings.load(locale);

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

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