Open codelovercc opened 9 months ago
I have the same problem, can you share with me the solution 2 you did? or the first one?
@Douglas-Pontes
Solution 2:
class MultiCompositeMessageLookup extends CompositeMessageLookup {
@override
void addLocale(String localeName, Function findLocale) {
final canonical = Intl.canonicalizedLocale(localeName);
final newLocale = findLocale(canonical);
if (newLocale != null) {
final oldLocale = availableMessages[localeName];
if (oldLocale != null && newLocale != oldLocale) {
if (newLocale is! MessageLookupByLibrary) {
throw Exception('Merge locale messages failed, type ${newLocale.runtimeType} is not supported.');
}
// solve issue https://github.com/dart-lang/i18n/issues/798 if you are using intl_translate and intl_util both.
if (oldLocale.messages is Map<String, Function> && newLocale.messages is! Map<String, Function>) {
final newMessages = newLocale.messages.map((key, value) => MapEntry(key, value as Function));
oldLocale.messages.addAll(newMessages);
} else {
oldLocale.messages.addAll(newLocale.messages);
}
return;
}
super.addLocale(localeName, findLocale);
}
}
}
Then call initializeInternalMessageLookup(() => MultiCompositeMessageLookup());
before any localizations class load
method.
I have the same problem. Almost all examples I found use a very simple one-package setup. How do people use this in larger projects?
@stwarwas Just call initializeInternalMessageLookup(() => MultiCompositeMessageLookup());
at the first line in your main
method.
simple solution is - https://github.com/Luvti/i18n
dependency_overrides:
intl: #0.19.0
git:
url: https://github.com/Luvti/i18n
path: pkgs/intl
I created some packages and one flutter application , these projects use
Intl
for localizations. Packages and application:member_end
, pure dart package, contains business logic codes, it usesIntl
andintl_translation
for localizations, it has a customMemberLocalizations
class that defined localization message getters and has aload
method.member_end_flutter
, Flutter package, contains common widgets and implements for Flutter, it depends onmember_end
, it usesintl
andintl_utils
for localizations, the localizations class is namedMemberFlutterLocalizations
.member_end_app
, Flutter application, it depends onmember_end
andmember_end_flutter
, it usesintl
andintl_utils
for localizations, the localizations class is defaultS
.These projects supports
en
andzh
Locales.Files:
member_end
member_end |---lib |---|---l10n |---|---|---intl_en.arb |---|---|---intl_zh.arb |---|---src |---|---|---intl |---|---|---|---messages_all.dart |---|---|---|---messages_en.dart |---|---|---|---messages_zh.dartmember_end_flutter
member_end_flutter |---lib |---|---l10n |---|---|---intl_en.arb |---|---|---intl_zh.arb |---|---generated |---|---|---l10n.dart |---|---|---intl |---|---|---|---messages_all.dart |---|---|---|---messages_en.dart |---|---|---|---messages_zh.dartmember_end_app
member_end_app |---lib |---|---l10n |---|---|---intl_en.arb |---|---|---intl_zh.arb |---|---generated |---|---|---l10n.dart |---|---|---intl |---|---|---|---messages_all.dart |---|---|---|---messages_en.dart |---|---|---|---messages_zh.dartLet's say the current locale is
zh
, theLocalizations
classes are loaded in orderMemberLocalizations
MemberFlutterLocalizations
S
The problem is only the first
MemberLocalizations
will load itsmember_end/lib/src/intl/messages_zh.dart
, this causemember_end_flutter
andmember_end_app
can't get the correct locale messages.In Localizations classes
static Future<S> load(Locale locale)
method, it useFuture<bool> initializeMessages(String localeName)
method to init and load messages,Future<bool> initializeMessages(String localeName)
useCompositeMessageLookup
to add locale messages, let's checkCompositeMessageLookup.addLocale
method:When the first
MemberLocalizations
load, the localezh
is not exists, solocaleExists(localeName)
returnsfalse
, and then themember_end
package'szh
locale message will load.MemberFlutterLocalizations
will be loaded by next in the order, when it runs intoCompositeMessageLookup.addLocale
,localeExists(localeName)
returnstrue
, because localezh
MessageLookupByLibrary
is already added byMemberLocalizations
inmember_end
package,S
will be the same when it's loading.To solve this issue, I have few ways to do:
intl
.CompositeMessageLookup
namedCustomCompositeMessageLookup
and override methodaddLocale
, check if locale exists and then merge the newMessageLookupByLibrary
into the oldMessageLookupByLibrary
, if the locale message name is already exists then overwrite with the new value that provided by the newMessageLookupByLibrary
, then callvoid initializeInternalMessageLookup(()=>CustomCompositeMessageLookup())
method in themain
method to init global fieldMessageLookup messageLookup
. ButinitializeInternalMessageLookup
is not a public API.intl
works in multiple projects.If there is other better way to solve this, please tell me :)