目標(biāo):實(shí)現(xiàn)flutter國(guó)際化
提示:這里參考一下幾個(gè)鏈接
例如:
- https://github.com/ThinkerWing/language
-
https://juejin.cn/post/6844903823119482888
這篇也很詳細(xì),還有包括兼容中文的繁體簡(jiǎn)體… 可以看看
feat/use-Flutter-Intl
該分支對(duì)應(yīng)的提交是使用Android Studio 和 Flutter Intl插件 并根據(jù)掘金這篇文章的實(shí)踐,兼容漢字簡(jiǎn)體和繁體字
https://github.com/ThinkerWing/language/commit/f5fd58453f85b5b9c0e58df6270fac4cf22f200d
實(shí)現(xiàn)效果
- 本地語言中文:你好,think
- 本地語言英語:hello,think
開始之前先創(chuàng)建項(xiàng)目
flutter create `project`
可以看我git上的提交記錄,創(chuàng)建項(xiàng)目完之后initial commit了,然后第二次提交就是增加多語言的功能。
https://github.com/ThinkerWing/language
第一步,添加intl and flutter_localizations,并啟動(dòng)generate標(biāo)志:
https://pub.flutter-io.cn/packages/intl/install
這將在您的包的 pubspec.yaml 中添加這樣一行
https://docs.flutter.dev/development/accessibility-and-localization/internationalization
啟動(dòng)generate標(biāo)志
第二步,lib文件夾中新建文件夾l10n或者locale,并在其中創(chuàng)建app_en.arb 和app_zh.arb文件:
第三步,在flutte項(xiàng)目的根目錄中添加l10n.yaml, 內(nèi)容如下:
Add a new yaml file to the root directory of the Flutter project called l10n.yaml with the following content:
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
添加完成之后,執(zhí)行命令 flutter run,dart_tools會(huì)自動(dòng)生成相關(guān)的文件
第四步,在主程序MaterialApp中,添加下面內(nèi)容:
需要導(dǎo)入的包文章來源:http://www.zghlxwxcb.cn/news/detail-422291.html
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
return const MaterialApp(
title: 'Localizations Sample App',
localizationsDelegates: [ // 本地化應(yīng)用的代理
AppLocalizations.delegate, // 應(yīng)用程序本地化代理
GlobalMaterialLocalizations.delegate, // 全局材質(zhì)組件的本地化代理
GlobalWidgetsLocalizations.delegate, // 全局組件本地化代理
],
supportedLocales: [
Locale('en', 'US'), // English, no country code
Locale('zh', 'CN'), // Spanish, no country code
],
home: MyHomePage(),
);
在您的應(yīng)用程序的任何位置使用 AppLocalizations。 在這里,翻譯后的消息用于文本小部件。
Use AppLocalizations anywhere in your app. Here, the translated message is used in a Text widget.文章來源地址http://www.zghlxwxcb.cn/news/detail-422291.html
Text(AppLocalizations.of(context)!.helloWorld);
到了這里,關(guān)于【國(guó)際化Intl】Flutter 國(guó)際化多語言實(shí)踐的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!