npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

flutter-i18n-generator

v1.5.0

Published

Flutter国际化提取工具,提取项目中的”中文“替换为多语言并生成语种翻译文件。

Downloads

1,527

Readme

flutter-i18n-generator

介绍

Flutter 国际化提取工具, 基于显式定义$t()消息的国际化自动化方案。

安装

# 全局安装
npm install flutter-i18n-generator -g

# 或本地安装
npm install flutter-i18n-generator --save-dev

准备

使用 flutter_localizations库添加 Flutter 组件对其他语言的支持(默认仅提供英文), 使用 Intl 包实现国际化(提供多语言实现方法),intl_utils 包是 Dart 代码生成工具。

添加依赖

在 pubspec.yaml 文件中添加必要依赖:

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations: # 添加 Flutter 本地化支持
    sdk: flutter
  intl: ^0.20.2 # 添加 intl 包

dev_dependencies:
  flutter_test:
    sdk: flutter
  intl_utils: ^2.8.14 # 使用IDE插件 Flutter Intl 自动生成则忽略

添加标记函数

在项目中添加国际化标记文件 lib/i18n_marker.dart,内容如下:

/// 国际化标记函数
String $t(String template, [Map<String, Object>? args]) {
  if (args == null) return template;
  String result = template;
  for (final entry in args.entries) {
    result = result.replaceAll('{${entry.key}}', '${entry.value}');
  }
  return result;
}

/// 动态翻译覆盖层(--dynamic-i18n 有效)
class Translation {
  static Map<String, String> _overrides = {};

  static String t(String dValue, String key, [Map<String, Object>? args]) {
    final template = _overrides[key];
    return template != null ? $t(template, args) : dValue;
  }

  static void apply(Map<String, dynamic> remoteJson) {
    _overrides = remoteJson.map((key, value) => MapEntry(key, value.toString()));
  }
}

命令行参数

extractor [options]

Options:
  -d, --dir <dir>            扫描的目录路径,默认 lib/
  -f, --file <path>          仅处理指定的单个 .dart 文件
  -o, --output <dir>         扫描结果输出路径,默认 lib/l10n/
  -e, --exclude <value...>   排除的文件/目录,如 models generated
  -l, --locale <value...>    生成的语言代码(符合BCP47标准),首个为主语言
  -t, --translate <value...> 需要翻译的语言代码,对应 --locale 参数
  -c, --channel <value>      翻译渠道 baidu(百度)|youdao(有道)|google(谷歌)
  --appKey <value>           翻译 API 应用 key
  --secretKey <value>        翻译 API 密钥
  --unique-key               是否碰撞时追加自增序号
  --dynamic-i18n             是否动态翻译覆盖
  --dry-run                  是否预览模式
  • --dry-run 模式说明:

    • 预览模式:仅输出匹配结果和翻译文件不修改源文件。适合初次评估项目规模。
    • 写入模式:输出匹配结果和翻译文件并且修改源文件。,用于生产打包部署前执行。
  • 备注:

    • 百度和有道翻译需同时提供 --appKey 和 --secretKey 参数。
    • 谷歌翻译提供 --appKey时,使用谷歌API翻译,未提供则降级为谷歌网页翻译。
    • 静态翻译只对新增 key 有效,需重新翻译可删除多语言文件中的 key 后重新生成。

命令行使用

# 全量扫描
extractor

# 预览模式,不修改扫描文件
extractor --dry-run

# 百度翻译
extractor -l zh en -t en -c baidu --appKey 你的appid --secretKey 你的密钥

公共多语言

在根目录创建translator.json 内容如下,适用于高频率出现的中文。

{
   "com_yes": "是",
   "com_no": "否",
   "com_save_success": "保存成功",
   "com_copy_success": "复制成功"
 }

工作原理

1. 标记文本

在 Dart 代码中使用 $t() 包裹需要国际化的字符串:

import 'package:you_app_name/i18n_marker.dart';

// 简单文本
Text($t('消息设置'))

// 带插值参数(使用 ARB 的 {param} 占位语法)
Text($t('共{count}条', {'count': total}))

2. 运行工具

extractor -l zh en

运行工具会将源代码中的 $t() 调用替换为 Flutter Intl 的标准调用方式:

// 替换前
Text($t('消息设置'))

// 替换后
Text(S.current.t5eb63bbb)

同时自动管理 import 语句:

  • 移除 import 'package:you_app_name/i18n_marker.dart'(开发阶段标记工具)
  • 添加 import 'package:you_app_name/generated/l10n.dart'(S 类所在的包)

并会为每个 locale 生成对应的 .arb 文件,默认添加到 lib/l10n/ 目录下 。

3. 生成 Dart 代码

flutter pub run intl_utils:generate

它会扫描 pubspec.yaml 中的配置和 lib/l10n/ 目录下的 .arb 文件,在 lib/generated/ 目录下生成对应的 Dart 本地化代码。

动态翻译代码示例


 void main() async {
   WidgetsFlutterBinding.ensureInitialized();
   // 模拟启动时拉取一次覆盖数据
   final remoteJson = await _fetchRemoteTranslations('zh');
   Translation.apply(remoteJson);
   runApp(const MyApp());
 }

  // 替换为真实接口调用
 Future<Map<String, dynamic>> _fetchRemoteTranslations(String locale) async {
   return {
     'welcomeMessage': '欢迎回来(动态更新)',
     'greeting': '你好,{name}!',
   };
 }