|
| 1 | +// Generated by package:messages_builder. |
| 2 | + |
| 3 | +import 'dart:ffi'; |
| 4 | + |
| 5 | +import 'package:intl4x/intl4x.dart'; |
| 6 | +import 'package:messages/messages_json.dart'; |
| 7 | + |
| 8 | +class AboutPageMessages { |
| 9 | + AboutPageMessages(); |
| 10 | + |
| 11 | + String _currentLocale = 'en'; |
| 12 | + |
| 13 | + final Map<String, MessageList> _messages = {}; |
| 14 | + |
| 15 | + static const _dataFiles = { |
| 16 | + 'fr': ('package:example_json/assets/l10n/testarbctx2_fr.json', 'EyPjEJJU'), |
| 17 | + 'en': ('package:example_json/assets/l10n/testarbctx2.json', 'QrwRSsOy') |
| 18 | + }; |
| 19 | + |
| 20 | + String get currentLocale => _currentLocale; |
| 21 | + |
| 22 | + MessageList get _currentMessages => _messages[currentLocale]!; |
| 23 | + |
| 24 | + static Iterable<String> get knownLocales => _dataFiles.keys; |
| 25 | + |
| 26 | + Future<void> loadLocale(String locale) async { |
| 27 | + if (!_messages.containsKey(locale)) { |
| 28 | + final info = _dataFiles[locale]; |
| 29 | + final carb = info?.$1; |
| 30 | + if (carb == null) { |
| 31 | + throw ArgumentError('Locale $locale is not in $knownLocales'); |
| 32 | + } |
| 33 | + final data = await AssetBundle.loadString(carb); |
| 34 | + final messageList = MessageListJson.fromString(data, _pluralSelector); |
| 35 | + if (messageList.preamble.hash != info?.$2) { |
| 36 | + throw ArgumentError(''' |
| 37 | + Messages file for locale $locale has different hash "${messageList.preamble.hash}" than generated code "${info?.$2}".'''); |
| 38 | + } |
| 39 | + _messages[locale] = messageList; |
| 40 | + } |
| 41 | + _currentLocale = locale; |
| 42 | + } |
| 43 | + |
| 44 | + void loadAllLocales() { |
| 45 | + for (final locale in knownLocales) { |
| 46 | + loadLocale(locale); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + String aboutMessage(String websitename) => |
| 51 | + _currentMessages.generateStringAtIndex(0, [websitename]); |
| 52 | + |
| 53 | + String helloAndWelcome( |
| 54 | + String firstName, |
| 55 | + int lastName, |
| 56 | + ) => |
| 57 | + _currentMessages.generateStringAtIndex(1, [firstName, lastName]); |
| 58 | + |
| 59 | + String newMessages(int newMessages) => |
| 60 | + _currentMessages.generateStringAtIndex(2, [newMessages]); |
| 61 | + |
| 62 | + String newMessages2( |
| 63 | + String gender, |
| 64 | + int newVar, |
| 65 | + ) => |
| 66 | + _currentMessages.generateStringAtIndex(3, [gender, newVar]); |
| 67 | + |
| 68 | + String get otherMsg => _currentMessages.generateStringAtIndex(4, []); |
| 69 | +} |
| 70 | + |
| 71 | +class HomePageMessages { |
| 72 | + HomePageMessages(); |
| 73 | + |
| 74 | + String _currentLocale = 'en'; |
| 75 | + |
| 76 | + final Map<String, MessageList> _messages = {}; |
| 77 | + |
| 78 | + static const _dataFiles = { |
| 79 | + 'de': ('package:example_json/assets/l10n/testarb_de.json', 'hbDN1MhX'), |
| 80 | + 'en': ('package:example_json/assets/l10n/testarb.json', 'dr9Md951') |
| 81 | + }; |
| 82 | + |
| 83 | + String get currentLocale => _currentLocale; |
| 84 | + |
| 85 | + MessageList get _currentMessages => _messages[currentLocale]!; |
| 86 | + |
| 87 | + static Iterable<String> get knownLocales => _dataFiles.keys; |
| 88 | + |
| 89 | + Future<void> loadLocale(String locale) async { |
| 90 | + if (!_messages.containsKey(locale)) { |
| 91 | + final info = _dataFiles[locale]; |
| 92 | + final carb = info?.$1; |
| 93 | + if (carb == null) { |
| 94 | + throw ArgumentError('Locale $locale is not in $knownLocales'); |
| 95 | + } |
| 96 | + final data = await AssetBundle.loadString(carb); |
| 97 | + final messageList = MessageListJson.fromString(data, _pluralSelector); |
| 98 | + if (messageList.preamble.hash != info?.$2) { |
| 99 | + throw ArgumentError(''' |
| 100 | + Messages file for locale $locale has different hash "${messageList.preamble.hash}" than generated code "${info?.$2}".'''); |
| 101 | + } |
| 102 | + _messages[locale] = messageList; |
| 103 | + } |
| 104 | + _currentLocale = locale; |
| 105 | + } |
| 106 | + |
| 107 | + void loadAllLocales() { |
| 108 | + for (final locale in knownLocales) { |
| 109 | + loadLocale(locale); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + String helloAndWelcome( |
| 114 | + String firstName, |
| 115 | + String lastName, |
| 116 | + ) => |
| 117 | + _currentMessages.generateStringAtIndex(0, [firstName, lastName]); |
| 118 | + |
| 119 | + String helloAndWelcome2( |
| 120 | + String firstName, |
| 121 | + String lastName, |
| 122 | + ) => |
| 123 | + _currentMessages.generateStringAtIndex(1, [firstName, lastName]); |
| 124 | + |
| 125 | + String newMessages(int newMessages) => |
| 126 | + _currentMessages.generateStringAtIndex(2, [newMessages]); |
| 127 | + |
| 128 | + String newMessages2( |
| 129 | + String gender, |
| 130 | + int newVar, |
| 131 | + ) => |
| 132 | + _currentMessages.generateStringAtIndex(3, [gender, newVar]); |
| 133 | +} |
| 134 | + |
| 135 | +Message _pluralSelector( |
| 136 | + num howMany, |
| 137 | + String locale, { |
| 138 | + required Message other, |
| 139 | + Message? few, |
| 140 | + Message? many, |
| 141 | + Map<int, Message>? numberCases, |
| 142 | + Map<int, Message>? wordCases, |
| 143 | +}) { |
| 144 | + Message getCase(int i) => numberCases?[i] ?? wordCases?[i] ?? other; |
| 145 | + return switch (Intl(locale: Locale.parse(locale)).plural().select(howMany)) { |
| 146 | + PluralCategory.zero => getCase(0), |
| 147 | + PluralCategory.one => getCase(1), |
| 148 | + PluralCategory.two => getCase(2), |
| 149 | + PluralCategory.few => few ?? other, |
| 150 | + PluralCategory.many => many ?? other, |
| 151 | + PluralCategory.other => other, |
| 152 | + }; |
| 153 | +} |
0 commit comments