Skip to content

Commit

Permalink
Squashed commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mosuem committed Jul 29, 2024
1 parent 8d63e87 commit 1dc22c0
Show file tree
Hide file tree
Showing 39 changed files with 554 additions and 468 deletions.
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/
File renamed without changes.
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 @@ -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 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.
153 changes: 153 additions & 0 deletions pkgs/messages/example_json/lib/messages.g.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Generated by package:messages_builder.

import 'dart:ffi';

import 'package:intl4x/intl4x.dart';
import 'package:messages/messages_json.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) =>
_currentMessages.generateStringAtIndex(0, [websitename]);

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

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

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

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

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,
) =>
_currentMessages.generateStringAtIndex(0, [firstName, lastName]);

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

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

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

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.

89 changes: 0 additions & 89 deletions pkgs/messages/example_json/lib/testarbctx2.g.dart

This file was deleted.

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

This file was deleted.

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

This file was deleted.

Loading

0 comments on commit 1dc22c0

Please sign in to comment.