I want to use flutter_i18n in a Flutter package. I have created assets/i18n in the package and written translation files inside it. Testing in the example of the package works fine.
The Flutter web app will load the translation file, but not the package's translation file.
Below are screenshots of the directories of the two projects and the invocation and loading methods of the flutter_i18n
web project
Directory screenshot
flutter_i18n init function
FlutterI18nDelegate initI18n() {
final i18n = FlutterI18nDelegate(
translationLoader: FileTranslationLoader(
fallbackFile: "zh_CN",
basePath: DartConsole.isDebug() ? "i18n" : "assets/assets/i18n",
// DartConsole.isDebug() Used to determine the directory that needs to be loaded. File loading is normal and can be ignored.
decodeStrategies: [JsonDecodeStrategy()],
useCountryCode: true,
),
missingTranslationHandler: (key, locale) {
print("--- Missing Key: $key, languageCode: ${locale?.languageCode}");
},
);
return i18n;
}
As the title says.
Below are screenshots of the directories of the two projects and the invocation and loading methods of the flutter_i18n
web project
import 'package:device_control_widget/device_control_widget.dart'; import 'package:flutter/material.dart'; import 'package:flutter_i18n/flutter_i18n.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
void main() { DartConsole.initLoggerFile(); CoreInfo.init(); runApp( MyApp( i18n: initI18n(), ), ); }
class MyApp extends StatelessWidget { final FlutterI18nDelegate i18n; const MyApp({ super.key, required this.i18n, }); // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo1', theme: StyleDef.theme, localizationsDelegates: [ i18n, DeviceControlWidget.i18n, // This code loads the FlutterI18nDelegate from the package. GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, GlobalCupertinoLocalizations.delegate, ], navigatorObservers: [FlutterSmartDialog.observer], supportedLocales: const [Locale('en', 'US'), Locale('zh', 'CN')], initialRoute: '/', routes: AppRouter.routerMap, builder: (context, widget) { final i18nInit = FlutterI18n.rootAppBuilder(); final smartDialogInit = FlutterSmartDialog.init(); final i18n = i18nInit(context, widget); return smartDialogInit(context, i18n); }, ); } }