如何在颤振自定义库中设置本地化?

时间:2021-05-11 16:50:22

标签: flutter

我在我的项目中创建了一些库。有很多单词和短语,所以我必须翻译所有内容。 我使用 bloc 设置了应用程序的语言,但我不知道如何在我的自定义库中使用它。 我想我应该使用与上下文相关的东西。我一点也不知道。 请帮帮我。

这是我的本地化课程。

import 'dart:convert';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;

import 'constants/constants.dart';

class AppLocalizations {
  Locale locale;

  AppLocalizations(this.locale);

  /// Helper method to keep the code in the widgets concise
  /// Localizations are accessed using an InheritedWidget "of" syntax
  static AppLocalizations of(BuildContext context) {
    return Localizations.of(context, AppLocalizations);
  }

  /// Static member to have a simple access to the delegate from the MaterialApp
  static const LocalizationsDelegate<AppLocalizations> delegate =
      _AppLocalizationsDelegate();

  late Map<String, String> _localizedStrings;

  Future<bool> load() async {
    String jsonString =
        await rootBundle.loadString('assets/langs/${locale.languageCode}.json');
    Map<String, dynamic> jsonMap = json.decode(jsonString);

    _localizedStrings = jsonMap.map((key, value) {
      return MapEntry(key, value.toString());
    });

    return true;
  }

  String? translate(String key) {
    return _localizedStrings[key];
  }
}

class _AppLocalizationsDelegate
    extends LocalizationsDelegate<AppLocalizations> {
  const _AppLocalizationsDelegate();

  // add all languages code here
  @override
  bool isSupported(Locale locale) {
    return LanguageKeys.LANGUAGE_KEYS_LIST.contains(locale.languageCode);
  }

  // load all localization files
  @override
  Future<AppLocalizations> load(Locale locale) async {
    AppLocalizations localizations = new AppLocalizations(locale);
    await localizations.load();
    return localizations;
  }

  @override
  bool shouldReload(LocalizationsDelegate<AppLocalizations> old) => false;
}

1 个答案:

答案 0 :(得分:1)

我对 bloc 模式了解不多,因为我一直使用提供程序。

但基本步骤是:

(1) 将 AppLocalizations 类本地化注入到 Material 应用程序中

MaterialApp(
  supportedLocales: [
    Locale('en'),
    Locale('es'),
  ],
  localizationsDelegates: [
    AppLocalizations.delegate,
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
  ],
  localeResolutionCallback: (locale, supportedLocales) {
    for (var supportedLocale in supportedLocales) {
      if (supportedLocale.languageCode == locale.languageCode &&
          supportedLocale.countryCode == locale.countryCode) {
        return supportedLocale;
      }
    }
    return supportedLocales.first;
  },
  locale: Locale('en'), // translate to this language
  .......
);

(2) 对于任何翻译,传递应用本地化的关键

AppLocalizations.of(context).translate("understood")

您可以在 AppLocalizations 类中更改翻译方法

(3) 如果您想更改应用语言环境/语言,请将您的状态变量添加到 Material App 中的语言环境键,并将消费者置于 MaterialApp 本身之上。

ChangeNotifierProvider<LangSettingsProvider>(
      create: (_) => LangSettingsProvider(),
      child: Consumer<LangSettingsProvider>(
        builder: (context, lang, _) {
          return MaterialApp(
           locale : lang.appLocal
          
       ....

(4) 在assets文件夹中包含assets并在pubspec yml文件中指定

  assets:
    - assets/icon/
    - assets/lang/en.json
    - assets/lang/es.json

(5) json键值对示例

//en.json 
"understood":"Understood",
//es.json
"understood":"Entendido",