diff --git a/pkgs/messages/lib/src/deserializer/deserializer.dart b/pkgs/messages/lib/src/deserializer/deserializer.dart index 3441f44e..1bc61209 100644 --- a/pkgs/messages/lib/src/deserializer/deserializer.dart +++ b/pkgs/messages/lib/src/deserializer/deserializer.dart @@ -6,5 +6,5 @@ import '../message_format.dart'; import '../plural_selector.dart'; abstract class Deserializer { - T deserialize(PluralSelector intl); + T deserialize(PluralSelector selector); } diff --git a/pkgs/messages/lib/src/deserializer/deserializer_json.dart b/pkgs/messages/lib/src/deserializer/deserializer_json.dart index 27c0c7f6..98a56edf 100644 --- a/pkgs/messages/lib/src/deserializer/deserializer_json.dart +++ b/pkgs/messages/lib/src/deserializer/deserializer_json.dart @@ -20,7 +20,7 @@ class JsonDeserializer extends Deserializer { } @override - MessageListJson deserialize(PluralSelector intl) { + MessageListJson deserialize(PluralSelector selector) { if (preamble.version != serializationVersion) { throw ArgumentError( '''This message has version ${preamble.version}, while the deserializer has version $serializationVersion'''); @@ -32,7 +32,7 @@ class JsonDeserializer extends Deserializer { return MessageListJson( preamble, _messages, - intl, + selector, mapping?.map((key, value) => MapEntry( int.parse(key, radix: serializationRadix), int.parse(value as String, radix: serializationRadix), @@ -89,15 +89,16 @@ class JsonDeserializer extends Deserializer { for (var i = 0; i < submessages.length - 1; i += 2) { final msg = getMessage(submessages[i + 1]); final messageMarker = submessages[i]; - if (messageMarker case Plural.few) { + if (messageMarker case PluralMarker.few) { fewMessage = msg; - } else if (messageMarker case Plural.many) { + } else if (messageMarker case PluralMarker.many) { manyMessage = msg; - } else if (messageMarker case final int d) { - numberCases[d] = msg; + } else if (messageMarker case final int digit) { + numberCases[digit] = msg; } else if (messageMarker is String && - messageMarker.startsWith(Plural.wordCase)) { - wordCases[int.parse(messageMarker.substring(1))] = msg; + messageMarker.startsWith(PluralMarker.wordCase)) { + final digit = int.parse(messageMarker.substring(1)); + wordCases[digit] = msg; } } return PluralMessage( diff --git a/pkgs/messages/lib/src/message_format.dart b/pkgs/messages/lib/src/message_format.dart index 611d6d89..909d7a42 100644 --- a/pkgs/messages/lib/src/message_format.dart +++ b/pkgs/messages/lib/src/message_format.dart @@ -30,31 +30,15 @@ abstract class Preamble { abstract class MessageList { Preamble get preamble; - PluralSelector get intl; + PluralSelector get pluralSelector; String generateStringAtIndex(int index, List args); String generateStringAtId(String id, List args); } -enum PluralEnum { - zeroWord, - zeroNumber, - oneWord, - oneNumber, - twoWord, - twoNumber, - few, - many, -} - -class Plural { +sealed class PluralMarker { static const String wordCase = 'w'; static const String few = 'f'; static const String many = 'm'; } - -class Gender { - static const int female = 1; - static const int male = 2; -} diff --git a/pkgs/messages/lib/src/message_list_json.dart b/pkgs/messages/lib/src/message_list_json.dart index 081e9c60..bb0eb1c4 100644 --- a/pkgs/messages/lib/src/message_list_json.dart +++ b/pkgs/messages/lib/src/message_list_json.dart @@ -40,12 +40,12 @@ class JsonPreamble extends Preamble { class MessageListJson extends MessageList { final List messages; - final PluralSelector _intl; + final PluralSelector _selector; final JsonPreamble _preamble; final Map? messageIndices; @override - PluralSelector get intl => _intl; + PluralSelector get pluralSelector => _selector; @override Preamble get preamble => _preamble; @@ -53,7 +53,7 @@ class MessageListJson extends MessageList { MessageListJson( this._preamble, this.messages, - this._intl, + this._selector, this.messageIndices, ); @@ -64,11 +64,11 @@ class MessageListJson extends MessageList { String generateStringAtId(String id, List args) => messages .where((element) => element.id == id) .first - .generateString(args, pluralSelector: _intl); + .generateString(args, pluralSelector: _selector); @override String generateStringAtIndex(int index, List args) => - messages[getIndex(index)].generateString(args, pluralSelector: _intl); + messages[getIndex(index)].generateString(args, pluralSelector: _selector); int getIndex(int index) => messageIndices?[index] ?? index; } diff --git a/pkgs/messages_builder/lib/generation_options.dart b/pkgs/messages_builder/lib/generation_options.dart index 4267b695..6573b3fe 100644 --- a/pkgs/messages_builder/lib/generation_options.dart +++ b/pkgs/messages_builder/lib/generation_options.dart @@ -36,9 +36,10 @@ class GenerationOptions { /// dart native code. final DeserializationType deserialization; - /// + /// The header to add to all generated files, for example for licensing. final String header; + /// The origin of the algorithm for determining which plural case to use. final PluralSelectorType pluralSelector; GenerationOptions({ @@ -62,21 +63,31 @@ class GenerationOptions { deserialization: DeserializationType.web, messageCalls: (messagesOptions?['generateMethods'] as bool?) ?? true, findById: (messagesOptions?['generateFindById'] as bool?) ?? false, - indexType: IndexType.values - .where((type) => - type.name == messagesOptions?['generateFindBy'] as String?) - .firstOrNull ?? - IndexType.integer, + indexType: _indexType(messagesOptions), header: messagesOptions?['header'] as String? ?? 'Generated by package:messages_builder.', - pluralSelector: PluralSelectorType.values - .where((type) => - type.name == messagesOptions?['pluralSelector'] as String?) - .firstOrNull ?? - PluralSelectorType.intl, + pluralSelector: _pluralSelector(messagesOptions), ); return generationOptions; } + + static IndexType _indexType(YamlMap? messagesOptions) { + final generateFindString = messagesOptions?['generateFindBy'] as String?; + return generateFindString != null + ? IndexType.values + .where((type) => type.name == generateFindString) + .first + : IndexType.integer; + } + + static PluralSelectorType _pluralSelector(YamlMap? messagesOptions) { + final pluralSelectorString = messagesOptions?['pluralSelector'] as String?; + return pluralSelectorString != null + ? PluralSelectorType.values + .where((type) => type.name == pluralSelectorString) + .first + : PluralSelectorType.intl; + } } enum SerializationType { @@ -87,14 +98,27 @@ enum DeserializationType { web; } +/// How the indexing of the messages should be implemented. enum IndexType { + /// No indexing. none, + + /// Indexing via a collection of `static int`s. integer, + + /// Indexing via an enum. enumerate; } +/// The origin of the algorithm for determining which plural case to use. enum PluralSelectorType { + /// From `package:intl`. intl, + + /// From `package:intl4x`. + intl4x, + + /// A user-specified algorithm. custom; } diff --git a/pkgs/messages_serializer/lib/src/serializer_json.dart b/pkgs/messages_serializer/lib/src/serializer_json.dart index 0471e717..a1bb1a5a 100644 --- a/pkgs/messages_serializer/lib/src/serializer_json.dart +++ b/pkgs/messages_serializer/lib/src/serializer_json.dart @@ -134,19 +134,20 @@ class JsonSerializer extends Serializer { m.add(encodeMessage(message.other)); final caseIndices = []; if (message.few != null) { - caseIndices.add(Plural.few); + caseIndices.add(PluralMarker.few); caseIndices.add(encodeMessage(message.few!)); } if (message.many != null) { - caseIndices.add(Plural.many); + caseIndices.add(PluralMarker.many); caseIndices.add(encodeMessage(message.many!)); } - for (final entry in message.numberCases.entries) { - caseIndices.add(entry.key); - caseIndices.add(encodeMessage(entry.value)); + for (final MapEntry(key: caseIndex, value: messageIndex) + in message.numberCases.entries) { + caseIndices.add(caseIndex); + caseIndices.add(encodeMessage(messageIndex)); } for (final entry in message.wordCases.entries) { - caseIndices.add(Plural.wordCase + entry.key.toString()); + caseIndices.add(PluralMarker.wordCase + entry.key.toString()); caseIndices.add(encodeMessage(entry.value)); } m.add(caseIndices); diff --git a/pkgs/messages_shrinker/lib/messages_shrinker.dart b/pkgs/messages_shrinker/lib/messages_shrinker.dart index e465a3e6..2099b3e6 100644 --- a/pkgs/messages_shrinker/lib/messages_shrinker.dart +++ b/pkgs/messages_shrinker/lib/messages_shrinker.dart @@ -34,7 +34,8 @@ class MessageShrinker { final sizeBefore = buffer.length; final json = JsonDeserializer(buffer).deserialize( (howMany, {few, locale, many, numberCases, required other, wordCases}) { - throw UnimplementedError(); + throw StateError('As the deserialized MessageList is not used, but ' + 'just immediately reserialized, this selector will not be called.'); }, ); final data = JsonSerializer(json.preamble.hasIds)