Flutter -- Intl 国际化

2021-02-03  本文已影响0人  jancywen

1.安装插件

在VS Code 的Marketplace里搜索并安装

2.初始化

(Cmd+Shift+P)
View -> Command Palette -> Flutter Intl: Initialize

默认英文,自动生成lib/l10n/intl_en.arb, lib/generated/

注:lib/generated/ 文件夹里的文件不可手动编辑,当编辑 .arb 文件时,generated文件夹里的文件会自动生成

3.设置工程

添加依赖

dependencies:
    // Other dependencies...
    flutter_localizations:
        sdk: flutter

设置代理

import 'package:flutter_localizations/flutter_localizations.dart';
import 'generated/l10n.dart';

class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
        return new MaterialApp(
            localizationsDelegates: [
                S.delegate,
                GlobalMaterialLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate,
                GlobalCupertinoLocalizations.delegate,
            ],
            supportedLocales: S.delegate.supportedLocales,
            title: 'Flutter Demo',
            home: new MyHomePage(title: 'Flutter Demo Home Page'),
        );
    }
}

4.使用

Text(S.of(context).pageHomeConfirm,)

5.添加Locale

Flutter Intl: Add locale

6.删除

Flutter Intl: Remove locale

7.更换Locale

S.load(Locale('de', 'DE'));

为iOS应用程序启用本地化,扩展iOS /Runner/Info。支持地区配置的plist文件。这个列表应该与lib/l10n文件夹中列出的区域设置一致。

<key>CFBundleLocalizations</key>
<array>
    <string>en</string>
    <string>de_DE</string>
    ...
</array>
上一篇 下一篇

猜你喜欢

热点阅读