gettext

Creator: coderz1093

Last updated:

Add to Cart

Description:

gettext

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);
copied to clipboard
Load messages from .json file:
new File("./en_US.json").readAsString().then(
(data) {
final gt = new Gettext();
gt.addLocale(json.decode(data));
}
);
copied to clipboard
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));
}
);
copied to clipboard
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"
]
}
}
}
}
copied to clipboard
Change locale:
gt.locale = "en_US";
copied to clipboard
Change default domain:
gt.domain = "types";
copied to clipboard
Translate messages:
gt.gettext("Hello");
copied to clipboard
Translate messages with plural forms:
gt.ngettext("An apple", "%d apples", 3); // returns "%d apples"
copied to clipboard
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

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Customer Reviews

There are no reviews.