gettext_parser
Dart로 gettext po 및 mo 파일을 분석하고 컴파일하는 플랫폼 독립적인 라이브러리
Dart로 gettext po 및 mo 파일을 분석하고 컴파일하는 플랫폼 독립적인 라이브러리
아래는 영문 원문 스냅샷입니다. 최신 내용은 GitHub에서 확인하세요.
Parse and compile gettext po and mo files with dart. Ported gettext-parser npm package to dartlang.
Add dependency to pubspec.yaml
dependencies:
gettext_parser: any
Import library:
import 'package:gettext_parser/gettext_parser.dart' as gettextParser;
Map translateTable = gettextParser.po.parse(
file.readAsStringSync(),
);
Map translateTable = gettextParser.mo.parse(
file.readAsBytesSync(),
);
String data = gettextParser.po.compile(
translateTable,
);
UInt8List data = gettextParser.mo.compile(
translateTable,
);
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);
Parsed data is always in unicode but the original charset of the file can
be found from the charset property.
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 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:
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.
MIT