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

插件详情v0.2.0

gettext_parser

与平台无关的库,用于使用 Dart 解析和编译 gettext po 和 mo 文件

AndroidiOSLinuxmacOSWebWindows
近 30 天下载
4.7万
喜欢数
4
Pub 分数
130

安装插件

flutter pub add gettext_parser

插件介绍

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

一个与平台无关的 Dart 库,用于解析和编译 gettext .po 和 .mo 文件,可在 Flutter 及其他 Dart 应用中实现强大的本地化和国际化功能。它支持读取翻译目录、提取消息以及生成编译后的二进制 .mo 文件。该包非常适合在 Android、iOS、Linux、macOS、Web 和 Windows 平台之间管理多语言内容的开发者。依赖项极少,API 简单明了,可无缝集成到现有工作流程中。对于需要精确控制翻译文件处理而无需依赖外部工具的项目尤其有用。

适合场景

  • 解析 .po 文件以提取翻译
  • 将 .po 文件编译为 .mo 二进制文件
  • 与 Flutter 应用集成以支持多语言
  • 在CI/CD流水线中管理翻译
  • 支持桌面和网页本地化

主要特点

  • 支持 .po 和 .mo 文件格式
  • 跨平台兼容性
  • 用于解析和编译的简单 API
  • 无外部依赖
  • 高效的消息提取和编码

适合对象

  • 需要国际化支持的Flutter开发者
  • 使用 gettext 进行翻译的团队
  • 面向多个平台的项目
  • 涉及翻译文件的构建自动化
  • 开发者避免使用运行时翻译库

使用建议

  • 需要手动处理翻译加载
  • 不是完整的国际化框架
  • 仅限文件解析/编译
  • 无内置回退或区域设置切换
  • 最好与自定义 i18n 层一起使用

原始 README

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

Dart gettext mo/po parser

Parse and compile gettext po and mo files with dart. Ported gettext-parser npm package to dartlang.

Usage

Add dependency to pubspec.yaml

dependencies:
    gettext_parser: any

Import library:

import 'package:gettext_parser/gettext_parser.dart' as gettextParser;
Parse .po files:
Map translateTable = gettextParser.po.parse(
    file.readAsStringSync(),
);
Parse .mo files:
Map translateTable = gettextParser.mo.parse(
    file.readAsBytesSync(),
);
Compile .po files:
String data = gettextParser.po.compile(
    translateTable,
);
Compile .mo files:
UInt8List data = gettextParser.mo.compile(
    translateTable,
);

Encoding

gettext_parser use Encoding interface for encoding and decoding charsets from dart:convert package with utf8, base64, latin1 built-in encoders. If you need other encoding you could implement Encoding interface by your own.

Example:

gettextParser.mo.parse(buffer, encoding: latin1);
gettextParser.po.parse(buffer, encoding: latin1);

Data structure of parsed mo/po files

Character set

Parsed data is always in unicode but the original charset of the file can be found from the charset property.

Headers

Headers can be found from the headers object, all keys are lowercase and the value for a key is a string. This value will also be used when compiling.

Translations

Translations can be found from the translations object which in turn holds context objects for msgctxt. Default context can be found from translations[""].

Context objects include all the translations, where msgid value is the key. The value is an object with the following possible properties:

  • msgctxt context for this translation, if not present the default context applies
  • msgid string to be translated
  • msgid_plural the plural form of the original string (might not be present)
  • msgstr an array of translations
  • comments an object with the following properties: translator, reference, extracted, flag, previous.

Example

{
  "charset": "iso-8859-1",

  "headers": {
    "content-type": "text/plain; charset=iso-8859-1",
    "plural-forms": "nplurals=2; plural=(n!=1);"
  },

  "translations": {
    "": {
      "": {
        "msgid": "",
        "msgstr": ["Content-Type: text/plain; charset=iso-8859-1\n..."]
      }
    },
    "another context": {
      "%s example": {
        "msgctxt": "another context",
        "msgid": "%s example",
        "msgid_plural": "%s examples",
        "msgstr": ["% näide", "%s näidet"],
        "comments": {
          "translator": "This is regular comment",
          "reference": "/path/to/file:123"
        }
      }
    }
  }
}

Notice that the structure has both a headers object and a "" translation with the header string. When compiling the structure to a mo or a po file, the headers object is used to define the header. Header string in the "" translation is just for reference (includes the original unmodified data) but will not be used when compiling. So if you need to add or alter header values, use only the headers object.

License

MIT

所属分类

源码仓库

使用此插件的项目

所在榜单