gen_l10n_templates.dart 7.79 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
// ignore: unused_import
16
import 'package:flutter/foundation.dart';
17 18
import 'package:flutter/widgets.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
19
import 'package:intl/intl.dart' as intl;
20

21 22
@(messageClassImports)

23 24
/// Callers can lookup localized strings with an instance of @(class) returned
/// by `@(class).of(context)`.
25
///
26 27
/// Applications need to include `@(class).delegate()` in their app's
/// localizationDelegates list, and the locales they support in the app's
28 29 30 31 32 33
/// supportedLocales list. For example:
///
/// ```
/// import '@(importFile)';
///
/// return MaterialApp(
34 35
///   localizationsDelegates: @(class).localizationsDelegates,
///   supportedLocales: @(class).supportedLocales,
36 37 38 39 40 41 42 43 44 45 46 47 48 49
///   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
50
///   intl: ^0.17.0-nullsafety.2
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
///
///   # 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
72
/// be consistent with the languages listed in the @(class).supportedLocales
73
/// property.
74
abstract class @(class) {
75
  @(class)(String locale) : localeName = intl.Intl.canonicalizedLocale(locale.toString());
76

77
  // ignore: unused_field
78
  final String localeName;
79

80
  static @(class)? of(BuildContext context) {
81
    return Localizations.of<@(class)>(context, @(class));
82 83
  }

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

  /// 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.
104 105 106
  static const List<Locale> supportedLocales = <Locale>[
    @(supportedLocales)
  ];
107

108
@(methods)}
109

110
@(delegateClass)
111 112 113 114
''';

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

const String dateFormatTemplate = '''
122
    final intl.DateFormat @(placeholder)DateFormat = intl.DateFormat.@(format)(localeName);
123 124 125 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
    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),
151
      locale: localeName,
152 153 154 155
@(pluralLogicArgs),
    );
  }''';

156 157 158 159 160 161 162 163
const String classFileTemplate = '''
@(header)
// ignore: unused_import
import 'package:intl/intl.dart' as intl;
import '@(fileName)';

// ignore_for_file: unnecessary_brace_in_string_interps

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

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

const String subclassTemplate = '''

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

@(methods)
}
''';
181 182

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

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

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
// 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);''';

220 221
// DELEGATE LOOKUP TEMPLATES

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

  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.'
  );
232 233
}''';

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

  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.'
  );
244 245
}''';

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

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

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

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

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

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