pubspec添加依赖
想要使用 flutter_localizations 的话,我们需要在 pubspec.yaml
文件中添加它作为依赖:
flutter_localizations: #组件国际化
sdk: flutter
设置MaterialApp
-
在localizationsDelegates中指定哪些Widget需要进行国际化
-
用于生产本地化值集合的工厂
-
我们这里指定了Material、Widgets、Cupertino都使用国际化
-
-
supportedLocales指定要支持哪些国际化
-
我们这里指定中文和英文(也可以指定国家编码)
MaterialApp( localizationsDelegates: [ GlobalMaterialLocalizations.delegate, // 指定本地化的字符串 GlobalCupertinoLocalizations.delegate, // 对应的Cupertino风格 GlobalWidgetsLocalizations.delegate // 指定默认的文本排列方向, 由左到右或由右到左 ], supportedLocales: [ Locale("en"), Locale("zh") ], )
注意:如果要指定语言代码、文字代码和国家代码,可以进行如下指定方式:
-
// Full Chinese support for CN, TW, and HK supportedLocales: [ const Locale.fromSubtags(languageCode: 'zh'), // generic Chinese 'zh' const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans'), // generic simplified Chinese 'zh_Hans' const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hant'), // generic traditional Chinese 'zh_Hant' const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans', countryCode: 'CN'), // 'zh_Hans_CN' const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hant', countryCode: 'TW'), // 'zh_Hant_TW' const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hant', countryCode: 'HK'), // 'zh_Hant_HK' ],
-
-
THE END