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 !😊😊😊

相关推荐
Geek_Vison8 小时前
保险APP已经用了Flutter,还能接入小程序吗?
flutter·小程序·uni-app·mpaas·小程序容器
FungLeo8 小时前
成为全栈·Flutter App 篇·go_router 与四 Tab App Shell:保留导航状态、深链和登录回跳
flutter·移动开发·成为全栈·go_router·路由设计·深链接
m0_738185829 小时前
Flutter 鸿蒙化实战:screen_protector 适配 OpenHarmony,防截屏/防泄露
flutter·华为·harmonyos·鸿蒙
梦想的颜色14 小时前
【编程实战】AI 时代 APP 开发全栈硬核指南:技术选型 + AI 架构 + 模型落地全维度决策
python·flutter·react native·react.js·ai·桌面应用·milvus
yuanlaile1 天前
2026年全新Flutter 跨平台开发全栈学习资料整理
flutter·getx·鸿蒙 next·flutter开发鸿蒙·flutter入门实战·flutter全栈
恋猫de小郭2 天前
Genkit Dart 1.0 发布,Flutter 原生的 AI Agent 终于完整了
android·前端·flutter
第10086个小白2 天前
flutter桌面端打包方式(Inno Setup Compiler)版本
前端·flutter
陆断枫3 天前
Flutter 3.47 动画引擎盘点:Impeller 全平台,和那些"新特性"
flutter
程序员老刘3 天前
Flet 1.0 思路很好,可惜来晚了
flutter·ai编程·客户端
m0_738185823 天前
Flutter 鸿蒙化实战:qr_code_scanner_plus 适配 OpenHarmony,二维码扫描
flutter·华为·harmonyos·鸿蒙