Flutter语言处理插件plural_lint的使用

plural_lint简介

plural_lint 是一个用于检查项目中多语言翻译文件(ARB文件)中复数形式翻译是否完整的开发者工具。它根据Unicode CLDR复数规则检测每个区域语言的缺失或未使用的复数数量。



安装plural_lint

plural_lint 基于 custom_lint 构建。要使用它,可以选择以下两种方式之一:

方法1:在 pubspec.yaml 中添加依赖

dev_dependencies:
  custom_lint:
  plural_lint:

方法2:通过命令行安装

flutter pub add --dev custom_lint plural_lint

接下来,在项目的 analysis_options.yaml 文件中启用 custom_lint 分析器插件:

analyzer:
  plugins:
    - custom_lint

所有lint规则

缺失的复数形式 (missing_quantity)

此规则会检测是否缺少某些必需的复数形式翻译。例如,在英语(en)中,需要提供 oneother 的翻译;而在波兰语(pl)中,则需要 onefewmanyother 的翻译。如果缺少这些必需的复数形式,plural_lint 会发出警告。

示例:

在英语的 ARB 文件中:

{
  "things": "{count, plural, other{things}}"
}

将触发警告:

warning: These quantities: [one] are missing for locale: en (missing_quantity at [app] lib/l10n/intl_en.arb:3)

未使用的复数形式 (unused_quantity)

此规则会检测是否有未使用的复数形式翻译。例如,在英语(en)中,fewmany 的复数形式永远不会被使用。尽管如此,dartintl 包支持所有语言的 zeroonetwo 复数形式,即使它们不在CLDR规则中。

示例:

在英语的 ARB 文件中:

{
  "things2": "{count, plural, one{thing} few{things} many{things} other{things}}"
}

将触发警告:

info: These quantities: [few, many] are not used in locale: en (unused_quantity at [app] lib/l10n/intl_en.arb:4)

数据来源

该包嵌入了来自 CLDR复数规则 的数据,该文件来源于官方 Unicode CLDR存储库

禁用特定规则

默认情况下,所有规则都会启用。可以通过修改 analysis_options.yaml 文件禁用特定规则:

custom_lint:
  rules:
    - unused_quantity: false
    - missing_quantity: false

从终端或CI运行plural_lint

自定义的 lint 规则可能不会出现在 dart analyze 中。为了解决这个问题,可以使用以下命令手动运行 custom_lint

dart run custom_lint

示例代码

以下是 plural_lint 的示例项目,展示了如何使用该插件。

示例项目结构

example/
├── analysis_options.yaml
├── pubspec.yaml
└── lib/
    └── l10n/
        ├── intl_en.arb
        └── intl_pl.arb

示例代码:pubspec.yaml

name: plural_lint_example
description: A sample project for plural_lint.

version: 1.0.0

environment:
  sdk: '>=2.12.0 <3.0.0'

dev_dependencies:
  custom_lint:
  plural_lint:

示例代码:analysis_options.yaml

analyzer:
  plugins:
    - custom_lint

示例代码:intl_en.arb

{
  "@[@locale](/user/locale)": "en",
  "things": "{count, plural, one{thing} other{things}}",
  "things2": "{count, plural, one{thing} few{things} many{things} other{things}}"
}

示例代码:intl_pl.arb

{
  "@[@locale](/user/locale)": "pl",
  "things": "{count, plural, one{thing} few{things} many{things} other{things}}"
}
1 回复

更多关于Flutter语言处理插件plural_lint的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


plural_lint 是一个用于 Flutter 的静态分析工具,它可以帮助开发者在代码中检测并修复与复数形式相关的字符串资源问题。这个插件特别适用于需要支持多语言的应用程序,因为它可以确保复数形式的字符串资源在不同语言环境下都能正确显示。

安装 plural_lint

首先,你需要在 pubspec.yaml 文件中添加 plural_lint 作为开发依赖项:

dev_dependencies:
  plural_lint: ^1.0.0

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

使用 plural_lint

plural_lint 通常与 flutter analyze 命令一起使用,它会自动检测代码中的复数形式字符串资源问题。

1. 在代码中使用复数形式字符串

在 Flutter 中,复数形式的字符串通常定义在 intl 包中。例如:

import 'package:intl/intl.dart';

String getPluralString(int count) {
  return Intl.plural(
    count,
    zero: 'No items',
    one: 'One item',
    other: '$count items',
    name: 'getPluralString',
    args: [count],
  );
}

2. 运行 plural_lint

你可以通过运行以下命令来使用 plural_lint 进行静态分析:

flutter analyze

plural_lint 会自动检测代码中的复数形式字符串资源,并报告任何潜在的问题。

3. 修复问题

如果 plural_lint 报告了问题,你需要根据提示修复代码。例如,如果某个复数形式的字符串资源缺少 zeroother 分支,你需要添加这些分支。

配置 plural_lint

你可以在 analysis_options.yaml 文件中配置 plural_lint 的行为。例如,你可以启用或禁用某些规则,或者调整规则的严格程度。

analyzer:
  plugins:
    - plural_lint
  strong-mode:
    implicit-casts: false
    implicit-dynamic: false
  errors:
    missing_plural_branch: error
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!