gettext_parser 0.0.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new48

Dart gettext mo/po parser

Parse and compile gettext po and mo files with dart. Port of npm package gettext-parser.

Readiness

  • [x] Mo parse
  • [ ] Mo compile
  • [x] Po parse
  • [ ] Po compile

Usage

Add dependency to pubspec.yaml

dependencies:
    gettext_parser: any

Import library:

import 'package:gettext_parser/gettext_parser.dart' as gettextParser;

Parse Mo Files:

Map translateTable = gettextParser.mo.parse(
    file.readAsBytesSync(),
);

Parse Po Files:

Map translateTable = gettextParser.po.parse(
    file.readAsStringSync(),
);

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.parseRaw(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

0.0.1

  • Initial version

example/README.md

Parse Mo Files:

import 'package:gettext_parser/gettext_parser.dart' as gettextParser;

Map translateTable = gettextParser.mo.parse(
    file.readAsBytesSync(),
);

// with custom encoding
Map translateTable = gettextParser.mo.parse(
    file.readAsBytesSync(),
    encoding: latin1,
);

Parse Po Files:

import 'package:gettext_parser/gettext_parser.dart' as gettextParser;

Map translateTable = gettextParser.mo.parse(
    file.readAsStringSync()
);

// with custom encoding
Map translateTable = gettextParser.mo.parseRaw(
    file.readAsBytesSync(),
    encoding: latin1,
);

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  gettext_parser: ^0.0.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:gettext_parser/gettext_parser.dart';
  
Version Uploaded Documentation Archive
0.0.1 Jan 12, 2019 Go to the documentation of gettext_parser 0.0.1 Download gettext_parser 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
9
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
70
Overall:
Weighted score of the above. [more]
48
Learn more about scoring.

We analyzed this package on Jan 15, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.10

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:gettext_parser/gettext_parser.dart.

Health issues and suggestions

Document public APIs. (-1 points)

3 out of 3 API elements (library, class, field or method) have no adequate dartdoc content. Good documentation improves code readability and discoverability through search.

Fix lib/src/mo/parser.dart. (-0.50 points)

Analysis of lib/src/mo/parser.dart reported 1 hint:

line 25 col 7: The value of the field '_revision' isn't used.

Format lib/src/utils/parse_header.dart.

Run dartfmt to format lib/src/utils/parse_header.dart.

Maintenance suggestions

The description is too short. (-20 points)

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Package is pre-v0.1 release. (-10 points)

While there is nothing inherently wrong with versions of 0.0.*, it usually means that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
path ^1.6.2
test ^1.3.4