Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resource identifiers to package:messages #844

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkgs/messages/example_json/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

# Conventional directory for build output.
build/
bin/example/
8 changes: 3 additions & 5 deletions pkgs/messages/example_json/bin/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

// ignore_for_file: prefer_function_declarations_over_variables

import 'dart:io';

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

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

await messages.loadLocale('en');
Expand All @@ -26,6 +23,7 @@ Future<void> main(List<String> arguments) async {
await messages.loadLocale('fr');
print('AboutMessage fr:');
print('\t${messages.aboutMessage('typesafe.fr')}');
print('\t${messages.helloAndWelcome('henry', 5)}');

///To enable this, add `generateFindById: true` to the pubspec section
// print('\t${messages.getById('aboutMessage', ['get-by-id.fr'])}');
Expand Down
22 changes: 22 additions & 0 deletions pkgs/messages/example_json/hook/build.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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 'package:messages_builder/hook.dart';
import 'package:native_assets_cli/native_assets_cli.dart';

void main(List<String> args) {
build(args, (config, output) async {
// final builder = MessagesDataBuilder.fromFiles(
// [
// 'assets/l10n/testarb.arb',
// 'assets/l10n/testarb_de.arb',
// 'assets/l10n/testarbctx2.arb',
// 'assets/l10n/testarbctx2_fr.arb',
// ],
// );
final builder = MessagesDataBuilder.fromFolder('assets/l10n/');

await builder.run(config: config, output: output, logger: null);
});
}
Empty file.
172 changes: 172 additions & 0 deletions pkgs/messages/example_json/lib/messages.g.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
// Generated by package:messages_builder.

import 'dart:ffi';

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

class AboutPageMessages {
AboutPageMessages();

String _currentLocale = 'en';

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

static const _dataFiles = {
'fr': ('package:example_json/assets/l10n/testarbctx2_fr.json', 'EyPjEJJU'),
'en': ('package:example_json/assets/l10n/testarbctx2.json', 'QrwRSsOy')
};

String get currentLocale => _currentLocale;

MessageList get _currentMessages => _messages[currentLocale]!;

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

Future<void> loadLocale(String locale) async {
if (!_messages.containsKey(locale)) {
final info = _dataFiles[locale];
final carb = info?.$1;
if (carb == null) {
throw ArgumentError('Locale $locale is not in $knownLocales');
}
final data = await AssetBundle.loadString(carb);
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;
}

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

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

String helloAndWelcome(
String firstName,
int lastName,
) =>
generateStringAtIndex(1, [firstName, lastName]);

String newMessages(int newMessages) =>
generateStringAtIndex(2, [newMessages]);

String newMessages2(
String gender,
int newVar,
) =>
generateStringAtIndex(3, [gender, newVar]);

String get otherMsg => generateStringAtIndex(4, []);
}

extension on AboutPageMessages {
@ResourceIdentifier('AboutPageMessages')
String generateStringAtIndex(
int index,
List args,
) =>
_currentMessages.generateStringAtIndex(index, args);
}

class HomePageMessages {
HomePageMessages();

String _currentLocale = 'en';

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

static const _dataFiles = {
'de': ('package:example_json/assets/l10n/testarb_de.json', 'hbDN1MhX'),
'en': ('package:example_json/assets/l10n/testarb.json', 'dr9Md951')
};

String get currentLocale => _currentLocale;

MessageList get _currentMessages => _messages[currentLocale]!;

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

Future<void> loadLocale(String locale) async {
if (!_messages.containsKey(locale)) {
final info = _dataFiles[locale];
final carb = info?.$1;
if (carb == null) {
throw ArgumentError('Locale $locale is not in $knownLocales');
}
final data = await AssetBundle.loadString(carb);
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;
}

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

String helloAndWelcome(
String firstName,
String lastName,
) =>
generateStringAtIndex(0, [firstName, lastName]);

String helloAndWelcome2(
String firstName,
String lastName,
) =>
generateStringAtIndex(1, [firstName, lastName]);

String newMessages(int newMessages) =>
generateStringAtIndex(2, [newMessages]);

String newMessages2(
String gender,
int newVar,
) =>
generateStringAtIndex(3, [gender, newVar]);
}

extension on HomePageMessages {
@ResourceIdentifier('HomePageMessages')
String generateStringAtIndex(
int index,
List args,
) =>
_currentMessages.generateStringAtIndex(index, args);
}

Message _pluralSelector(
num howMany,
String locale, {
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(locale)).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,
};
}
90 changes: 0 additions & 90 deletions pkgs/messages/example_json/lib/testarb.g.dart

This file was deleted.

1 change: 0 additions & 1 deletion pkgs/messages/example_json/lib/testarb.json

This file was deleted.

1 change: 0 additions & 1 deletion pkgs/messages/example_json/lib/testarb_de.json

This file was deleted.

Loading
Loading