搜尋套件、儲存庫與 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

所屬分類

原始碼儲存庫

使用此套件的專案

所在榜單