-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adapt
package:messages
to be loading mechanism independent (#843)
* Squashed commit * Add newline * Add g.dart files to generated * Upgrade to dart:asset * Add changelogs * Add wip * Run tests only on dev * Add logger * Update examples * Do not use dart:asset * Use a dart run script instead of hooks * Also output code in dart run * Adapt workflow * Fixes * Add licenses * typo * Fix issues * Sort on key * Refactor to list * Sort * Clean ups * sort deps * Changelog * Remove build_runner remaisn * Delete old examples * Add new examples * Fix flutter specific package loading * Fix zero word case * Switch to flutter in wf * Fix CI * Setup flutter * use dart not flutter * do not check flutter examples * Changes as per review * Update CI sha * Use main channel * use beta channel * Use new CI tool * Rename * Add licenses * Use dart run messages again
- Loading branch information
Showing
95 changed files
with
1,724 additions
and
653 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pkgs/intl4x/lib/src/bindings/* linguist-generated=true | ||
pkgs/intl4x/**/*.g.dart linguist-generated=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
## 0.3.0-wip | ||
|
||
- Adapt to output data files to assets. | ||
|
||
## 0.2.0 | ||
|
||
- Remove `IntlObject` interface. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:io'; | ||
|
||
Future<void> main(List<String> args) async { | ||
final runBuilder = await Process.run('dart', ['run', 'messages_builder']); | ||
stdout.write(runBuilder.stdout as String); | ||
|
||
final runBuilderStdErr = runBuilder.stderr as String; | ||
final messagesBuilderNotInDeps = | ||
runBuilderStdErr.contains('Could not find package `messages_builder`'); | ||
if (messagesBuilderNotInDeps) { | ||
print('Adding `package:messages_builder` to dev dependencies...'); | ||
final addBuilder = await runDart(['pub', 'add', 'dev:messages_builder']); | ||
if (addBuilder.exitCode == 0) { | ||
print('Re-running message generation'); | ||
await runDart(['run', 'messages_builder']); | ||
} | ||
} else { | ||
stderr.write(runBuilderStdErr); | ||
} | ||
} | ||
|
||
Future<ProcessResult> runDart(List<String> arguments) async { | ||
final processResult = await Process.run('dart', arguments); | ||
stdout.write(processResult.stdout as String); | ||
stderr.write(processResult.stderr as String); | ||
return processResult; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
|
||
# Conventional directory for build output. | ||
build/ | ||
bin/example/ |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,4 @@ | |
} | ||
}, | ||
"helloAndWelcome2": "Welcome {firstName} von {lastName}!" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,4 +43,4 @@ | |
} | ||
} | ||
} | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
...sages/example_json/lib/testarbctx2_fr.arb → ...es/example/assets/l10n/testarbctx2_fr.arb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
{ | ||
"@@context": "AboutPage", | ||
"@@locale": "fr", | ||
"helloAndWelcome": "Welcome {firstName} von {lastName} <", | ||
"otherMsg": "other", | ||
"aboutMessage": "Sur {websitename}", | ||
"newMessages": "test {newMessages, plural, =0 {No new messages} =1 {One new message} two{Two new Messages} other {test {newMessages} new messages}}", | ||
"newMessages2": "test {gender, select,male {No new messages} female {One new message} other{Two new Messages} other {test {gender} new messages of type {newVar}}}" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[0,"en","dr9Md951",1,null,["helloAndWelcome","Welcome von !",[8,0],[13,1]],["helloAndWelcome2","Welcome von !",[8,0],[13,1]],[6,"newMessages","test ",[3,0,["test new messages",[5,0]],[0,"No new messages",1,"One new message","w2","Two new Messages"]]],[6,"newMessages2","test ",[4,0,"Two new Messages",{"male":"No new messages","female":"One new message"}]]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[0,"de","hbDN1MhX",1,null,["helloAndWelcome","Willkommen von ",[11,0],[16,1]],["helloAndWelcome2","Willkommen von 2",[11,0],[16,1]],[6,"newMessages","testde ",[3,0,["test new messages",[5,0]],[0,"No new messages",1,"One new message","w2","Two new Messages"]]],["newMessages2","testdse is just a simple message"]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[0,"en","QrwRSsOy",1,null,["aboutMessage","About ",[6,0]],["helloAndWelcome","Welcome von <",[8,0],[13,1]],[6,"newMessages","test ",[3,0,["test new messages",[5,0]],[0,"No new messages",1,"One new message","w2","Two new Messages"]]],[6,"newMessages2","test ",[4,0,"Two new Messages",{"male":"No new messages","female":"One new message"}]],["otherMsg","other"]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[0,"fr","390XWry3",1,null,["aboutMessage","Sur ",[4,0]],["helloAndWelcome","Welcome von <",[8,0],[13,1]],[6,"newMessages","test ",[3,0,["test new messages",[5,0]],[0,"No new messages",1,"One new message","w2","Two new Messages"]]],[6,"newMessages2","test ",[4,0,"Two new Messages",{"male":"No new messages","female":"One new message"}]],["otherMsg","other"]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
// Generated by package:messages_builder. | ||
|
||
// ignore_for_file: non_constant_identifier_names | ||
|
||
import 'package:intl/intl.dart'; | ||
import 'package:messages/messages_json.dart'; | ||
|
||
Message _pluralSelector( | ||
num howMany, | ||
String locale, { | ||
required Message other, | ||
Message? few, | ||
Message? many, | ||
Map<int, Message>? numberCases, | ||
Map<int, Message>? wordCases, | ||
}) { | ||
return Intl.pluralLogic( | ||
howMany, | ||
few: few, | ||
many: many, | ||
zero: numberCases?[0] ?? wordCases?[0], | ||
one: numberCases?[1] ?? wordCases?[1], | ||
two: numberCases?[2] ?? wordCases?[2], | ||
other: other, | ||
locale: locale, | ||
); | ||
} | ||
|
||
class AboutPageMessages { | ||
AboutPageMessages(this._assetLoader); | ||
|
||
final Future<String> Function(String id) _assetLoader; | ||
|
||
String _currentLocale = 'en'; | ||
|
||
final Map<String, MessageList> _messages = {}; | ||
|
||
static const _dataFiles = { | ||
'en': ('packages/example/assets/testarbctx2.arb.json', 'QrwRSsOy'), | ||
'fr': ('packages/example/assets/testarbctx2_fr.arb.json', '390XWry3') | ||
}; | ||
|
||
String get currentLocale => _currentLocale; | ||
|
||
MessageList get _currentMessages => _messages[currentLocale]!; | ||
|
||
String getById( | ||
String id, [ | ||
List<dynamic> args = const [], | ||
]) { | ||
return _currentMessages.generateStringAtId(id, args); | ||
} | ||
|
||
static Iterable<String> get knownLocales => _dataFiles.keys; | ||
|
||
Future<void> loadLocale(String locale) async { | ||
if (!_messages.containsKey(locale)) { | ||
final info = _dataFiles[locale]; | ||
final dataFile = info?.$1; | ||
if (dataFile == null) { | ||
throw ArgumentError('Locale $locale is not in $knownLocales'); | ||
} | ||
final data = await _assetLoader(dataFile); | ||
final messageList = MessageListJson.fromString(data, _pluralSelector); | ||
if (messageList.preamble.hash != info?.$2) { | ||
throw ArgumentError(''' | ||
Messages file for locale $locale has different hash "${messageList.preamble.hash}" than generated code "${info?.$2}".'''); | ||
} | ||
_messages[locale] = messageList; | ||
} | ||
_currentLocale = locale; | ||
} | ||
|
||
Future<void> loadAllLocales() async { | ||
for (final locale in knownLocales) { | ||
await loadLocale(locale); | ||
} | ||
} | ||
|
||
String aboutMessage(String websitename) => | ||
_currentMessages.generateStringAtIndex(0, [websitename]); | ||
|
||
String helloAndWelcome( | ||
String firstName, | ||
int lastName, | ||
) => | ||
_currentMessages.generateStringAtIndex(1, [firstName, lastName]); | ||
|
||
String newMessages(int newMessages) => | ||
_currentMessages.generateStringAtIndex(2, [newMessages]); | ||
|
||
String newMessages2( | ||
String gender, | ||
int newVar, | ||
) => | ||
_currentMessages.generateStringAtIndex(3, [gender, newVar]); | ||
|
||
String get otherMsg => _currentMessages.generateStringAtIndex(4, []); | ||
} | ||
|
||
class HomePageMessages { | ||
HomePageMessages(this._assetLoader); | ||
|
||
final Future<String> Function(String id) _assetLoader; | ||
|
||
String _currentLocale = 'en'; | ||
|
||
final Map<String, MessageList> _messages = {}; | ||
|
||
static const _dataFiles = { | ||
'de': ('packages/example/assets/testarb_de.arb.json', 'hbDN1MhX'), | ||
'en': ('packages/example/assets/testarb.arb.json', 'dr9Md951') | ||
}; | ||
|
||
String get currentLocale => _currentLocale; | ||
|
||
MessageList get _currentMessages => _messages[currentLocale]!; | ||
|
||
String getById( | ||
String id, [ | ||
List<dynamic> args = const [], | ||
]) { | ||
return _currentMessages.generateStringAtId(id, args); | ||
} | ||
|
||
static Iterable<String> get knownLocales => _dataFiles.keys; | ||
|
||
Future<void> loadLocale(String locale) async { | ||
if (!_messages.containsKey(locale)) { | ||
final info = _dataFiles[locale]; | ||
final dataFile = info?.$1; | ||
if (dataFile == null) { | ||
throw ArgumentError('Locale $locale is not in $knownLocales'); | ||
} | ||
final data = await _assetLoader(dataFile); | ||
final messageList = MessageListJson.fromString(data, _pluralSelector); | ||
if (messageList.preamble.hash != info?.$2) { | ||
throw ArgumentError(''' | ||
Messages file for locale $locale has different hash "${messageList.preamble.hash}" than generated code "${info?.$2}".'''); | ||
} | ||
_messages[locale] = messageList; | ||
} | ||
_currentLocale = locale; | ||
} | ||
|
||
Future<void> loadAllLocales() async { | ||
for (final locale in knownLocales) { | ||
await loadLocale(locale); | ||
} | ||
} | ||
|
||
String helloAndWelcome( | ||
String firstName, | ||
String lastName, | ||
) => | ||
_currentMessages.generateStringAtIndex(0, [firstName, lastName]); | ||
|
||
String helloAndWelcome2( | ||
String firstName, | ||
String lastName, | ||
) => | ||
_currentMessages.generateStringAtIndex(1, [firstName, lastName]); | ||
|
||
String newMessages(int newMessages) => | ||
_currentMessages.generateStringAtIndex(2, [newMessages]); | ||
|
||
String newMessages2( | ||
String gender, | ||
int newVar, | ||
) => | ||
_currentMessages.generateStringAtIndex(3, [gender, newVar]); | ||
} |
Oops, something went wrong.