Skip to content

Commit

Permalink
Refactorings!
Browse files Browse the repository at this point in the history
  • Loading branch information
mosuem committed Nov 5, 2024
1 parent 6ba3fd2 commit 63d1b55
Show file tree
Hide file tree
Showing 32 changed files with 356 additions and 348 deletions.
2 changes: 1 addition & 1 deletion pkgs/messages/example/bin/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import 'dart:io';

import 'package:example/messages.g.dart';
import 'package:example/AboutPage_messages.g.dart';

Future<void> main(List<String> arguments) async {
final messages = AboutPageMessages(
Expand Down
1 change: 1 addition & 0 deletions pkgs/messages/example/lib/AboutPage_en_empty.g.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// This is a helper file for deferred loading of the messages for locale en, generated by `dart run messages`.
1 change: 1 addition & 0 deletions pkgs/messages/example/lib/AboutPage_fr_empty.g.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// This is a helper file for deferred loading of the messages for locale fr, generated by `dart run messages`.
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,8 @@
import 'package:intl/intl.dart';
import 'package:messages/messages_json.dart';

Message _pluralSelector(
num howMany,
String locale, {
required Message other,
Message? few,
Message? many,
Map<int, Message>? numberCases,
Map<int, Message>? wordCases,
}) {
return Intl.pluralLogic(
howMany,
few: few,
many: many,
zero: numberCases?[0] ?? wordCases?[0],
one: numberCases?[1] ?? wordCases?[1],
two: numberCases?[2] ?? wordCases?[2],
other: other,
locale: locale,
);
}
import 'AboutPage_en_empty.g.dart' deferred as AboutPage_en_empty;
import 'AboutPage_fr_empty.g.dart' deferred as AboutPage_fr_empty;

class AboutPageMessages {
AboutPageMessages(this._assetLoader);
Expand Down Expand Up @@ -60,6 +42,12 @@ class AboutPageMessages {
if (dataFile == null) {
throw ArgumentError('Locale $locale is not in $knownLocales');
}
if (locale == 'en') {
await AboutPage_en_empty.loadLibrary();
} else if (locale == 'fr') {
await AboutPage_fr_empty.loadLibrary();
}

final data = await _assetLoader(dataFile);
final messageList = MessageListJson.fromString(data, _pluralSelector);
if (messageList.preamble.hash != info?.$2) {
Expand Down Expand Up @@ -98,75 +86,23 @@ class AboutPageMessages {
String get otherMsg => _currentMessages.generateStringAtIndex(4, []);
}

class HomePageMessages {
HomePageMessages(this._assetLoader);

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

String _currentLocale = 'en';

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

static const _dataFiles = {
'de': ('packages/example/assets/testarb_de.arb.json', 'hbDN1MhX'),
'en': ('packages/example/assets/testarb.arb.json', 'dr9Md951')
};

String get currentLocale => _currentLocale;

MessageList get _currentMessages => _messages[currentLocale]!;

String getById(
String id, [
List<dynamic> args = const [],
]) {
return _currentMessages.generateStringAtId(id, args);
}

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

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

Future<void> loadAllLocales() async {
for (final locale in knownLocales) {
await 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,
}) {
return Intl.pluralLogic(
howMany,
few: few,
many: many,
zero: numberCases?[0] ?? wordCases?[0],
one: numberCases?[1] ?? wordCases?[1],
two: numberCases?[2] ?? wordCases?[2],
other: other,
locale: locale,
);
}
1 change: 1 addition & 0 deletions pkgs/messages/example/lib/HomePage_de_empty.g.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// This is a helper file for deferred loading of the messages for locale de, generated by `dart run messages`.
1 change: 1 addition & 0 deletions pkgs/messages/example/lib/HomePage_en_empty.g.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// This is a helper file for deferred loading of the messages for locale en, generated by `dart run messages`.
109 changes: 109 additions & 0 deletions pkgs/messages/example/lib/HomePage_messages.g.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Generated by package:messages_builder.

// ignore_for_file: non_constant_identifier_names

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

import 'HomePage_de_empty.g.dart' deferred as HomePage_de_empty;
import 'HomePage_en_empty.g.dart' deferred as HomePage_en_empty;

class HomePageMessages {
HomePageMessages(this._assetLoader);

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

String _currentLocale = 'en';

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

static const _dataFiles = {
'de': ('packages/example/assets/testarb_de.arb.json', 'hbDN1MhX'),
'en': ('packages/example/assets/testarb.arb.json', 'dr9Md951')
};

String get currentLocale => _currentLocale;

MessageList get _currentMessages => _messages[currentLocale]!;

String getById(
String id, [
List<dynamic> args = const [],
]) {
return _currentMessages.generateStringAtId(id, args);
}

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

Future<void> loadLocale(String locale) async {
if (!_messages.containsKey(locale)) {
final info = _dataFiles[locale];
final dataFile = info?.$1;
if (dataFile == null) {
throw ArgumentError('Locale $locale is not in $knownLocales');
}
if (locale == 'de') {
await HomePage_de_empty.loadLibrary();
} else if (locale == 'en') {
await HomePage_en_empty.loadLibrary();
}

final data = await _assetLoader(dataFile);
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;
}

Future<void> loadAllLocales() async {
for (final locale in knownLocales) {
await 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,
}) {
return Intl.pluralLogic(
howMany,
few: few,
many: many,
zero: numberCases?[0] ?? wordCases?[0],
one: numberCases?[1] ?? wordCases?[1],
two: numberCases?[2] ?? wordCases?[2],
other: other,
locale: locale,
);
}
2 changes: 1 addition & 1 deletion pkgs/messages/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ package_options:
plural_selector: intl
arb_input_folder: assets/l10n/
message_output_folder: assets/
generated_code_file: lib/messages.g.dart
generated_code_files: lib/
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"placeholders": {
"arg": {
"type":"String",
"example":"1"
"example":"winter"
}
}
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,8 @@
import 'package:intl/intl.dart';
import 'package:messages/messages_json.dart';

import 'empty_en.dart' deferred as empty_en;
import 'empty_de.dart' deferred as empty_de;

Message _pluralSelector(
num howMany,
String locale, {
required Message other,
Message? few,
Message? many,
Map<int, Message>? numberCases,
Map<int, Message>? wordCases,
}) {
return Intl.pluralLogic(
howMany,
few: few,
many: many,
zero: numberCases?[0] ?? wordCases?[0],
one: numberCases?[1] ?? wordCases?[1],
two: numberCases?[2] ?? wordCases?[2],
other: other,
locale: locale,
);
}
import 'my_app_de_DE_empty.g.dart' deferred as my_app_de_DE_empty;
import 'my_app_en_US_empty.g.dart' deferred as my_app_en_US_empty;

class MyAppMessages {
MyAppMessages(this._assetLoader);
Expand All @@ -39,6 +18,10 @@ class MyAppMessages {
final Map<String, MessageList> _messages = {};

static const _dataFiles = {
'de_DE': (
'packages/my_application/assets/messages_de.arb.json',
'xXViKglj'
),
'en_US': ('packages/my_application/assets/messages.arb.json', 'IT21w/eV')
};

Expand All @@ -55,10 +38,10 @@ class MyAppMessages {
if (dataFile == null) {
throw ArgumentError('Locale $locale is not in $knownLocales');
}
if (locale.startsWith('en')) {
empty_en.loadLibrary();
} else if (locale.startsWith('de')) {
empty_de.loadLibrary();
if (locale == 'de_DE') {
await my_app_de_DE_empty.loadLibrary();
} else if (locale == 'en_US') {
await my_app_en_US_empty.loadLibrary();
}

final data = await _assetLoader(dataFile);
Expand All @@ -81,3 +64,24 @@ class MyAppMessages {
String current_sale_name(String arg) =>
_currentMessages.generateStringAtIndex(0, [arg]);
}

Message _pluralSelector(
num howMany,
String locale, {
required Message other,
Message? few,
Message? many,
Map<int, Message>? numberCases,
Map<int, Message>? wordCases,
}) {
return Intl.pluralLogic(
howMany,
few: few,
many: many,
zero: numberCases?[0] ?? wordCases?[0],
one: numberCases?[1] ?? wordCases?[1],
two: numberCases?[2] ?? wordCases?[2],
other: other,
locale: locale,
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// This is a helper file for deferred loading of the messages for locale de_DE, generated by `dart run messages`.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// This is a helper file for deferred loading of the messages for locale de_DE, generated by `dart run messages`.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// This is a helper file for deferred loading of the messages for locale en_US, generated by `dart run messages`.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// This is a helper file for deferred loading of the messages for locale en_US, generated by `dart run messages`.
Loading

0 comments on commit 63d1b55

Please sign in to comment.