Skip to content

Commit

Permalink
Adaptions for Flutter Gallery demo
Browse files Browse the repository at this point in the history
  • Loading branch information
mosuem committed Oct 25, 2023
1 parent f7d19cd commit 36f1d62
Show file tree
Hide file tree
Showing 41 changed files with 375 additions and 350 deletions.
12 changes: 6 additions & 6 deletions pkgs/messages/example_json/bin/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ import 'package:messages/package_intl_object.dart';

Future<void> main(List<String> arguments) async {
final messages = AboutPageMessages(
(String id) => File('lib/$id').readAsStringSync(),
OldIntlObject(),
(String id) async => File(id).readAsString(),
const OldIntlObject(),
);
// final index = AboutPageMessagesEnum.aboutMessage;

messages.loadLocale('en');
await messages.loadLocale('en');
print('AboutMessage en:');
print('\t${messages.aboutMessage(websitename: 'typesafe.en')}');
print('\t${messages.aboutMessage('typesafe.en')}');

///To enable this, add `generateFindById: true` to the pubspec section
// print('\t${messages.getById('aboutMessage', ['get-by-id.en'])}');

///To enable this, add `generateFindBy: enumerate` to the pubspec section
// print('\t${messages.getByEnum(index, ['get-by-index.en'])}');

messages.loadLocale('fr');
await messages.loadLocale('fr');
print('AboutMessage fr:');
print('\t${messages.aboutMessage(websitename: 'typesafe.fr')}');
print('\t${messages.aboutMessage('typesafe.fr')}');

///To enable this, add `generateFindById: true` to the pubspec section
// print('\t${messages.getById('aboutMessage', ['get-by-id.fr'])}');
Expand Down
1 change: 0 additions & 1 deletion pkgs/messages/example_json/lib/testarb.arb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"@@locale": "en",
"@@x-reference": true,
"@@context": "HomePage",
"helloAndWelcome": "Welcome {firstName} von {lastName}!",
"@helloAndWelcome": {
Expand Down
53 changes: 26 additions & 27 deletions pkgs/messages/example_json/lib/testarb.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ class HomePageMessages {
this.intlObject,
);

final String Function(String id) _fileLoader;
final Future<String> Function(String id) _fileLoader;

String _currentLocale = 'en';

final Map<String, MessageList> _messages = {};

final _carbs = {'de': 'testarb_de.json', 'en': 'testarb.json'};
static const carbs = {'de': 'lib/testarb_de.json', 'en': 'lib/testarb.json'};

final _messageListHashes = {
'testarb_de.json': '8qk919',
'testarb.json': 's69t31'
'lib/testarb_de.json': 'ju3nni',
'lib/testarb.json': 'rqcv4f'
};

IntlObject intlObject;
Expand All @@ -27,15 +27,15 @@ class HomePageMessages {

MessageList get _currentMessages => _messages[currentLocale]!;

Iterable<String> get knownLocales => _carbs.keys;
static Iterable<String> get knownLocales => carbs.keys;

void loadLocale(String locale) {
Future<void> loadLocale(String locale) async {
if (!_messages.containsKey(locale)) {
final carb = _carbs[locale];
final carb = carbs[locale];
if (carb == null) {
throw ArgumentError('Locale $locale is not in $knownLocales');
}
final data = _fileLoader(carb);
final data = await _fileLoader(carb);
final messageList = MessageListJson.fromString(data, intlObject);
if (messageList.preamble.hash != _messageListHashes[carb]) {
throw ArgumentError('''
Expand All @@ -52,35 +52,34 @@ class HomePageMessages {
}
}

String helloAndWelcome({
required String firstName,
required String lastName,
}) =>
String helloAndWelcome(
String firstName,
String lastName,
) =>
_currentMessages.generateStringAtIndex(
HomePageMessagesEnum.helloAndWelcome.index, [firstName, lastName]);

String newMessages({required int newMessages}) =>
String helloAndWelcome2(
String firstName,
String lastName,
) =>
_currentMessages.generateStringAtIndex(
HomePageMessagesEnum.newMessages.index, [newMessages]);
HomePageMessagesEnum.helloAndWelcome2.index, [firstName, lastName]);

String newMessages2({
required String gender,
required int newVar,
}) =>
_currentMessages.generateStringAtIndex(
HomePageMessagesEnum.newMessages2.index, [gender, newVar]);
String newMessages(int newMessages) => _currentMessages.generateStringAtIndex(
HomePageMessagesEnum.newMessages.index, [newMessages]);

String helloAndWelcome2({
required String firstName,
required String lastName,
}) =>
String newMessages2(
String gender,
int newVar,
) =>
_currentMessages.generateStringAtIndex(
HomePageMessagesEnum.helloAndWelcome2.index, [firstName, lastName]);
HomePageMessagesEnum.newMessages2.index, [gender, newVar]);
}

enum HomePageMessagesEnum {
helloAndWelcome,
helloAndWelcome2,
newMessages,
newMessages2,
helloAndWelcome2
newMessages2
}
2 changes: 1 addition & 1 deletion pkgs/messages/example_json/lib/testarb.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[0,"en","s69t31",0,null,["Welcome von !",["8","0"],["d","1"]],[6,"test ",[3,0,["test new messages",["5","0"]],[2,"No new messages",4,"One new message",5,"Two new Messages"]]],[6,"test ",[4,0,"Two new Messages",{"male":"No new messages","female":"One new message"}]],["Welcome von !",["8","0"],["d","1"]]]
[0,"en","rqcv4f",0,null,["Welcome von !",[8,0],[13,1]],["Welcome von !",[8,0],[13,1]],[6,"test ",[3,0,["test new messages",[5,0]],[2,"No new messages",4,"One new message",5,"Two new Messages"]]],[6,"test ",[4,0,"Two new Messages",{"male":"No new messages","female":"One new message"}]]]
10 changes: 2 additions & 8 deletions pkgs/messages/example_json/lib/testarb_de.arb
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
{
"@@locale":"de",
"@@locale": "de",
"@@context": "HomePage",
"helloAndWelcome": "Willkommen {firstName} von {lastName}",
"helloAndWelcome2": "Willkommen {firstName} von {lastName} 2",
"newMessages": "testde {newMessages, plural, =0 {No new messages} =1 {One new message} two{Two new Messages} other {test {newMessages} new messages}}",
"@newMessages": {
"placeholders": {
"newMessages" : {
"type":"int"
}
}
},
"newMessages2": "testdse is just a simple message"
}
2 changes: 1 addition & 1 deletion pkgs/messages/example_json/lib/testarb_de.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[0,"de","8qk919",0,null,["Willkommen von ",["b","0"],["g","1"]],[6,"testde ",[3,0,["test new messages",["5","0"]],[2,"No new messages",4,"One new message",5,"Two new Messages"]]],"testdse is just a simple message"]
[0,"de","iagmc",0,null,["Willkommen von ",[11,0],[16,1]],["Willkommen von 2",[11,0],[16,1]],[6,"testde ",[3,0,["test new messages",[5,0]],[2,"No new messages",4,"One new message",5,"Two new Messages"]]],"testdse is just a simple message"]
8 changes: 6 additions & 2 deletions pkgs/messages/example_json/lib/testarbctx2.arb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"@@locale": "en",
"@@x-reference": true,
"@@context": "AboutPage",
"helloAndWelcome": "Welcome {firstName} von {lastName} <",
"@helloAndWelcome": {
"description": "Initial welcome message",
"placeholders": {
"firstName": {},
"lastName": {}
"lastName": {
"example": "Meyer",
"type": "int"
}
}
},
"aboutMessage": "About {websitename}",
Expand All @@ -17,6 +19,8 @@
"websitename": {}
}
},
"otherMsg": "other",
"@otherMsg": {},
"newMessages": "test {newMessages, plural, =0 {No new messages} =1 {One new message} two{Two new Messages} other {test {newMessages} new messages}}",
"@newMessages": {
"type": "text",
Expand Down
54 changes: 30 additions & 24 deletions pkgs/messages/example_json/lib/testarbctx2.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ class AboutPageMessages {
this.intlObject,
);

final String Function(String id) _fileLoader;
final Future<String> Function(String id) _fileLoader;

String _currentLocale = 'en';

final Map<String, MessageList> _messages = {};

final _carbs = {'fr': 'testarbctx2_fr.json', 'en': 'testarbctx2.json'};
static const carbs = {
'fr': 'lib/testarbctx2_fr.json',
'en': 'lib/testarbctx2.json'
};

final _messageListHashes = {
'testarbctx2_fr.json': '3nj3c2',
'testarbctx2.json': 'skm01b'
'lib/testarbctx2_fr.json': 'tjk424',
'lib/testarbctx2.json': 't7o6rv'
};

IntlObject intlObject;
Expand All @@ -27,15 +30,15 @@ class AboutPageMessages {

MessageList get _currentMessages => _messages[currentLocale]!;

Iterable<String> get knownLocales => _carbs.keys;
static Iterable<String> get knownLocales => carbs.keys;

void loadLocale(String locale) {
Future<void> loadLocale(String locale) async {
if (!_messages.containsKey(locale)) {
final carb = _carbs[locale];
final carb = carbs[locale];
if (carb == null) {
throw ArgumentError('Locale $locale is not in $knownLocales');
}
final data = _fileLoader(carb);
final data = await _fileLoader(carb);
final messageList = MessageListJson.fromString(data, intlObject);
if (messageList.preamble.hash != _messageListHashes[carb]) {
throw ArgumentError('''
Expand All @@ -52,32 +55,35 @@ class AboutPageMessages {
}
}

String helloAndWelcome({
required String firstName,
required String lastName,
}) =>
_currentMessages.generateStringAtIndex(
AboutPageMessagesEnum.helloAndWelcome.index, [firstName, lastName]);

String aboutMessage({required String websitename}) =>
String aboutMessage(String websitename) =>
_currentMessages.generateStringAtIndex(
AboutPageMessagesEnum.aboutMessage.index, [websitename]);

String newMessages({required int newMessages}) =>
String helloAndWelcome(
String firstName,
int lastName,
) =>
_currentMessages.generateStringAtIndex(
AboutPageMessagesEnum.newMessages.index, [newMessages]);
AboutPageMessagesEnum.helloAndWelcome.index, [firstName, lastName]);

String newMessages2({
required String gender,
required int newVar,
}) =>
String newMessages(int newMessages) => _currentMessages.generateStringAtIndex(
AboutPageMessagesEnum.newMessages.index, [newMessages]);

String newMessages2(
String gender,
int newVar,
) =>
_currentMessages.generateStringAtIndex(
AboutPageMessagesEnum.newMessages2.index, [gender, newVar]);

String get otherMsg => _currentMessages
.generateStringAtIndex(AboutPageMessagesEnum.otherMsg.index, []);
}

enum AboutPageMessagesEnum {
helloAndWelcome,
aboutMessage,
helloAndWelcome,
newMessages,
newMessages2
newMessages2,
otherMsg
}
2 changes: 1 addition & 1 deletion pkgs/messages/example_json/lib/testarbctx2.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[0,"en","skm01b",0,null,["Welcome von <",["8","0"],["d","1"]],["About ",["6","0"]],[6,"test ",[3,0,["test new messages",["5","0"]],[2,"No new messages",4,"One new message",5,"Two new Messages"]]],[6,"test ",[4,0,"Two new Messages",{"male":"No new messages","female":"One new message"}]]]
[0,"en","t7o6rv",0,null,["About ",[6,0]],["Welcome von <",[8,0],[13,1]],[6,"test ",[3,0,["test new messages",[5,0]],[2,"No new messages",4,"One new message",5,"Two new Messages"]]],[6,"test ",[4,0,"Two new Messages",{"male":"No new messages","female":"One new message"}]],"other"]
31 changes: 2 additions & 29 deletions pkgs/messages/example_json/lib/testarbctx2_fr.arb
Original file line number Diff line number Diff line change
@@ -1,35 +1,8 @@
{
"@@locale": "fr",
"@@context": "AboutPage",
"helloAndWelcome": "Welcome {firstName} von {lastName} <",
"@helloAndWelcome": {
"description": "Initial welcome message",
"placeholders": {
"firstName": {},
"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}}",
"@newMessages": {
"type": "text",
"description": "Number of new messages in inbox.",
"placeholders": {
"newMessages": {
"type": "int"
}
}
},
"newMessages2": "test {gender, select,male {No new messages} female {One new message} other{Two new Messages} other {test {gender} new messages of type {newVar}}}",
"@newMessages2": {
"type": "text",
"placeholders": {
"gender": {
"type": "String"
},
"newVar": {
"type": "int"
}
}
}
"newMessages2": "test {gender, select,male {No new messages} female {One new message} other{Two new Messages} other {test {gender} new messages of type {newVar}}}"
}
2 changes: 1 addition & 1 deletion pkgs/messages/example_json/lib/testarbctx2_fr.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[0,"fr","3nj3c2",0,null,["Welcome von <",["8","0"],["d","1"]],["Sur ",["4","0"]],[6,"test ",[3,0,["test new messages",["5","0"]],[2,"No new messages",4,"One new message",5,"Two new Messages"]]],[6,"test ",[4,0,"Two new Messages",{"male":"No new messages","female":"One new message"}]]]
[0,"fr","vblv7q",0,null,["Sur ",[4,0]],["Welcome von <",[8,0],[13,1]],[6,"test ",[3,0,["test new messages",[5,0]],[2,"No new messages",4,"One new message",5,"Two new Messages"]]],[6,"test ",[4,0,"Two new Messages",{"male":"No new messages","female":"One new message"}]],"other"]
2 changes: 2 additions & 0 deletions pkgs/messages/lib/src/intl_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import 'message.dart';

abstract class IntlObject {
const IntlObject();

Message gender(
Gender gender,
Message? female,
Expand Down
1 change: 1 addition & 0 deletions pkgs/messages/lib/src/old_intl_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'intl_object.dart';
import 'message.dart';

class OldIntlObject extends IntlObject {
const OldIntlObject();
@override
Message gender(
Gender gender,
Expand Down
2 changes: 1 addition & 1 deletion pkgs/messages/test/messagelist_json_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void main() {
0,
)
],
OldIntlObject(),
const OldIntlObject(),
null,
);

Expand Down
4 changes: 2 additions & 2 deletions pkgs/messages_builder/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
builders:
copyBuilder:
import: "package:messages_builder/builder.dart"
builder_factories: ["carbBuilder"]
build_extensions: {'.arb': ['.carb','.carb.dart', '.g.dart', '.json'], '^pubspec.yaml': []}
builder_factories: ["messagesBuilder"]
build_extensions: {'.arb': ['.g.dart', '.json'], '^pubspec.yaml': []}
build_to: source
auto_apply: root_package
Loading

0 comments on commit 36f1d62

Please sign in to comment.