ilteoood / flutter_i18n

I18n made easy, for Flutter!
MIT License
217 stars 57 forks source link

How to use flutter_i18n to create i18n widgets in a Flutter package #215

Closed MengLizhi closed 3 months ago

MengLizhi commented 8 months ago

As the title says.

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

image

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;
}

main.dart


import 'package:device_control/i18n/i18n.dart';
import 'package:device_control/router.dart';
import 'package:device_control/store/core_info.dart';
import 'package:device_control/style/style_def.dart';
import 'package:device_control/utils/log/dart_console.dart';

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); }, ); } }


#### package project
![image](https://github.com/ilteoood/flutter_i18n/assets/21377743/f31a9796-2654-443c-98ed-ef1e9ddfdac9)

> flutter_i18n create  FlutterI18nDelegate

```dart
import 'package:dart_console/dart_console.dart';
import 'package:flutter_i18n/flutter_i18n_delegate.dart';
import 'package:flutter_i18n/loaders/decoders/json_decode_strategy.dart';
import 'package:flutter_i18n/loaders/file_translation_loader.dart';

FlutterI18nDelegate initI18n() {
  final i18n = FlutterI18nDelegate(
    translationLoader: FileTranslationLoader(
      fallbackFile: "zh_CN",
      basePath: DartConsole.isDebug()
          ? "packages/device_control_widget/assets/i18n"
          : "packages/device_control_widget/assets/i18n",
      decodeStrategies: [JsonDecodeStrategy()],
      useCountryCode: true,
    ),
    missingTranslationHandler: (key, locale) {
      print("--- Missing Key: $key, languageCode: ${locale?.languageCode}");
    },
  );

  return i18n;
}

device_control_widget.dart

class DeviceControlWidget {
/// Definition of a multilingual module
static FlutterI18nDelegate i18n = initI18n();
}
ilteoood commented 8 months ago

I think this case has never been supported. If you could provide a repro, I could try to fix it but I don't guarantee

ilteoood commented 3 months ago

No repro provided