Flutter-国际化工具flutter_i18n

1.配置 pubspec.yaml

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter

2. vscode搜索插件 vscode-flutter-i18n-json

安装重新启动vscode
安装成功目录下会多一个目录


屏幕快照 2019-08-26 上午10.41.36.png

3. 配置语言

按住shift+alt+p打开搜索框
输入Flutter I18n Json: Initialize
选择语言默认en-US. 英语
Locale("zh","CN");CN是简体中文,如果是香港中文是Locale("zh","HK")
选择成功后会多一个文件


Flutter-国际化工具flutter_i18n_第1张图片
屏幕快照 2019-08-26 上午10.42.15.png

4.配置main.dart

引入import 'generated/i18n.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
在localizationsDelegates中增加S.delegate
supportedLocales: S.delegate.supportedLocales,
如果只支持中文:localeResolutionCallback: S.delegate.resolution(fallback: Locale("zh",""))

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:hello_world/generated/i18n.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      localizationsDelegates: [
        I18n.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: I18n.delegate.supportedLocales,
      home: MyHomePage(title: '1111'),
//      home: MyHomePag(I18n.of(context).appName)
    );
  }
}

// home: MyHomePag(S.of(context).appName)
为什么注释掉?
I18n.of(context). appName。

因为这时候I18n还没被塞到context中,I18n.of(context)这时候还是null

4.使用方法

I18n.of(context).greetTo('lcj')

你可能感兴趣的:(Flutter-国际化工具flutter_i18n)