gettext 1.1.0+2

  • Readme
  • Changelog
  • Example
  • Installing
  • 88

Dart Gettext #

A Dart implementation of gettext, a localization framework.

Ported node-gettext package to dart.

Also works perfectly with flutter.

Features #

  • Supports domains, contexts and plurals
  • Ships with plural forms for 136 languages
  • Change locale or domain on the fly
  • Emits events for internal errors, such as missing translations

Usage #

import "package:gettext/gettext.dart";

final gt = new Gettext(onWarning: print);

Load messages from .json file:

new File("./en_US.json").readAsString().then(
   (data) {
       final gt = new Gettext();
       gt.addLocale(json.decode(data));
   }
);

Load messages from .mo or .po files using gettext_parser:

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

File("./en_US.mo").readAsString().then(
   (data) {
       final gt = new Gettext();
       gt.addLocale(gettextParser.mo.parse(data));
   }
);

Json file content example:

{
  "charset": "utf-8",
  "headers": {
    "mime-version": "1.0",
    "content-type": "text/plain; charset=utf-8",
    "content-transfer-encoding": "8bit",
    "language": "es-ES",
    "plural-forms": "nplurals=2; plural=(n!=1);"
  },
  "translations": {
    "": {
      "Hello": {
        "msgid": "Hello",
        "comments": {
           "translator": "Normal string"
        },
        "msgstr": [
           "Hola"
        ]
      },
      "An apple": {
        "msgid": "1 apple",
        "comments": {
           "translator": "Plural string"
        },
        "msgstr": [
           "una manzana",
           "%d manzanas"
        ]
      }
    }
  }
}

Change locale:

gt.locale = "en_US";

Change default domain:

gt.domain = "types";

Translate messages:

gt.gettext("Hello");

Translate messages with plural forms:

gt.ngettext("An apple", "%d apples", 3); // returns "%d apples"

See example

Usage with flutter #

Use flutter_gettext package.

API #

  • addLocale(Map<String, dynamic> locale, {String domain: 'messages'})
  • addTranslations(String localeName, Map<String, dynamic> translations, {String domain: 'messages'})
  • setCatalog(String localeName, Catalog catalog)
  • gettext(String msgid, {String domain, String context}) → String
  • ngettext(String msgid, String msgplural, int count, {String domain, String context}) → String

1.1.0 #

  • Breaking change. Method addLocale(Map<String, dynamic> locale, {String domain: 'messages'}) changed to addLocale(Map<String, dynamic> locale, {String domain: 'messages'}).
  • Add setCatalog and addLocale methods

1.0.1 #

  • Add changelog and flutter example

1.0.0 #

  • First implementation

example/main.dart

import 'dart:convert';
import 'dart:io';

import "package:gettext/gettext.dart";

void main() async {
  final data = await File("./en_US.json").readAsString();

  final gt = new Gettext();
  gt.addLocale(json.decode(data));

  print(gt.gettext("Hello"));
  print(gt.ngettext("An apple", "%d apples", 1));
  print(gt.ngettext("An apple", "%d apples", 3));
}

Use this package as a library

1. Depend on it

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


dependencies:
  gettext: ^1.1.0+2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:gettext/gettext.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
76
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
88
Learn more about scoring.

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

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

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

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
test any