Flutter国际化语言轻松搞定

Flutter国际化

1.引入依赖

yaml 复制代码
dependencies: 
 intl: ^0.20.2
  flutter_localizations:
    sdk: flutter
 
 flutter:
  # 针对多语言 l10n
  generate: true

2.在项目根目录创建 l10n.yaml 配置文件

yaml 复制代码
arb-dir: lib/common/config/localization/l10n # 多语言资源文件的目录
template-arb-file: app_en.arb 	 # 基础模板文件(通常为英文)
output-localization-file: app_localizations.dart # 生成的国际化代码文件名
output-class: AppLocalizations # 生成的国际化类名
preferred-supported-locales:
  - en
  - zh
synthetic-package: false

3.创建语言文件 app_en.arb, app_zh.arb

txt 复制代码
zh:
{
  "welcomeMessage": "Welcome To Flutter",
  "app_name": "WarmTodo",
  "today": "今天",
  "no_todo": "暂无待办事项",
  "todo": "待办事项",
  "enter_todo": "输入待办事项...",
  "save": "保存",
  "mark_completed": "标记为完成",
  "mark_uncompleted": "标记为未完成",
  "priority": "优先级",
  "low": "低优先级",
  "medium": "中优先级",
  "high": "高优先级"
}

en:

{
  "welcomeMessage": "Welcome To Flutter",
  "app_name": "WarmTodo",
  "today": "Today",
  "no_todo": "There are no pending tasks",
  "todo": "To-do",
  "enter_todo": "Enter the to-do items",
  "save": "Save",
  "mark_completed": "Marked as finished",
  "mark_uncompleted": "Marked as unfinished",
  "priority": "Priority",
  "low": "Low priority",
  "medium": "Medium priority",
  "high": "High priority"
}

4.执行 flutter gen-l10n 命令

保存配置后,执行 flutter gen-l10n 命令,Flutter 会自动在 lib/generated 目录下生成 app_localizations.dart 文件,该文件包含所有多语言文案的访问方法。

5.在应用中配置本地化代理

dart 复制代码
void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: AppLocalizations.localizationsDelegates, //配置本地化代理(必须)
      //配置应用支持的语言列表
      supportedLocales: [
        const Locale('en','US'),
        const Locale('zh','CN'),
      ],
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: .fromSeed(seedColor: Colors.deepPurple),
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

6.通过扩展方法(Extension)封装国际化工具类的便捷调用

dart 复制代码
import 'package:flutter/material.dart';
import 'l10n/app_localizations.dart';

extension LocalizationExtension on BuildContext {
  AppLocalizations get l10n => AppLocalizations.of(this)!;
}

7.在组件中使用

dart 复制代码
Text(context.l10n.app_name)

恭喜!你学会了Flutter国际化!Happy Coding !😊😊😊

相关推荐
GitLqr9 小时前
玩转 Flutter 中的 Stack 与 Positioned:解决 UI 重叠问题的实战指南
flutter·面试·全栈
唔6617 小时前
flutter web iOS 在浏览器加载中文慢的问题
前端·flutter·ios
恋猫de小郭17 小时前
Jetpack Compose 8 月版正式发布,核心模块 1.12
android·前端·flutter
梦想的颜色2 天前
AI 时代小白 VibeCoding 做 APP:UniApp(含 Uni‑X)、Flutter 与 React Native+Expo全维度技术选型对比指南
flutter·react native·app·uniapp·vibecoding·unippx·app产品
坚果的博客2 天前
Flutter-OH 3.44.9-dev 悄然上线|首个 OpenHarmony Canary 预览版上线
flutter
大龄秃头程序员2 天前
Flutter 动画随笔:业务里真正高频的几类控件
flutter
天空之城--2 天前
Android Flutter行业最新动态与实用参考(2026年8月第2周)
android·flutter
恋猫de小郭2 天前
Flutter 3.47 发布,快来看看有什么更新吧
android·前端·flutter
大龄秃头程序员2 天前
一次 Flutter 动画 Demo 的整理与复盘
flutter