gen_l10n_templates.dart 7.95 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
const String emptyPubspecTemplate = '''
# Generated by the flutter tool
7 8 9 10
name: synthetic_package
description: The Flutter application's synthetic package.
''';

11
const String fileTemplate = '''
12
@(header)
13 14
import 'dart:async';

15
@(requiresFoundationImport)
16 17
import 'package:flutter/widgets.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
18
import 'package:intl/intl.dart' as intl;
19

20 21
@(messageClassImports)

22 23
/// Callers can lookup localized strings with an instance of @(class) returned
/// by `@(class).of(context)`.
24
///
25 26
/// Applications need to include `@(class).delegate()` in their app's
/// localizationDelegates list, and the locales they support in the app's
27 28 29 30 31 32
/// supportedLocales list. For example:
///
/// ```
/// import '@(importFile)';
///
/// return MaterialApp(
33 34
///   localizationsDelegates: @(class).localizationsDelegates,
///   supportedLocales: @(class).supportedLocales,
35 36 37 38 39 40 41 42 43 44 45 46 47 48
///   home: MyApplicationHome(),
/// );
/// ```
///
/// ## Update pubspec.yaml
///
/// Please make sure to update your pubspec.yaml to include the following
/// packages:
///
/// ```
/// dependencies:
///   # Internationalization support.
///   flutter_localizations:
///     sdk: flutter
Dan Field's avatar
Dan Field committed
49
///   intl: any # Use the pinned version from flutter_localizations
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
///
///   # rest of dependencies
/// ```
///
/// ## 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
71
/// be consistent with the languages listed in the @(class).supportedLocales
72
/// property.
73
abstract class @(class) {
74
  @(class)(String locale) : localeName = intl.Intl.canonicalizedLocale(locale.toString());
75

76
  final String localeName;
77

78 79
  static @(class)@(canBeNullable) of(BuildContext context) {
    return Localizations.of<@(class)>(context, @(class))@(needsNullCheck);
80 81
  }

82
  static const LocalizationsDelegate<@(class)> delegate = _@(class)Delegate();
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

  /// 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.
  ///
  /// 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.
  static const List<LocalizationsDelegate<dynamic>> localizationsDelegates = <LocalizationsDelegate<dynamic>>[
    delegate,
    GlobalMaterialLocalizations.delegate,
    GlobalCupertinoLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
  ];

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

106
@(methods)}
107

108
@(delegateClass)
109 110
''';

111 112 113 114 115 116
const String numberFormatPositionalTemplate = '''
    final intl.NumberFormat @(placeholder)NumberFormat = intl.NumberFormat.@(format)(localeName);
    final String @(placeholder)String = @(placeholder)NumberFormat.format(@(placeholder));
''';

const String numberFormatNamedTemplate = '''
117
    final intl.NumberFormat @(placeholder)NumberFormat = intl.NumberFormat.@(format)(
118
      locale: localeName,
119 120 121 122 123 124
      @(parameters)
    );
    final String @(placeholder)String = @(placeholder)NumberFormat.format(@(placeholder));
''';

const String dateFormatTemplate = '''
125
    final intl.DateFormat @(placeholder)DateFormat = intl.DateFormat.@(format)(localeName);
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    final String @(placeholder)String = @(placeholder)DateFormat.format(@(placeholder));
''';

const String getterTemplate = '''
  @override
  String get @(name) => @(message);''';

const String methodTemplate = '''
  @override
  String @(name)(@(parameters)) {
    return @(message);
  }''';

const String formatMethodTemplate = '''
  @override
  String @(name)(@(parameters)) {
@(dateFormatting)
@(numberFormatting)
    return @(message);
  }''';

const String pluralMethodTemplate = '''
  @override
  String @(name)(@(parameters)) {
@(dateFormatting)
@(numberFormatting)
    return intl.Intl.pluralLogic(
      @(count),
154
      locale: localeName,
155 156 157 158
@(pluralLogicArgs),
    );
  }''';

159 160 161
const String classFileTemplate = '''
@(header)

162 163
@(requiresIntlImport)
import '@(fileName)';
164

165 166 167 168 169
/// The translations for @(language) (`@(localeName)`).
class @(class) extends @(baseClass) {
  @(class)([String locale = '@(localeName)']) : super(locale);

@(methods)
170 171 172 173 174 175 176 177 178 179 180 181
}
@(subclasses)''';

const String subclassTemplate = '''

/// The translations for @(language) (`@(localeName)`).
class @(class) extends @(baseLanguageClassName) {
  @(class)(): super('@(localeName)');

@(methods)
}
''';
182 183

const String baseClassGetterTemplate = '''
184 185 186
  /// @(comment)
  ///
@(templateLocaleTranslationComment)
187
  String get @(name);
188
''';
189 190

const String baseClassMethodTemplate = '''
191 192 193
  /// @(comment)
  ///
@(templateLocaleTranslationComment)
194 195 196
  String @(name)(@(parameters));
''';

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
// DELEGATE CLASS TEMPLATES

const String delegateClassTemplate = '''
class _@(class)Delegate extends LocalizationsDelegate<@(class)> {
  const _@(class)Delegate();

  @override
  Future<@(class)> load(Locale locale) {
    @(loadBody)
  }

  @override
  bool isSupported(Locale locale) => <String>[@(supportedLanguageCodes)].contains(locale.languageCode);

  @override
  bool shouldReload(_@(class)Delegate old) => false;
}

@(lookupFunction)''';

const String loadBodyTemplate = '''return SynchronousFuture<@(class)>(@(lookupName)(locale));''';

const String loadBodyDeferredLoadingTemplate = '''return @(lookupName)(locale);''';

221 222
// DELEGATE LOOKUP TEMPLATES

223
const String lookupFunctionTemplate = r'''
224 225
@(class) @(lookupName)(Locale locale) {
  @(lookupBody)
226 227 228 229 230 231 232

  throw FlutterError(
    '@(class).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.'
  );
233 234
}''';

235
const String lookupFunctionDeferredLoadingTemplate = r'''
236 237
Future<@(class)> @(lookupName)(Locale locale) {
  @(lookupBody)
238 239 240 241 242 243 244

  throw FlutterError(
    '@(class).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.'
  );
245 246
}''';

247 248 249 250 251
const String lookupBodyTemplate = '''
@(lookupAllCodesSpecified)
@(lookupScriptCodeSpecified)
@(lookupCountryCodeSpecified)
@(lookupLanguageCodeSpecified)''';
252

253 254
const String switchClauseTemplate = '''case '@(case)': return @(localeClass)();''';

255
const String switchClauseDeferredLoadingTemplate = '''case '@(case)': return @(library).loadLibrary().then((dynamic _) => @(library).@(localeClass)());''';
256

257 258 259
const String nestedSwitchTemplate = '''
case '@(languageCode)': {
  switch (locale.@(code)) {
260 261
    @(switchClauses)
  }
262 263 264 265
  break;
}''';

const String languageCodeSwitchTemplate = '''
266 267 268 269
  @(comment)
  switch (locale.languageCode) {
    @(switchClauses)
  }
270 271
''';

272
const String allCodesLookupTemplate = '''
273 274 275 276
  // Lookup logic when language+script+country codes are specified.
  switch (locale.toString()) {
    @(allCodesSwitchClauses)
  }
277
''';