Flutter国际化翻译检查插件intl_translation_linter的使用

Flutter国际化翻译检查插件intl_translation_linter的使用

简介

Intl Translation Linter 是一个为 Flutter 设计的自定义检查插件,用于帮助捕获代码中缺失的翻译字符串。它确保所有字符串都使用了 intl pub 包的 S 变量进行本地化,从而提高应用的可访问性和国际化能力。

安装

要安装 Intl Translation Linter,请将其作为依赖项添加到你的 pubspec.yaml 文件中:

dev_dependencies:
  custom_lint: ^latest_version
  intl_translation_linter: ^1.0.3

然后运行 flutter pub get 来获取该包。

配置

要使用 Intl Translation Linter,你需要在 analysis_options.yaml 文件中包含它:

analyzer:
  plugins:
    - custom_lint

custom_lint:
  rules:
    - intl_translation_linter_use_s_role: true # 启用或 false 禁用

这将启用由 Intl Translation Linter 提供的检查规则。

使用

安装并配置后,Intl Translation Linter 将自动检查你的代码是否存在缺少翻译的字符串。如果字符串未使用 intl_translation 包的 S 变量进行本地化,此检查器将在 IDE 中发出警告。

示例代码

以下是示例代码:

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'generated/l10n.dart';

void main() {
  runApp(const MyApp());
}

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo', // 此示例未使用 S 会在 IDE 中警告
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'), // 此示例未使用 S 会在 IDE 中警告
      localizationsDelegates: const [
        S.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: S.delegate.supportedLocales,
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title), // 此示例未使用 S 会在 IDE 中警告
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text(
              'not translated text',
            ), // 此示例未使用 S 会在 IDE 中警告
            // ignore: intl_translation_linter_use_s_role
            const Text(
              'Still not translated text',
            ), // 此示例未使用 S 会不警告在 IDE 中
            Text('translated text'.trim()), // 此示例未使用 S 但调用了其他字符串操作会在 IDE 中警告
            Text(S.of(context).translated_text), // 此示例使用 S.of(context) 不会在 IDE 中警告
            Text(S.current.translated_text), // 此示例使用 S.current 不会在 IDE 中警告
          ],
        ),
      ),
    );
  }
}

更多关于Flutter国际化翻译检查插件intl_translation_linter的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter国际化翻译检查插件intl_translation_linter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


intl_translation_linter 是一个用于检查 Flutter 项目中 intl_translation 生成的 .arb 文件格式和内容的静态分析工具。它可以帮助开发者确保国际化文件的一致性和正确性,从而减少在运行时可能出现的错误。

安装 intl_translation_linter

首先,你需要在你的 Flutter 项目中添加 intl_translation_linter 作为开发依赖项。打开 pubspec.yaml 文件,并在 dev_dependencies 部分添加以下内容:

dev_dependencies:
  intl_translation_linter: ^1.0.0

然后运行 flutter pub get 来安装依赖。

配置 intl_translation_linter

intl_translation_linter 通常通过一个配置文件来定义检查规则。你可以在项目根目录下创建一个 intl_translation_linter.yaml 文件来配置它。

一个简单的配置文件示例可能如下所示:

rules:
  - missing_translation
  - unused_translation
  - duplicate_translation
  - invalid_placeholder

运行 intl_translation_linter

安装并配置好 intl_translation_linter 后,你可以通过以下命令来运行它:

flutter pub run intl_translation_linter
回到顶部