国际化组件

国际化组件用于创建支持多语言和地区的应用。

提示:国际化需要引入 intl 包,并配置 ARB 文件。
格式化结果预览
日期格式化
2026-03-16
数字格式化
1,234,567.89
货币格式化
¥1,234.56
相对时间
3月16日

Intl

Intl 是 Flutter 的国际化包,提供日期、数字格式化等功能。

import 'package:intl/intl.dart';

// 日期格式化
final now = DateTime.now();
final dateFormatter = DateFormat('yyyy-MM-dd');
final formattedDate = dateFormatter.format(now);

// 数字格式化
final numberFormatter = NumberFormat.decimalPattern('zh_CN');
final formattedNumber = numberFormatter.format(1234567.89);

// 货币格式化
final currencyFormatter = NumberFormat.currency(
  locale: 'zh_CN',
  symbol: '¥',
);
final formattedCurrency = currencyFormatter.format(1234.56);

// 相对时间
final now = DateTime.now();
final tomorrow = now.add(Duration(days: 1));
final timeFormatter = DateFormat.yMd('zh_CN');
print('明天是: ${timeFormatter.format(tomorrow)}');

Localizations

Localizations 用于访问本地化的资源。

// 在 Widget 中使用
class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final locale = Localizations.localeOf(context);
    return Text('当前语言: ${locale.languageCode}');
  }
}

// 使用预定义的 MaterialLocalizations
class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final localizations = MaterialLocalizations.of(context);
    return Text(localizations.okButtonLabel);
  }
}

LocalizationsDelegate

LocalizationsDelegate 用于创建本地化委托。

class MyLocalizationsDelegate extends LocalizationsDelegate {
  const MyLocalizationsDelegate();

  @override
  bool isSupported(Locale locale) {
    return ['en', 'zh'].contains(locale.languageCode);
  }

  @override
  Future load(Locale locale) async {
    MyLocalizations localizations = MyLocalizations(locale);
    await localizations.load();
    return localizations;
  }

  @override
  bool shouldReload(MyLocalizationsDelegate old) => false;
}
支持的语言列表
🇺🇸
English
Hello
en_US
🇨🇳
简体中文
你好
zh_CN
🇹🇼
繁體中文
你好
zh_TW
🇯🇵
日本語
こんにちは
ja_JP

supportedLocales

supportedLocales 指定应用支持的语言列表。

MaterialApp(
  localizationsDelegates: [
    MyLocalizationsDelegate(),
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
    GlobalCupertinoLocalizations.delegate,
  ],
  supportedLocales: [
    const Locale('en', 'US'), // 英语
    const Locale('zh', 'CN'), // 简体中文
    const Locale('zh', 'TW'), // 繁体中文
    const Locale('ja', 'JP'), // 日语
  ],
  locale: Locale('zh', 'CN'),
  home: MyHomePage(),
)
Locale 创建方式
Locale('zh', 'CN')
zh_CN
Locale('en')
en
Locale.fromSubtags(...)
zh_Hans_CN
当前语言: zh_CN
语言代码: zh | 地区代码: CN | 脚本代码: Hans

Locale

Locale 表示特定的语言和地区。

// 创建 Locale
final locale1 = Locale('zh', 'CN'); // 语言_地区
final locale2 = Locale('en');       // 仅语言
final locale3 = Locale.fromSubtags(
  languageCode: 'zh',
  scriptCode: 'Hans',
  countryCode: 'CN',
);

// 获取当前 Locale
final currentLocale = Localizations.localeOf(context);

// 切换语言
void changeLanguage(BuildContext context, Locale newLocale) {
  MyApp.setLocale(context, newLocale);
}

// 格式化
final formatter = NumberFormat.currency(
  locale: locale.toString(),
);
国际化配置步骤:
  1. 在 pubspec.yaml 中添加 intl 依赖
  2. 创建 ARB 文件(如 en.arb, zh.arb)
  3. 运行 flutter gen-l10n 生成本地化代码
  4. 配置 MaterialApp 的 localizationsDelegates
  5. 在 Widget 中使用本地化字符串
// en.arb
{
  "hello": "Hello",
  "welcome": "Welcome, {name}!",
  "@welcome": {
    "placeholders": {
      "name": {
        "type": "String"
      }
    }
  }
}

// zh.arb
{
  "hello": "你好",
  "welcome": "欢迎,{name}!",
  "@welcome": {
    "placeholders": {
      "name": {
        "type": "String"
      }
    }
  }
}
ARB 文件预览
en.arb
"hello": "Hello",
"welcome": "Welcome, {name}!",
"@welcome": {
  "placeholders": {
    "name": {
      "type": "String"
    }
  }
}
zh.arb
"hello": "你好",
"welcome": "欢迎,{name}!",
"@welcome": {
  "placeholders": {
    "name": {
      "type": "String"
    }
  }
}