All Projects → VitaliiTsilnyk → Ngettext

VitaliiTsilnyk / Ngettext

Licence: mit
A cross-platform .NET implementation of the GNU/Gettext library.

Projects that are alternatives of or similar to Ngettext

msgtools
Tools for Developing Diagnostic Messages
Stars: ✭ 18 (-89.53%)
Mutual labels:  i18n, l10n, gettext
Weblate
Web based localization tool with tight version control integration.
Stars: ✭ 2,719 (+1480.81%)
Mutual labels:  i18n, l10n, gettext
Node Gettext
A JavaScript implementation of gettext, a localization framework.
Stars: ✭ 175 (+1.74%)
Mutual labels:  i18n, l10n, gettext
wp-l10n-validator
Gettext localization validator for WordPress
Stars: ✭ 17 (-90.12%)
Mutual labels:  i18n, l10n, gettext
gettext-extractor
A flexible and powerful Gettext message extractor with support for JavaScript, TypeScript, JSX and HTML.
Stars: ✭ 82 (-52.33%)
Mutual labels:  i18n, l10n, gettext
Kotsu
✨ Clean, opinionated foundation for new projects — to boldly go where no man has gone before
Stars: ✭ 48 (-72.09%)
Mutual labels:  i18n, l10n, gettext
Glotpress Wp
🌍 🌎 🌏 GlotPress is a WordPress plugin to let you set up your own collaborative, web-based software translation tool.
Stars: ✭ 205 (+19.19%)
Mutual labels:  i18n, l10n, gettext
stone.js
gettext-like client-side Javascript Internationalization Library
Stars: ✭ 20 (-88.37%)
Mutual labels:  i18n, l10n, gettext
polib
Pure python library to manipulate, create, modify gettext files (pot, po and mo files).
Stars: ✭ 34 (-80.23%)
Mutual labels:  i18n, l10n, gettext
Gotext
Go (Golang) GNU gettext utilities package
Stars: ✭ 292 (+69.77%)
Mutual labels:  i18n, l10n, gettext
Django Rosetta
Rosetta is a Django application that eases the translation process of your Django projects
Stars: ✭ 806 (+368.6%)
Mutual labels:  i18n, gettext
Translation
The Translation component provides tools to internationalize your application.
Stars: ✭ 6,196 (+3502.33%)
Mutual labels:  i18n, l10n
Fluent.js
JavaScript implementation of Project Fluent
Stars: ✭ 622 (+261.63%)
Mutual labels:  i18n, l10n
Gettext
PHP library to collect and manipulate gettext (.po, .mo, .php, .json, etc)
Stars: ✭ 578 (+236.05%)
Mutual labels:  i18n, gettext
Webfactoryicutranslationbundle
Enables ICU message formatting for translations in Symfony applications.
Stars: ✭ 27 (-84.3%)
Mutual labels:  i18n, l10n
Aeiou
i18n scripts
Stars: ✭ 24 (-86.05%)
Mutual labels:  i18n, gettext
Corpus Christi
Church management suite, open source, fully internationalized
Stars: ✭ 29 (-83.14%)
Mutual labels:  i18n, l10n
Locale2
💪 Try as hard as possible to detect the client's language tag ("locale") in node or the browser. Browserify and Webpack friendly!
Stars: ✭ 65 (-62.21%)
Mutual labels:  i18n, l10n
Gettext Go
🆎 GNU gettext for Go (Imported By Kubernetes)
Stars: ✭ 66 (-61.63%)
Mutual labels:  i18n, gettext
Fluent Rs
Rust implementation of Project Fluent
Stars: ✭ 503 (+192.44%)
Mutual labels:  i18n, l10n

NGettext Build Status Build Status NuGet NuGet

A cross-platform .NET implementation of the GNU/Gettext library.

This fully managed library works fine on Microsoft .NET Framework version 2.0 or higher, Mono and .NET Core even on full-AOT runtimes. It is fully COM and CLS compatible.

This implementation loads translations directly from gettext *.mo files (no need to compile a satellite assembly) and can handle multiple translation domains and multiple locales in one application instance. NGettext supports both little-endian and big-endian MO files, automatic (header-based) encoding detection and (optional) plural form rules parsing.

By default, NGettext uses pre-compiled plural form rules for most known locales. You can enable plural form rule parsing from *.mo file headers (see MoCompilingPluralLoader description below) or use a custom plural rules passed to your Catalog instance through API.

Why NGettext?

There are other GNU/Gettext implementations for C#, but they all have some huge disadvantages.

Why not Mono.Unix.Catalog? Mono's Catalog is just a bindings to three native functions (bindtextdomain, gettext, and ngettext). It does not support multiple domains/locales and contexts. It is not cross-patform, it have problems with Windows OS.

Why not GNU.Gettext? It uses satellite assemblies as a translation files and does not support multiple locales in one application instance. It's hard to build and maintain translation files and change locale inside your application.

So why NGettext?

  • NGettext is fully cross-platform as it doesn't use any native or managed 3rd-party libraries.
  • NGettext supports multiple domains. You can separate translation files for each of your application's module or plugin.
  • NGettext supports multiple locales in one application instance and gives really simple API to choose locale of your application. You don't even need to care about locales of your application's threads.
  • NGettext loads translations from *.mo files. You can even load translations from specified file or stream.
  • NGettext supports message contexts.
  • NGettext provides nice and simple API, compatible with any type of application (console, GUI, web...).

Build status

OS Target frameworks (build) Target frameworks (test) Status
Windows net20 net35 net40 net45 net46
netstandard1.0 netstandard1.3 netstandard2.0
.NETPortable,Version=v4.0,Profile=Profile136
.NETPortable,Version=v4.0,Profile=Profile328
netcoreapp1.0
netcoreapp2.0
Build Status
Linux net20 net40 net45
netstandard1.0 netstandard1.3 netstandard2.0
.NETPortable,Version=v4.0,Profile=Profile136
.NETPortable,Version=v4.0,Profile=Profile328
netcoreapp1.0
netcoreapp2.0
Build Status
Mac OS X net20 net40 net45
netstandard1.0 netstandard1.3 netstandard2.0
.NETPortable,Version=v4.0,Profile=Profile136
.NETPortable,Version=v4.0,Profile=Profile328
netcoreapp2.0 Build Status

Installation and usage

All you need to do is just install a NuGet package from the package manager console:

PM> Install-Package NGettext

or through .NET CLI utility:

$ dotnet add package NGettext

Now you can use NGettext in your code:

	using NGettext;
	// This will load translations from "./locale/<CurrentUICulture>/LC_MESSAGES/Example.mo"
	ICatalog catalog = new Catalog("Example", "./locale");
	
	// or
	
	// This will load translations from "./locale/ru_RU/LC_MESSAGES/Example.mo"
	ICatalog catalog = new Catalog("Example", "./locale", new CultureInfo("ru-RU"));
	Console.WriteLine(catalog.GetString("Hello, World!")); // will translate "Hello, World!" using loaded translations
	Console.WriteLine(catalog.GetString("Hello, {0}!", "World")); // String.Format support

.NET CoreCLR

If you using this library under CoreCLR and you want to use encodings different from UTF-8 for your *.mo files, you need to include System.Text.Encoding.CodePages package into your application and initialize it like this:

	#if NETCOREAPP1_0
		Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
	#endif

Culture-specific message formatting

	// All translation methods support String.Format optional arguments
	catalog.GetString("Hello, {0}!", "World");
	
	// Catalog's current locale will be used to format messages correctly
	catalog.GetString("Here's a number: {0}!", 1.23);
	// Will return "Here's a number: 1.23!" for en_US locale
	// But something like this will be returned for ru_RU locale with Russian translation: "А вот и номер: 1,23!"

Plural forms

	catalog.GetPluralString("You have {0} apple.", "You have {0} apples.", count);
	// Returns (for en_US locale):
	//     "You have {0} apple." for count = 1
	//     "You have {0} apples." otherwise


	catalog.GetPluralString("You have {0} apple.", "You have {0} apples.", 5, 5);
	// Returns translated plural massage: "You have 5 apples." (for en_US locale)
	// First “5” used in plural forms determination; second — in String.Format method


	// Example plural forms usage for fractional numbers:
	catalog.GetPluralString("You have {0} apple.", "You have {0} apples.", (long)1.23, 1.23);
	// Internal String.Format will be used in context of catalog's locale and formats objects respectively

Contexts

	catalog.GetParticularString("Menu|File|", "Open"); // will translate message "Open" using context "Menu|File|"
	catalog.GetParticularString("Menu|Project|", "Open"); // will translate message "Open" using context "Menu|Project|"

Multiple locales and domains in one application instance

	// "./locale/en_US/LC_MESSAGES/Example.mo"
	ICatalog example_en = new Catalog("Example", "./locale", new CultureInfo("en-US"));

	// "./locale/fr/LC_MESSAGES/Example.mo"
	ICatalog example_fr = new Catalog("Example", "./locale", new CultureInfo("fr"));

	// "./locale/<CurrentUICulture>/LC_MESSAGES/AnotherDomainName.mo"
	ICatalog anotherDomain = new Catalog("AnotherDomainName", "./locale");

Direct MO file loading

	Stream moFileStream = File.OpenRead("path/to/domain.mo");
	ICatalog catalog = new Catalog(moFileStream, new CultureInfo("en-US"));

Parsing plural rules from the *.mo file header

NGettext can parse plural rules directly from the *.mo file header and compile it to a dynamic method in runtime. To enable this option you can just create a catalog using the MoCompilingPluralLoader from the NGettext.PluralCompile package:

	ICatalog catalog = new Catalog(new MoCompilingPluralLoader("Example", "./locale"));

This loader will parse plural formula from the *.mo file header and compile it to plural rule for your Catalog instance at runtime, just when your *.mo file loads. Your Catalog's *PluralString methods performance will be the same as if you were using NGettext's default precompiled plural rules, only *.mo file loading will be slightly slower.

This feature requires enabled JIT compiler in your runtime. You can not use MoCompilingPluralLoader in an full-AOT environment. This is why MoCompilingPluralLoader moved to a separate library.

For hosts without enabled JIT you can use MoAstPluralLoader which will only parse plural formulas to an abstract syntax tree and interpret it every time you call a *PluralString method from your catalog, without compiling. Please note that this solution is slightly slower than MoCompilingPluralLoader even it's pretty well optimized.

Custom plural formulas

	catalog.PluralRule = new PluralRule(numPlurals, n => ( n == 1 ? 0 : 1 ));

Also you can create custom plural rule generator by implementing IPluralRuleGenerator interface, which will create a PluralRule for any culture.

Debugging

Debug version of the NGettext binary outputs debug messages to System.Diagnostics.Trace. You can register trace listeners to see NGettext debug messages. Please note that Release version of the NGettext binary does not produse any trace messages.

	Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));

Shorter syntax

In doc/examples/T.cs you can see an example of shorter syntax creation for NGettext:

	T._("Hello, World!"); // GetString
	T._n("You have {0} apple.", "You have {0} apples.", count, count); // GetPluralString
	T._p("Context", "Hello, World!"); // GetParticularString
	T._pn("Context", "You have {0} apple.", "You have {0} apples.", count, count); // GetParticularPluralString

Poedit compatibility

For Poedit compatibility, you need to specify plural form in your *.pot file header, even for english language:

	"Plural-Forms: nplurals=2; plural=n != 1;\n"

And a keywords list:

	"X-Poedit-KeywordsList: GetString;GetPluralString:1,2;GetParticularString:1c,2;GetParticularPluralString:1c,2,3;_;_n:1,2;_p:1c,2;_pn:1c,2,3\n"
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].