gen_l10n_templates.dart 8.93 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)import 'dart:async';
13

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

19 20
@(messageClassImports)

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

75
  final String localeName;
76

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

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

  /// 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.
101 102 103
  static const List<Locale> supportedLocales = <Locale>[
    @(supportedLocales)
  ];
104

105
@(methods)}
106

107
@(delegateClass)
108 109
''';

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

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

const String dateFormatTemplate = '''
124
    final intl.DateFormat @(placeholder)DateFormat = intl.DateFormat.@(format)(localeName);
125 126 127
    final String @(placeholder)String = @(placeholder)DateFormat.format(@(placeholder));
''';

128 129 130 131 132
const String dateFormatCustomTemplate = '''
    final intl.DateFormat @(placeholder)DateFormat = intl.DateFormat(@(format), localeName);
    final String @(placeholder)String = @(placeholder)DateFormat.format(@(placeholder));
''';

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
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),
158
      locale: localeName,
159 160 161 162
@(pluralLogicArgs),
    );
  }''';

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
const String pluralMethodTemplateInString = '''
  @override
  String @(name)(@(parameters)) {
@(dateFormatting)
@(numberFormatting)
    final String @(variable) = intl.Intl.pluralLogic(
      @(count),
      locale: localeName,
@(pluralLogicArgs),
    );

    return @(string);
  }''';

const String selectMethodTemplate = '''
  @override
  String @(name)(@(parameters)) {
    return intl.Intl.select(
      @(choice),
      {
        @(cases)
      },
      desc: '@(description)'
    );
  }''';

const String selectMethodTemplateInString = '''
  @override
  String @(name)(@(parameters)) {
    final String @(variable) = intl.Intl.select(
      @(choice),
      {
        @(cases)
      },
      desc: '@(description)'
    );

    return @(string);
  }''';

203
const String classFileTemplate = '''
204
@(header)@(requiresIntlImport)import '@(fileName)';
205

206 207 208 209 210
/// The translations for @(language) (`@(localeName)`).
class @(class) extends @(baseClass) {
  @(class)([String locale = '@(localeName)']) : super(locale);

@(methods)
211 212 213 214 215 216 217 218 219 220 221 222
}
@(subclasses)''';

const String subclassTemplate = '''

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

@(methods)
}
''';
223 224

const String baseClassGetterTemplate = '''
225 226 227
  /// @(comment)
  ///
@(templateLocaleTranslationComment)
228
  String get @(name);
229
''';
230 231

const String baseClassMethodTemplate = '''
232 233 234
  /// @(comment)
  ///
@(templateLocaleTranslationComment)
235 236 237
  String @(name)(@(parameters));
''';

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
// 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);''';

262 263
// DELEGATE LOOKUP TEMPLATES

264
const String lookupFunctionTemplate = r'''
265 266
@(class) @(lookupName)(Locale locale) {
  @(lookupBody)
267 268 269 270 271 272 273

  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.'
  );
274 275
}''';

276
const String lookupFunctionDeferredLoadingTemplate = r'''
277 278
Future<@(class)> @(lookupName)(Locale locale) {
  @(lookupBody)
279 280 281 282 283 284 285

  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.'
  );
286 287
}''';

288 289 290 291 292
const String lookupBodyTemplate = '''
@(lookupAllCodesSpecified)
@(lookupScriptCodeSpecified)
@(lookupCountryCodeSpecified)
@(lookupLanguageCodeSpecified)''';
293

294 295
const String switchClauseTemplate = '''case '@(case)': return @(localeClass)();''';

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

298 299 300
const String nestedSwitchTemplate = '''
case '@(languageCode)': {
  switch (locale.@(code)) {
301 302
    @(switchClauses)
  }
303 304 305 306
  break;
}''';

const String languageCodeSwitchTemplate = '''
307 308 309 310
  @(comment)
  switch (locale.languageCode) {
    @(switchClauses)
  }
311 312
''';

313
const String allCodesLookupTemplate = '''
314 315 316 317
  // Lookup logic when language+script+country codes are specified.
  switch (locale.toString()) {
    @(allCodesSwitchClauses)
  }
318
''';