Skip to content

Commit

Permalink
Updates to messages (#762)
Browse files Browse the repository at this point in the history
* Updates to messages

* Format

* Change carbs name

* Regenerate example

* Rev versions

* Changes as per review

* Remove unused file
  • Loading branch information
mosuem authored Jan 8, 2024
1 parent 893a92c commit 8cde33a
Show file tree
Hide file tree
Showing 42 changed files with 452 additions and 455 deletions.
4 changes: 4 additions & 0 deletions pkgs/intl4x/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.7.1

- Export plural rules.

## 0.7.0

- Add conformance testing workflow.
Expand Down
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
2 changes: 1 addition & 1 deletion pkgs/intl4x/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: intl4x
description: >-
A lightweight modular library for internationalization (i18n) functionality.
version: 0.7.0
version: 0.7.1
repository: https://github.com/dart-lang/i18n/tree/main/pkgs/intl4x
platforms: ## TODO: Add native platforms once ICU4X is integrated.
web:
Expand Down
5 changes: 5 additions & 0 deletions pkgs/messages/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.2.0

- Remove `IntlObject` interface.
- Introduce `PluralRules` to retrieve the correct message for plurals.

## 0.1.1

- Update README.
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
40 changes: 28 additions & 12 deletions pkgs/messages/example_json/lib/testarb.g.dart
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
// 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;

String _currentLocale = 'en';

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

static const carbs = {
static const _dataFiles = {
'de': ('lib/testarb_de.json', 'hbDN1MhX'),
'en': ('lib/testarb.json', 'dr9Md951')
};

IntlObject intlObject;

String get currentLocale => _currentLocale;

MessageList get _currentMessages => _messages[currentLocale]!;

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

Future<void> loadLocale(String locale) async {
if (!_messages.containsKey(locale)) {
final info = carbs[locale];
final info = _dataFiles[locale];
final carb = info?.$1;
if (carb == null) {
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"]
40 changes: 28 additions & 12 deletions pkgs/messages/example_json/lib/testarbctx2.g.dart
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
// 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;

String _currentLocale = 'en';

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

static const carbs = {
static const _dataFiles = {
'fr': ('lib/testarbctx2_fr.json', 'EyPjEJJU'),
'en': ('lib/testarbctx2.json', 'QrwRSsOy')
};

IntlObject intlObject;

String get currentLocale => _currentLocale;

MessageList get _currentMessages => _messages[currentLocale]!;

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

Future<void> loadLocale(String locale) async {
if (!_messages.containsKey(locale)) {
final info = carbs[locale];
final info = _dataFiles[locale];
final carb = info?.$1;
if (carb == null) {
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 selector);
}
Loading

0 comments on commit 8cde33a

Please sign in to comment.