Skip to content

Commit

Permalink
Updates to messages
Browse files Browse the repository at this point in the history
  • Loading branch information
mosuem committed Dec 15, 2023
1 parent c84b9a6 commit 11510bf
Show file tree
Hide file tree
Showing 35 changed files with 398 additions and 413 deletions.
1 change: 1 addition & 0 deletions pkgs/intl4x/lib/intl4x.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import 'src/plural_rules/plural_rules_impl.dart';
import 'src/plural_rules/plural_rules_options.dart';

export 'src/locale/locale.dart';
export 'src/plural_rules/plural_rules.dart';

typedef Icu4xKey = String;

Expand Down
7 changes: 2 additions & 5 deletions pkgs/messages/example_json/bin/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
import 'dart:io';

import 'package:example_json/testarbctx2.g.dart';
import 'package:messages/package_intl_object.dart';

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

await messages.loadLocale('en');
Expand Down
34 changes: 25 additions & 9 deletions pkgs/messages/example_json/lib/testarb.g.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Generated by package:messages_builder
// Generated by package:messages_builder.

import 'package:intl4x/intl4x.dart';
import 'package:messages/messages_json.dart';

class HomePageMessages {
HomePageMessages(
this._fileLoader,
this.intlObject,
);
HomePageMessages(this._fileLoader);

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

Expand All @@ -19,8 +17,6 @@ class HomePageMessages {
'en': ('lib/testarb.json', 'dr9Md951')
};

IntlObject intlObject;

String get currentLocale => _currentLocale;

MessageList get _currentMessages => _messages[currentLocale]!;
Expand All @@ -35,7 +31,7 @@ class HomePageMessages {
throw ArgumentError('Locale $locale is not in $knownLocales');
}
final data = await _fileLoader(carb);
final messageList = MessageListJson.fromString(data, intlObject);
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}".''');
Expand All @@ -46,11 +42,31 @@ class HomePageMessages {
}

void loadAllLocales() {
for (var locale in knownLocales) {
for (final locale in knownLocales) {
loadLocale(locale);
}
}

Message pluralSelector(
num howMany, {
required Message other,
Message? few,
Message? many,
Map<int, Message>? numberCases,
Map<int, Message>? wordCases,
}) {
Message getCase(int i) => numberCases?[i] ?? wordCases?[i] ?? other;
return switch (
Intl(locale: Locale.parse(currentLocale)).plural().select(howMany)) {
PluralCategory.zero => getCase(0),
PluralCategory.one => getCase(1),
PluralCategory.two => getCase(2),
PluralCategory.few => few ?? other,
PluralCategory.many => many ?? other,
PluralCategory.other => other,
};
}

String helloAndWelcome(
String firstName,
String lastName,
Expand Down
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","dr9Md951",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"}]]]
[0,"en","dr9Md951",0,null,["Welcome von !",[8,0],[13,1]],["Welcome von !",[8,0],[13,1]],[6,"test ",[3,0,["test new messages",[5,0]],[0,"No new messages",1,"One new message","w2","Two new Messages"]]],[6,"test ",[4,0,"Two new Messages",{"male":"No new messages","female":"One new 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","hbDN1MhX",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"]
[0,"de","hbDN1MhX",0,null,["Willkommen von ",[11,0],[16,1]],["Willkommen von 2",[11,0],[16,1]],[6,"testde ",[3,0,["test new messages",[5,0]],[0,"No new messages",1,"One new message","w2","Two new Messages"]]],"testdse is just a simple message"]
34 changes: 25 additions & 9 deletions pkgs/messages/example_json/lib/testarbctx2.g.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Generated by package:messages_builder
// Generated by package:messages_builder.

import 'package:intl4x/intl4x.dart';
import 'package:messages/messages_json.dart';

class AboutPageMessages {
AboutPageMessages(
this._fileLoader,
this.intlObject,
);
AboutPageMessages(this._fileLoader);

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

Expand All @@ -19,8 +17,6 @@ class AboutPageMessages {
'en': ('lib/testarbctx2.json', 'QrwRSsOy')
};

IntlObject intlObject;

String get currentLocale => _currentLocale;

MessageList get _currentMessages => _messages[currentLocale]!;
Expand All @@ -35,7 +31,7 @@ class AboutPageMessages {
throw ArgumentError('Locale $locale is not in $knownLocales');
}
final data = await _fileLoader(carb);
final messageList = MessageListJson.fromString(data, intlObject);
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}".''');
Expand All @@ -46,11 +42,31 @@ class AboutPageMessages {
}

void loadAllLocales() {
for (var locale in knownLocales) {
for (final locale in knownLocales) {
loadLocale(locale);
}
}

Message pluralSelector(
num howMany, {
required Message other,
Message? few,
Message? many,
Map<int, Message>? numberCases,
Map<int, Message>? wordCases,
}) {
Message getCase(int i) => numberCases?[i] ?? wordCases?[i] ?? other;
return switch (
Intl(locale: Locale.parse(currentLocale)).plural().select(howMany)) {
PluralCategory.zero => getCase(0),
PluralCategory.one => getCase(1),
PluralCategory.two => getCase(2),
PluralCategory.few => few ?? other,
PluralCategory.many => many ?? other,
PluralCategory.other => other,
};
}

String aboutMessage(String websitename) =>
_currentMessages.generateStringAtIndex(0, [websitename]);

Expand Down
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","QrwRSsOy",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"]
[0,"en","QrwRSsOy",0,null,["About ",[6,0]],["Welcome von <",[8,0],[13,1]],[6,"test ",[3,0,["test new messages",[5,0]],[0,"No new messages",1,"One new message","w2","Two new Messages"]]],[6,"test ",[4,0,"Two new Messages",{"male":"No new messages","female":"One new message"}]],"other"]
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","EyPjEJJU",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"]
[0,"fr","EyPjEJJU",0,null,["Sur ",[4,0]],["Welcome von <",[8,0],[13,1]],[6,"test ",[3,0,["test new messages",[5,0]],[0,"No new messages",1,"One new message","w2","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/example_json/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ environment:
sdk: ^3.0.0

dependencies:
intl4x: ^0.7.0
messages:
path: ../

Expand All @@ -25,3 +26,4 @@ package_options:
generateMethods: true
generateFindById: false
generateFindBy: integer
pluralSelector: intl4x
2 changes: 2 additions & 0 deletions pkgs/messages/example_json/pubspec_overrides.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ dependency_overrides:
path: ../
messages_serializer:
path: ../../messages_serializer
intl4x:
path: ../../intl4x
2 changes: 0 additions & 2 deletions pkgs/messages/lib/messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
// BSD-style license that can be found in the LICENSE file.

export 'src/deserializer/deserializer.dart';
export 'src/intl_object.dart' show IntlObject;
export 'src/intl_style_lookup.dart' show Intl;
export 'src/message_format.dart';
4 changes: 2 additions & 2 deletions pkgs/messages/lib/src/deserializer/deserializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// 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 '../intl_object.dart';
import '../message_format.dart';
import '../plural_selector.dart';

abstract class Deserializer<T extends MessageList> {
T deserialize(IntlObject intl);
T deserialize(PluralSelector intl);
}
83 changes: 16 additions & 67 deletions pkgs/messages/lib/src/deserializer/deserializer_json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import 'dart:convert';

import '../intl_object.dart';
import '../message_format.dart';
import '../message_list_json.dart';
import '../plural_selector.dart';
import 'deserializer.dart';

class JsonDeserializer extends Deserializer<MessageListJson> {
Expand All @@ -20,7 +20,7 @@ class JsonDeserializer extends Deserializer<MessageListJson> {
}

@override
MessageListJson deserialize(IntlObject intl) {
MessageListJson deserialize(PluralSelector intl) {
if (preamble.version != serializationVersion) {
throw ArgumentError(
'''This message has version ${preamble.version}, while the deserializer has version $serializationVersion''');
Expand Down Expand Up @@ -55,8 +55,6 @@ class JsonDeserializer extends Deserializer<MessageListJson> {
return _forPlural(message, start, id);
} else if (typeOrId == SelectMessage.type) {
return _forSelect(message, start, id);
} else if (typeOrId == GenderMessage.type) {
return _forGender(message, start, id);
} else if (typeOrId == CombinedMessage.type) {
return _forCombined(message, start, id);
} else if (typeOrId is String) {
Expand All @@ -83,51 +81,28 @@ class JsonDeserializer extends Deserializer<MessageListJson> {
PluralMessage _forPlural(List<dynamic> message, int start, String? id) {
final argIndex = message[start] as int;
final otherMessage = getMessage(message[start + 1]);
Message? zeroWordMessage;
Message? zeroNumberMessage;
Message? oneWordMessage;
Message? oneNumberMessage;
Message? twoWordMessage;
Message? twoNumberMessage;
Message? fewMessage;
Message? manyMessage;
final numberCases = <int, Message>{};
final wordCases = <int, Message>{};
final submessages = message[start + 2] as List;
for (var i = 0; i < submessages.length - 1; i += 2) {
final msg = getMessage(submessages[i + 1]);
switch (submessages[i]) {
case Plural.zeroWord:
zeroWordMessage = msg;
break;
case Plural.zeroNumber:
zeroNumberMessage = msg;
break;
case Plural.oneWord:
oneWordMessage = msg;
break;
case Plural.oneNumber:
oneNumberMessage = msg;
break;
case Plural.twoWord:
twoWordMessage = msg;
break;
case Plural.twoNumber:
twoNumberMessage = msg;
break;
case Plural.few:
fewMessage = msg;
break;
case Plural.many:
manyMessage = msg;
break;
final messageMarker = submessages[i];
if (messageMarker case Plural.few) {
fewMessage = msg;
} else if (messageMarker case Plural.many) {
manyMessage = msg;
} else if (messageMarker case final int d) {
numberCases[d] = msg;
} else if (messageMarker is String &&
messageMarker.startsWith(Plural.wordCase)) {
wordCases[int.parse(messageMarker.substring(1))] = msg;
}
}
return PluralMessage(
zeroNumber: zeroNumberMessage,
zeroWord: zeroWordMessage,
oneNumber: oneNumberMessage,
oneWord: oneWordMessage,
twoNumber: twoNumberMessage,
twoWord: twoWordMessage,
numberCases: numberCases,
wordCases: wordCases,
few: fewMessage,
many: manyMessage,
argIndex: argIndex,
Expand All @@ -153,30 +128,4 @@ class JsonDeserializer extends Deserializer<MessageListJson> {
message.skip(start).map(getMessage).toList(),
);
}

GenderMessage _forGender(List<dynamic> message, int start, String? id) {
final argIndex = message[start] as int;
final otherMessage = getMessage(message[start + 1]);
final submessages = message[start + 2] as List;
Message? femaleMessage;
Message? maleMessage;
for (var i = 0; i < submessages.length - 1; i += 2) {
final msg = getMessage(submessages[i + 1]);
switch (submessages[i]) {
case Gender.female:
femaleMessage = msg;
break;
case Gender.male:
maleMessage = msg;
break;
}
}
return GenderMessage(
female: femaleMessage,
male: maleMessage,
other: otherMessage,
argIndex: argIndex,
id: id,
);
}
}
29 changes: 0 additions & 29 deletions pkgs/messages/lib/src/intl_object.dart

This file was deleted.

23 changes: 0 additions & 23 deletions pkgs/messages/lib/src/intl_style_lookup.dart

This file was deleted.

Loading

0 comments on commit 11510bf

Please sign in to comment.