gettext 1.0.1

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

Dart Gettext

A Dart implementation of gettext, a localization framework.

Rewritten to dart of node-gettext package.

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 data from json:

new File("./en_US.json").readAsString().then(
   (data) {
       final json = json.decode(data["translations"]);
       final gt = new Gettext();
       gt.addTranslations('en_US', Translations.fromJSON(json));
   }
);

Json content:

{
  "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"

API

  • gettext(String msgid, {String domain, String context}) → String
  • ngettext(String msgid, String msgplural, int count, {String domain, String context}) → String

Working with Flutter

import 'package:flutter/widgets.dart';
import 'package:sprintf/sprintf.dart';
import 'package:gettext/gettext.dart';

class GettextLocalizations {
  final Gettext gt;

  GettextLocalizations(this.gt);

  static GettextLocalizations of(BuildContext context) {
    return Localizations.of<GettextLocalizations>(
        context, GettextLocalizations);
  }

  String __(
      String msgid, [
        List args = const [],
      ]) {
    return sprintf(gt.gettext(msgid), args);
  }

  String __n(
      String msgid,
      String msgidPlural,
      int count, [
        List arg = const [],
      ]) {
    return sprintf(
      gt.ngettext(msgid, msgidPlural, count),
      <dynamic>[count]..addAll(arg),
    );
  }
}

class GettextLocalizationsDelegate
    extends LocalizationsDelegate<GettextLocalizations> {
  final gettext = new Gettext();

  @override
  bool isSupported(Locale locale) => true;

  @override
  bool shouldReload(LocalizationsDelegate<GettextLocalizations> old) => false;

  @override
  Future<GettextLocalizations> load(Locale locale) async {
    final data = await rootBundle.loadString("l10n/$locale.json");

    final jsonData = json.decode(data);

    gettext.locale = locale.toString();
    gettext.addTranslations(locale.toString(), Translations.fromJson(jsonData));

    return GettextLocalizations(gettext);
  }
}

See full example

1.0.1

Added changelog and flutter example

1.0.0

First implementation

example/README.md

example

A new Flutter project.

Getting Started

For help getting started with Flutter, view our online documentation.

Use this package as a library

1. Depend on it

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


dependencies:
  gettext: ^1.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/gettext.dart';
  
Version Uploaded Documentation Archive
1.0.1 Nov 11, 2018 Go to the documentation of gettext 1.0.1 Download gettext 1.0.1 archive
1.0.0 Nov 11, 2018 Go to the documentation of gettext 1.0.0 Download gettext 1.0.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
47
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
70
Learn more about scoring.

We analyzed this package on Dec 5, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:gettext/gettext.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.

Dependencies

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