@@ -129,6 +129,25 @@ class L10nException implements Exception {
129
129
String toString () => message;
130
130
}
131
131
132
+ class L10nParserException extends L10nException {
133
+ L10nParserException (
134
+ this .error,
135
+ this .fileName,
136
+ this .messageId,
137
+ this .messageString,
138
+ this .charNumber
139
+ ): super ('''
140
+ $error
141
+ [$fileName :$messageId ] $messageString
142
+ ${List <String >.filled (4 + fileName .length + messageId .length + charNumber , ' ' ).join ()}^''' );
143
+
144
+ final String error;
145
+ final String fileName;
146
+ final String messageId;
147
+ final String messageString;
148
+ final int charNumber;
149
+ }
150
+
132
151
// One optional named parameter to be used by a NumberFormat.
133
152
//
134
153
// Some of the NumberFormat factory constructors have optional named parameters.
@@ -202,16 +221,16 @@ class Placeholder {
202
221
final String resourceId;
203
222
final String name;
204
223
final String ? example;
205
- final String ? type;
224
+ String ? type;
206
225
final String ? format;
207
226
final List <OptionalParameter > optionalParameters;
208
227
final bool ? isCustomDateFormat;
209
228
210
- bool get requiresFormatting => < String > ['DateTime' , 'double' , 'num' ].contains (type) || (type == 'int' && format != null );
211
- bool get isNumber => < String > ['double' , 'int' , 'num' ].contains (type);
229
+ bool get requiresFormatting => requiresDateFormatting || requiresNumFormatting;
230
+ bool get requiresDateFormatting => type == 'DateTime' ;
231
+ bool get requiresNumFormatting => < String > ['int' , 'num' , 'double' ].contains (type) && format != null ;
212
232
bool get hasValidNumberFormat => _validNumberFormats.contains (format);
213
233
bool get hasNumberFormatWithParameters => _numberFormatsWithNamedParameters.contains (format);
214
- bool get isDate => 'DateTime' == type;
215
234
bool get hasValidDateFormat => _validDateFormats.contains (format);
216
235
217
236
static String ? _stringAttribute (
@@ -290,6 +309,8 @@ class Placeholder {
290
309
// The value of this Message is "Hello World". The Message's value is the
291
310
// localized string to be shown for the template ARB file's locale.
292
311
// The docs for the Placeholder explain how placeholder entries are defined.
312
+ // TODO(thkim1011): We need to refactor this Message class to own all the messages in each language.
313
+ // See https://github.com/flutter/flutter/issues/112709.
293
314
class Message {
294
315
Message (Map <String , Object ?> bundle, this .resourceId, bool isResourceAttributeRequired)
295
316
: assert (bundle != null ),
@@ -298,7 +319,12 @@ class Message {
298
319
description = _description (bundle, resourceId, isResourceAttributeRequired),
299
320
placeholders = _placeholders (bundle, resourceId, isResourceAttributeRequired),
300
321
_pluralMatch = _pluralRE.firstMatch (_value (bundle, resourceId)),
301
- _selectMatch = _selectRE.firstMatch (_value (bundle, resourceId));
322
+ _selectMatch = _selectRE.firstMatch (_value (bundle, resourceId)) {
323
+ if (isPlural) {
324
+ final Placeholder placeholder = getCountPlaceholder ();
325
+ placeholder.type = 'num' ;
326
+ }
327
+ }
302
328
303
329
static final RegExp _pluralRE = RegExp (r'\s*\{([\w\s,]*),\s*plural\s*,' );
304
330
static final RegExp _selectRE = RegExp (r'\s*\{([\w\s,]*),\s*select\s*,' );
@@ -769,3 +795,50 @@ final Set<String> _iso639Languages = <String>{
769
795
'zh' ,
770
796
'zu' ,
771
797
};
798
+
799
+ // Used in LocalizationsGenerator._generateMethod.generateHelperMethod.
800
+ class HelperMethod {
801
+ HelperMethod (this .dependentPlaceholders, {this .helper, this .placeholder, this .string }):
802
+ assert ((() {
803
+ // At least one of helper, placeholder, string must be nonnull.
804
+ final bool a = helper == null ;
805
+ final bool b = placeholder == null ;
806
+ final bool c = string == null ;
807
+ return (! a && b && c) || (a && ! b && c) || (a && b && ! c);
808
+ })());
809
+
810
+ Set <Placeholder > dependentPlaceholders;
811
+ String ? helper;
812
+ Placeholder ? placeholder;
813
+ String ? string;
814
+
815
+ String get helperOrPlaceholder {
816
+ if (helper != null ) {
817
+ return '$helper ($methodArguments )' ;
818
+ } else if (string != null ) {
819
+ return '$string ' ;
820
+ } else {
821
+ if (placeholder! .requiresFormatting) {
822
+ return '${placeholder !.name }String' ;
823
+ } else {
824
+ return placeholder! .name;
825
+ }
826
+ }
827
+ }
828
+
829
+ String get methodParameters {
830
+ assert (helper != null );
831
+ return dependentPlaceholders.map ((Placeholder placeholder) =>
832
+ (placeholder.requiresFormatting)
833
+ ? 'String ${placeholder .name }String'
834
+ : '${placeholder .type } ${placeholder .name }' ).join (', ' );
835
+ }
836
+
837
+ String get methodArguments {
838
+ assert (helper != null );
839
+ return dependentPlaceholders.map ((Placeholder placeholder) =>
840
+ (placeholder.requiresFormatting)
841
+ ? '${placeholder .name }String'
842
+ : placeholder.name).join (', ' );
843
+ }
844
+ }
0 commit comments