搜索插件、代码仓与 APP 分类

插件详情v2.8.17

intl_utils

intl_utils 是一个 Dart 库,可从 ARB 文件生成 Dart 本地化代码。生成的代码依赖于 Intl 库。

AndroidiOSmacOSWindowsLinux
近 30 天下载
36万
喜欢数
243
Pub 分数
150

安装插件

flutter pub add intl_utils

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

intl_utils 是一个 Dart 库,可从 ARB(App Resource Bundle)文件自动生成本地化代码,利用 Intl 库实现国际化。它通过将本地化字符串转换为类型安全的 Dart 代码,简化了翻译工作流程,减少了手动操作和潜在错误。适用于需要多语言支持的 Flutter 项目,可与现有的本地化设置无缝集成,并支持所有主要平台。该工具通过将翻译与源代码同步,提升了开发人员的生产力。

适合场景

  • 自动化本地化代码生成
  • 将 ARB 文件集成到 Flutter 应用中
  • 管理多语言内容
  • 简化翻译工作流程
  • 支持 CI/CD 本地化流水线

主要特点

  • 生成类型安全的本地化类
  • 在底层使用 Intl 库
  • 支持 ARB 文件输入
  • 简化翻译管理
  • 启用平台级本地化

适合对象

  • 使用 ARB 文件的 Flutter 开发人员
  • 管理多种语言的团队
  • 频繁更新翻译的项目
  • CI/CD 集成的本地化工作流
  • 面向全球受众的应用

使用建议

  • 需要 ARB 文件格式正确
  • 依赖 Intl 库以实现运行时功能
  • 初始配置需要手动设置
  • 最好与结构化翻译工作流程一起使用
  • 仅限基于 ARB 的本地化

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Intl Utils

pub package Twitter Follow

Dart package that creates a binding between your translations from .arb files and your Flutter app. It generates boilerplate code for official Dart Intl library and adds auto-complete for keys in Dart code.

Usage

You can use this package directly (i.e. for Continuous Integration tools or via CLI) or leave it to Visual Studio Code or IntelliJ/Android Studio plugins to run it automatically whenever you modify ARB files.

Follow these steps to get started:

Configure package

Add package configuration to your pubspec.yaml file. Here is a full configuration for the package:

flutter_intl:
  enabled: true # Required. Must be set to true to activate the package. Default: false
  class_name: S # Optional. Sets the name for the generated localization class. Default: S
  main_locale: en # Optional. Sets the main locale used for generating localization files. Provided value should consist of language code and optional script and country codes separated with underscore (e.g. 'en', 'en_GB', 'zh_Hans', 'zh_Hans_CN'). Default: en
  arb_dir: lib/l10n # Optional. Sets the directory of your ARB resource files. Provided value should be a valid path on your system. Default: lib/l10n
  output_dir: lib/generated # Optional. Sets the directory of generated localization files. Provided value should be a valid path on your system. Default: lib/generated
  use_deferred_loading: false # Optional. Must be set to true to generate localization code that is loaded with deferred loading. Default: false
  localizely: # Optional settings if you use Localizely platform. Read more: https://localizely.com/blog/flutter-localization-step-by-step/?tab=automated-using-flutter-intl
    project_id: # Get it from the https://app.localizely.com/projects page.
    branch: # Get it from the “Branches” page on the Localizely platform, in case branching is enabled and you want to use a non-main branch.
    upload_overwrite: # Set to true if you want to overwrite translations with upload. Default: false
    upload_as_reviewed: # Set to true if you want to mark uploaded translations as reviewed. Default: false
    upload_tag_added: # Optional list of tags to add to new translations with upload (e.g. ['new', 'New translation']). Default: []
    upload_tag_updated: # Optional list of tags to add to updated translations with upload (e.g. ['updated', 'Updated translation']). Default: []
    upload_tag_removed: # Optional list of tags to add to removed translations with upload (e.g. ['removed', 'Removed translation']). Default: []
    download_empty_as: # Set to empty|main|skip, to configure how empty translations should be exported from the Localizely platform. Default: empty
    download_include_tags: # Optional list of tags to be downloaded (e.g. ['include', 'Include key']). If not set, all string keys will be considered for download. Default: []
    download_exclude_tags: # Optional list of tags to be excluded from download (e.g. ['exclude', 'Exclude key']). If not set, all string keys will be considered for download. Default: []
    ota_enabled: # Set to true if you want to use Localizely Over-the-air functionality. Default: false
Add ARB files

Add one ARB file for each locale you need to support in your Flutter app. Add them to lib/l10n folder inside your project, and name them in a following way: intl_<LOCALE_ISO_CODE>.arb.
For example: intl_en.arb or intl_en_GB.arb. You can also change the ARB folder from lib/l10n to a custom directory by adding the arb_dir line in your pubspec.yaml file.

If you wonder how to format key-values content inside ARB files, here is detailed explanation.

Run command

To generate boilerplate code for localization, run the generate program inside directory where your pubspec.yaml file is located:

  dart run intl_utils:generate

This will produce files inside lib/generated directory. You can also change the output folder from lib/generated to a custom directory by adding the output_dir line in your pubspec.yaml file.

Integration with Localizely
Upload main ARB file
  dart run intl_utils:localizely_upload_main [--project-id <PROJECT_ID> --api-token <API_TOKEN> --arb-dir <ARB_DIR> --main-locale <MAIN_LOCALE> --branch <BRANCH> --[no-]upload-overwrite --[no-]upload-as-reviewed] --upload-tag-added <UPLOAD_TAG_ADDED> --upload-tag-updated <UPLOAD_TAG_UPDATED> --upload-tag-removed <UPLOAD_TAG_REMOVED>

This will upload your main ARB file to Localizely.
All args are optional. If not provided, the intl_utils will use configuration from the pubspec.yaml file or default values (check the Configure package section for more details).

Download ARB files
  dart run intl_utils:localizely_download [--project-id <PROJECT_ID> --api-token <API_TOKEN> --arb-dir <ARB_DIR> --download-empty-as <DOWNLOAD_EMPTY_AS> --download-include-tags <DOWNLOAD_INCLUDE_TAGS> --download-exclude-tags <DOWNLOAD_EXCLUDE_TAGS> --branch <BRANCH>]

This will download all available ARB files from the Localizely platform and put them under arb-dir folder.
All args are optional. If not provided, the intl_utils will use configuration from the pubspec.yaml file or default values (check the Configure package section for more details).

Notes:
Argument project-id can be omitted if pubspec.yaml file contains project_id configuration under flutter_intl/localizely section.
Argument api-token can be omitted if ~/.localizely/credentials.yaml file contains api_token configuration (e.g. api_token: xxxxxx).
Optional argument arb-dir has the value lib/l10n as default and needs only to be set, if you want to place your ARB files in a custom directory.

所属分类

依赖

依赖清单 9 项

源码仓库

使用此插件的项目