Skip to content

Commit f56e144

Browse files
committed
Updates
1 parent 99ca13b commit f56e144

21 files changed

+186
-156
lines changed

pkgs/intl_translation/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## 0.21.0
22
* BREAKING CHANGE: Update `dart_style` to `^3.0.0`
33
* Allow analyzer `>=6.3.0 <8.0.0`
4-
* Upgrade SDK constraint to `^3.7.0`.
4+
* Upgrade SDK constraint to `^3.9.0`.
55

66
## 0.20.1
77
* Add topics to `pubspec.yaml`

pkgs/intl_translation/bin/generate_from_arb.dart

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -275,17 +275,16 @@ void generateLocaleFile(
275275
MessageGeneration generation,
276276
Map<String, List<MainMessage>> messages,
277277
) {
278-
var translations =
279-
localeData
280-
.expand((jsonTranslations) {
281-
return jsonTranslations.entries.map((e) {
282-
var id = e.key;
283-
var messageData = e.value;
284-
return recreateIntlObjects(id, messageData, messages);
285-
});
286-
})
287-
.whereType<TranslatedMessage>()
288-
.toList();
278+
var translations = localeData
279+
.expand((jsonTranslations) {
280+
return jsonTranslations.entries.map((e) {
281+
var id = e.key;
282+
var messageData = e.value;
283+
return recreateIntlObjects(id, messageData, messages);
284+
});
285+
})
286+
.whereType<TranslatedMessage>()
287+
.toList();
289288
generation.generateIndividualMessageFile(locale, translations, targetDir);
290289
}
291290

pkgs/intl_translation/bin/make_examples_const.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'dart:io';
1010

1111
import 'package:args/args.dart';
1212
import 'package:dart_style/dart_style.dart';
13+
import 'package:intl_translation/src/language_version.dart';
1314
import 'package:intl_translation/src/message_rewriter.dart';
1415
import 'package:intl_translation/src/messages/main_message.dart';
1516

@@ -37,7 +38,10 @@ Future<void> main(List<String> args) async {
3738
print('Writing new source to $outputFile');
3839
var out = File(outputFile);
3940

40-
var formatter = DartFormatter();
41+
var languageVersion =
42+
(await findPackageLanguageVersion(file)) ??
43+
DartFormatter.latestLanguageVersion;
44+
var formatter = DartFormatter(languageVersion: languageVersion);
4145

4246
out.writeAsStringSync(formatter.format(newSource));
4347
}

pkgs/intl_translation/bin/rewrite_intl_messages.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import 'dart:io';
1616

1717
import 'package:args/args.dart';
1818
import 'package:dart_style/dart_style.dart';
19+
import 'package:intl_translation/src/language_version.dart';
1920
import 'package:intl_translation/src/message_rewriter.dart';
2021

2122
String? outputFileOption = 'transformed_output.dart';
@@ -81,7 +82,10 @@ Future<void> main(List<String> args) async {
8182
print('Writing new source to $outputFile');
8283
var out = File(outputFile!);
8384

84-
var formatter = DartFormatter();
85+
var languageVersion =
86+
(await findPackageLanguageVersion(file)) ??
87+
DartFormatter.latestLanguageVersion;
88+
var formatter = DartFormatter(languageVersion: languageVersion);
8589

8690
out.writeAsStringSync(formatter.format(newSource));
8791
}

pkgs/intl_translation/lib/extract_messages.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,10 @@ class MessageExtraction {
129129
throwIfDiagnostics: false,
130130
);
131131

132-
final errors = List.of(result.errors)..removeWhere(
133-
(e) => ignoredErrorCodes.contains(e.errorCode.name.toLowerCase()),
134-
);
132+
final errors = List.of(result.errors)
133+
..removeWhere(
134+
(e) => ignoredErrorCodes.contains(e.diagnosticCode.name.toLowerCase()),
135+
);
135136
if (errors.isNotEmpty) {
136137
print('Error in parsing $origin, no messages extracted.');
137138
throw ArgumentError('Parsing errors in $origin');

pkgs/intl_translation/lib/generate_localized.dart

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,9 @@ class MessageGeneration {
108108
// Exclude messages with no translation and translations with no matching
109109
// original message (e.g. if we're using some messages from a larger
110110
// catalog)
111-
var usableTranslations =
112-
translations
113-
.where((translation) => translation.originalMessages.isNotEmpty)
114-
.toList();
111+
var usableTranslations = translations
112+
.where((translation) => translation.originalMessages.isNotEmpty)
113+
.toList();
115114
for (var translation in usableTranslations) {
116115
for (var original in translation.originalMessages) {
117116
original.addTranslation(locale, translation.message);
@@ -136,8 +135,10 @@ class MessageGeneration {
136135
for (var translation in usableTranslations) {
137136
// Some messages we generate as methods in this class. Simpler ones
138137
// we inline in the map from names to messages.
139-
var messagesThatNeedMethods =
140-
translation.originalMessages.where(_hasArguments).toSet().toList();
138+
var messagesThatNeedMethods = translation.originalMessages
139+
.where(_hasArguments)
140+
.toSet()
141+
.toList();
141142
for (var original in messagesThatNeedMethods) {
142143
output
143144
..write(' ')
@@ -151,17 +152,18 @@ class MessageGeneration {
151152

152153
// Now write the map of names to either the direct translation or to a
153154
// method.
154-
var names = (usableTranslations
155-
.expand((translation) => translation.originalMessages)
156-
.toSet()
157-
.toList()
158-
..sort((a, b) => a.name.compareTo(b.name)))
159-
.map(
160-
(original) =>
161-
" '${Message.escapeString(original.name)}'"
162-
': ${_mapReference(original, locale)}',
163-
)
164-
.join(',\n');
155+
var names =
156+
(usableTranslations
157+
.expand((translation) => translation.originalMessages)
158+
.toSet()
159+
.toList()
160+
..sort((a, b) => a.name.compareTo(b.name)))
161+
.map(
162+
(original) =>
163+
" '${Message.escapeString(original.name)}'"
164+
': ${_mapReference(original, locale)}',
165+
)
166+
.join(',\n');
165167
output
166168
..write(names)
167169
..write('\n };\n}\n');
@@ -184,7 +186,8 @@ class MessageGeneration {
184186

185187
/// [generateIndividualMessageFile] for the beginning of the file,
186188
/// parameterized by [locale].
187-
String prologue(String locale) => '''
189+
String prologue(String locale) =>
190+
'''
188191
// DO NOT EDIT. This is code generated via package:intl/generate_localized.dart
189192
// This is a library that provides messages for a $locale locale. All the
190193
// messages from the main program should be duplicated here with the same
@@ -250,10 +253,9 @@ ${releaseMode ? overrideLookup() : ''}''';
250253
output.write('Map<String, LibraryLoader> _deferredLibraries = {\n');
251254
for (var rawLocale in allLocales) {
252255
var locale = Intl.canonicalizedLocale(rawLocale);
253-
var loadOperation =
254-
(useDeferredLoading)
255-
? " '$locale': ${libraryName(locale)}.loadLibrary,\n"
256-
: " '$locale': () => Future.value(null),\n";
256+
var loadOperation = (useDeferredLoading)
257+
? " '$locale': ${libraryName(locale)}.loadLibrary,\n"
258+
: " '$locale': () => Future.value(null),\n";
257259
output.write(loadOperation);
258260
}
259261
output.write('};\n');
@@ -273,7 +275,8 @@ ${releaseMode ? overrideLookup() : ''}''';
273275

274276
/// Constant string used in [generateLocalesImportFile] for the beginning of
275277
/// the file.
276-
String get localesPrologue => """
278+
String get localesPrologue =>
279+
"""
277280
// DO NOT EDIT. This is code generated via package:intl/generate_localized.dart
278281
// This is a library that looks up messages for specific locales by
279282
// delegating to the appropriate library.
@@ -373,7 +376,8 @@ abstract class DataMapMessageGeneration extends MessageGeneration {
373376
/// We import the main file so as to get the shared code to evaluate
374377
/// the JSON data.
375378
@override
376-
String get extraImports => '''
379+
String get extraImports =>
380+
'''
377381
import 'dart:convert';
378382
379383
import '${generatedFilePrefix}messages_all.dart' show evaluateJsonTemplate;
@@ -393,7 +397,8 @@ import '${generatedFilePrefix}messages_all.dart' show evaluateJsonTemplate;
393397
);
394398

395399
@override
396-
String get mainPrologue => """${super.mainPrologue}
400+
String get mainPrologue =>
401+
"""${super.mainPrologue}
397402
import 'package:$intlImportPath/intl.dart';
398403
""";
399404

@@ -472,7 +477,8 @@ String? evaluateJsonTemplate(dynamic input, List<dynamic> args) {
472477

473478
/// Constant string used in [generateFlutterImportFile] for the beginning of
474479
/// the file.
475-
String get flutterPrologue => """
480+
String get flutterPrologue =>
481+
"""
476482
// DO NOT EDIT. This is code generated via package:intl/generate_localized.dart
477483
// This is a library that looks up messages for specific locales by
478484
// delegating to the appropriate library.
@@ -619,7 +625,8 @@ class JsonMessageGeneration extends DataMapMessageGeneration {
619625
/// Message generator that stores translations in a constant map.
620626
class CodeMapMessageGeneration extends JsonMessageGeneration {
621627
@override
622-
String get extraImports => '''
628+
String get extraImports =>
629+
'''
623630
${super.extraImports}
624631
import 'dart:collection';
625632
''';
@@ -753,10 +760,9 @@ class TranslatedMessage {
753760

754761
/// For backward compatibility, we still have the originalMessage API.
755762
MainMessage get originalMessage => originalMessages.first;
756-
set originalMessage(MainMessage m) =>
757-
originalMessages
758-
..clear()
759-
..add(m);
763+
set originalMessage(MainMessage m) => originalMessages
764+
..clear()
765+
..add(m);
760766

761767
TranslatedMessage(this.id, this.translated, this.originalMessages);
762768

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
import 'dart:async';
5+
import 'dart:io';
6+
7+
import 'package:package_config/package_config.dart';
8+
import 'package:pub_semver/pub_semver.dart';
9+
10+
/// Looks for a package surrounding [file] and, if found, returns the default
11+
/// language version specified by that package.
12+
Future<Version?> findPackageLanguageVersion(File file) async {
13+
try {
14+
var config = await findPackageConfig(file.parent);
15+
if (config?.packageOf(file.absolute.uri)?.languageVersion
16+
case var languageVersion?) {
17+
return Version(languageVersion.major, languageVersion.minor, 0);
18+
}
19+
} catch (error) {
20+
// If we fail to find or read a config, just silently do nothing and
21+
// default to the latest language version.
22+
}
23+
24+
return null;
25+
}

pkgs/intl_translation/lib/src/message_parser.dart

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,9 @@ class _ParserUtil {
117117
}
118118

119119
/// Match a simple string
120-
At<LiteralString>? matchString(int at, String t) =>
121-
input.startsWith(t, at)
122-
? At(LiteralString(t, null), at + t.length)
123-
: null;
120+
At<LiteralString>? matchString(int at, String t) => input.startsWith(t, at)
121+
? At(LiteralString(t, null), at + t.length)
122+
: null;
124123

125124
/// Match any of the given keywords
126125
At<LiteralString>? asKeywords(Map<String, RegExp> keywordsToRegex, int at) {
@@ -136,13 +135,9 @@ class _ParserUtil {
136135
}
137136

138137
/// Parse whitespace
139-
At<LiteralString> trimAt(int at) =>
140-
at < input.length
141-
? At(
142-
LiteralString(input),
143-
RegExp(r'\s*').matchAsPrefix(input, at)!.end,
144-
)
145-
: At(LiteralString(''), at);
138+
At<LiteralString> trimAt(int at) => at < input.length
139+
? At(LiteralString(input), RegExp(r'\s*').matchAsPrefix(input, at)!.end)
140+
: At(LiteralString(''), at);
146141

147142
Pattern matchUnescaped(String curly) => RegExp("(?:^|''|[^'])($curly)");
148143

@@ -293,18 +288,19 @@ class _ParserUtil {
293288

294289
At<CompositeMessage>? genderClause(int at) {
295290
return oneOrMore(
296-
(s1) => and([
297-
(s) => trimAt(s),
298-
(s) => genderKeyword(s),
299-
(s) => openCurly(s),
300-
(s) => interiorText(s),
301-
(s) => closeCurly(s),
302-
(s) => trimAt(s),
303-
], s1)?.mapResult((compMsg) {
304-
var genderKeyword = compMsg.pieces[1] as LiteralString;
305-
var interiorText = compMsg.pieces[3];
306-
return PairMessage(genderKeyword, interiorText);
307-
}),
291+
(s1) =>
292+
and([
293+
(s) => trimAt(s),
294+
(s) => genderKeyword(s),
295+
(s) => openCurly(s),
296+
(s) => interiorText(s),
297+
(s) => closeCurly(s),
298+
(s) => trimAt(s),
299+
], s1)?.mapResult((compMsg) {
300+
var genderKeyword = compMsg.pieces[1] as LiteralString;
301+
var interiorText = compMsg.pieces[3];
302+
return PairMessage(genderKeyword, interiorText);
303+
}),
308304
at,
309305
);
310306
}
@@ -327,16 +323,17 @@ class _ParserUtil {
327323

328324
At<CompositeMessage>? selectClause(int at) {
329325
return oneOrMore(
330-
(s1) => and([
331-
(s) => id(s),
332-
(s) => openCurly(s),
333-
(s) => interiorText(s),
334-
(s) => closeCurly(s),
335-
], s1)?.mapResult((compMsg) {
336-
var id = compMsg.pieces[0] as LiteralString;
337-
var interiorText = compMsg.pieces[2];
338-
return PairMessage(id, interiorText);
339-
}),
326+
(s1) =>
327+
and([
328+
(s) => id(s),
329+
(s) => openCurly(s),
330+
(s) => interiorText(s),
331+
(s) => closeCurly(s),
332+
], s1)?.mapResult((compMsg) {
333+
var id = compMsg.pieces[0] as LiteralString;
334+
var interiorText = compMsg.pieces[2];
335+
return PairMessage(id, interiorText);
336+
}),
340337
at,
341338
);
342339
}

pkgs/intl_translation/lib/src/messages/constant_evaluator.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ Constant<Map>? evaluateConstStringMap(SetOrMapLiteral map) {
4848
return null;
4949
}
5050
final evaluatedEntries = map.elements.whereType<MapLiteralEntry>().map(
51-
(literalEntry) => (
52-
evaluate(literalEntry.key),
53-
evaluate(literalEntry.value),
54-
),
51+
(literalEntry) =>
52+
(evaluate(literalEntry.key), evaluate(literalEntry.value)),
5553
);
5654
if (evaluatedEntries.any(
5755
(element) => element.$1 == null || element.$2 == null,

pkgs/intl_translation/lib/src/messages/main_message.dart

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,12 @@ class MainMessage extends ComplexMessage {
142142
/// Generate code for this message, expecting it to be part of a map
143143
/// keyed by name with values the function that calls Intl.message.
144144
String toCodeForLocale(String locale, String name) {
145-
var out =
146-
StringBuffer()
147-
..write('static $name(')
148-
..write(arguments.join(', '))
149-
..write(') => "')
150-
..write(translations[locale])
151-
..write('";');
145+
var out = StringBuffer()
146+
..write('static $name(')
147+
..write(arguments.join(', '))
148+
..write(') => "')
149+
..write(translations[locale])
150+
..write('";');
152151
return out.toString();
153152
}
154153

0 commit comments

Comments
 (0)