flutter国际化

首先Android studio安装插件flutter intl
安装完成后,选择tools-》flutterintl-》initialize for the project,初始化项目会生成几个文件如下:
在这里插入图片描述
在这里插入图片描述

会生成一个i10n的文件夹,里面有intl_en.zrb文件,里面是一个map,保存的是英文的国家化,如果需要添加新的字符,在这个文件修改即可,
还有一个generated文件夹,里面有i10n.dart,和messages.dart文件,这个有插件自动生成,这里的文件不要我们修改。
下面添加依赖库
将flutter_localizations和intl作为依赖项添加到您的pubspec.yaml文件中:

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  intl: ^0.16.0

下面是代码的实现
接下来,导入flutter_localizations库,并指定MaterialApp的localizationsDelegates和supportedLocales

import ‘package:flutter_localizations/flutter_localizations.dart’;

//localizationsDelegates列表中的元素是生成本地化值集合的工厂。
//GlobalMaterialLocalizations.delegate 为Material //Components库提供了本地化的字符串和其他值。 //GlobalWidgetsLocalizations.delegate定义widget默认的文本方向,从左到右或从右到左。
//GlobalCupertinoLocalizations.delegate iOS国际化需要

我们只要在下面这里添加我们要的语言即可

new MaterialApp(
 localizationsDelegates: [
   GlobalMaterialLocalizations.delegate,
   GlobalWidgetsLocalizations.delegate,
   GlobalCupertinoLocalizations.delegate,
   DemoLocalizationsDelegate(),
 ],
 supportedLocales: [
    const Locale('en', 'US'), // English
    const Locale('zh', 'CN'), // Hebrew
  ],
)

DemoLocalizations类包含应用程序的字符串(仅用于示例),该字符串被翻译为应用程序支持的语言环境。 使用Dart的intl 包生成的函数initializeMessages()来加载翻译的字符串,并使用Intl.message()查找它们。

class DemoLocalizations {
  DemoLocalizations(this.locale);
static Future<DemoLocalizations> load(Locale locale) {
    final String name = locale.countryCode.isEmpty ? locale.languageCode : locale.toString();
    final String localeName = Intl.canonicalizedLocale(name);
    return initializeMessages(localeName).then((Null _) {
      Intl.defaultLocale = localeName;
      return new DemoLocalizations();
    });
  }

  static DemoLocalizations of(BuildContext context) {
    return Localizations.of<DemoLocalizations>(context, DemoLocalizations);
  }

  String get title {
    return Intl.message(
      'Hello World',
      name: 'title',
      desc: 'Title for the Demo application',
    );
  }

}


class DemoLocalizationsDelegate extends LocalizationsDelegate<DemoLocalizations> {
  const DemoLocalizationsDelegate();

  @override
  bool isSupported(Locale locale) => ['en', 'cn'].contains(locale.languageCode);

  @override
  Future<DemoLocalizations> load(Locale locale) {
    return new SynchronousFuture<DemoLocalizations>(new DemoLocalizations(locale));
  }

  @override
  bool shouldReload(DemoLocalizationsDelegate old) => false;
}

使用DemoLocalizations.of(context).title

使用Dart intl工具

在使用Dart intl包构建API之前, 您需要查看intl包的文档。以下是根据intl软件包本地化应用程序的过程摘要。

示例程序依赖于一个生成的源文件l10n/messages_all.dart ,它定义了应用程序使用的所有本地化字符串。

重新构建 l10n/messages_all.dart 需要两个步骤.

将应用程序的根目录作为当前目录,从lib/main.dart生成l10n/intl_messages.arb

$ flutter pub pub run intl_translation:extract_to_arb --output-dir=lib/i10n lib/main.dart
该intl_messages.arb文件是一个JSON格式的map,拥有一个在main.dart中定义的Intl.message()函数入口。 此文件作为英语和西班牙语翻译的一个模板,intl_en.arb和intl_es.arb。这些翻译是由您,开发人员创建的。

使用应用程序的根目录作为当前目录,为每个intl_.arb文件生成intl_messages_.dart,并在intl_messages_all.dart中导入所有message文件:

$ flutter pub pub run intl_translation:generate_from_arb --output-dir=lib/l10n
–no-use-deferred-loading lib/main.dart lib/l10n/intl_*.arb
DemoLocalizations类使用生成的initializeMessages() 函数(定义在intl_messages_all.dart)来加载本地化的message并使用Intl.message()来查找它们。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值