Solve the problem that the package internationalization introduced by flutter is invalid

Solve the problem of invalid internationalization in the library introduced by flutter intl

Problem scene

You have introduced several modules, and these modules have been internationalized respectively. For example, the following four modules ABCD, and finally found that only the internationalization effect of A can be used, and other modules are invalid:

localizationsDelegates: const [
          moduleA.S.delegate,
          moduleB.S.delegate,
          moduleC.S.delegate,
          moduleD.S.delegate,
          S.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
        ]

Solution

Modify the internationalization-related code of the imported module:

Create a new my_intl.dart to replace intl in l10n.dart:

import 'package:flutter/material.dart';
import 'package:intl/src/intl_helpers.dart';
import 'package:intl/intl.dart' as RealIntl;
import '../generated/intl/messages_all.dart';
import '../generated/l10n.dart';

class Intl {
  // copied from the real intl package
  static String message(String message_str,
          {String desc: '',
          Map<String, Object> examples: const {},
          String locale,
          String name,
          List<Object> args,
          String meaning,
          bool skip}) =>
      _message(message_str, locale, name, args, meaning);

  // copied from the real intl package
  static String _message(
      String message_str, String locale, String name, List<Object> args, String meaning) {
    return myMessageLookup.lookupMessage(message_str, locale, name, args, meaning);
  }

  static MessageLookup myMessageLookup =
      UninitializedLocaleData('initializeMessages(<locale>)', null);

  static Future<S> load(Locale locale) {
    final name = locale.countryCode?.isEmpty ?? true ? locale.languageCode : locale.toString();
    final localeName = RealIntl.Intl.canonicalizedLocale(name);

    return initializeMessages(localeName).then((_) {
      final original = messageLookup;
      messageLookup = myMessageLookup;
      return initializeMessages(localeName).then((_) {
        myMessageLookup = messageLookup;
        messageLookup = original;
        RealIntl.Intl.defaultLocale = localeName;
        S.current = S();
        return S.current;
      });
    });
  }
}

Point the load method in l10n.dart to the load method in my_intl.

Modified l10n.dart:

import 'package:cece_login_module/l10n/my_intl.dart';
import 'package:flutter/material.dart';

class S {
 ...
  static Future<S> load(Locale locale) => Intl.load(locale);
 ...
 

Catalog map:
insert image description here

Related Linksgithub

Guess you like

Origin blog.csdn.net/u011272795/article/details/111176805