Flutter国际化插件l10n_esperanto的使用
Flutter国际化插件l10n_esperanto的使用
l10n_esperanto
Esperanto本地化支持适用于Flutter。这将仅本地化基本字符串(就像Flutter支持的所有其他语言一样),以及intl
包中的各种日期和时间格式。您的应用字符串需要通过自己的本地化处理,如正常操作。
尽管没有Android设备将Esperanto作为系统语言,因此不会自动选择该语言,但您可以将其用作应用程序中可用的语言之一。甚至可以将其设置为默认语言(完全不使用英语),所有设备都会以Esperanto显示您的应用,除非选择了其他语言。
开始使用
在您的应用中使用:
MaterialApp(
supportedLocales: [
const Locale(...),
const Locale('eo'), // 添加Esperanto语言
],
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
MaterialLocalizationsEo.delegate, // 加载Esperanto的MaterialLocalizations
CupertinoLocalizationsEo.delegate, // 加载Esperanto的CupertinoLocalizations
],
如果您使用标准的Flutter Intl国际化:
MaterialApp(
supportedLocales: S.delegate.supportedLocales,
localizationsDelegates: const [
S.delegate,
MaterialLocalizationsEo.delegate, // 加载Esperanto的MaterialLocalizations
CupertinoLocalizationsEo.delegate, // 加载Esperanto的CupertinoLocalizations
],
注意顺序很重要,始终从全局开始。
使用DateFormat
一个已知的限制似乎是,只有当您的应用运行在相同的语言环境中时,才能访问自定义语言环境的DateFormat
设置。确保您也在应用中或通过用户选择指定语言环境。
MaterialApp(
locale: const Locale('eo'), // 设置应用的语言环境为Esperanto
supportedLocales: [...], // 支持的其他语言
localizationsDelegates: [...], // 加载的本地化代理
如果您计划使用类似以下调用:
DateFormat.MMMMEEEEd('eo').format(DateTime.now()) // 使用Esperanto格式化当前日期
完整示例
为了更好地理解如何在Flutter应用中使用l10n_esperanto
插件,这里提供一个完整的示例Demo。
主文件 main.dart
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:l10n_esperanto/material_localizations_eo.dart'; // 导入Esperanto的MaterialLocalizations
import 'package:l10n_esperanto/cupertino_localizations_eo.dart'; // 导入Esperanto的CupertinoLocalizations
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
supportedLocales: [
const Locale('eo', ''), // Esperanto
const Locale('en', ''), // 英语
],
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
MaterialLocalizationsEo.delegate, // 加载Esperanto的MaterialLocalizations
CupertinoLocalizationsEo.delegate, // 加载Esperanto的CupertinoLocalizations
],
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
[@override](/user/override)
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
DateFormat.yMMMMd('eo').format(DateTime.now()), // 使用Esperanto格式化当前日期
style: Theme.of(context).textTheme.headlineMedium,
),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
更多关于Flutter国际化插件l10n_esperanto的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复