Code Monkey home page Code Monkey logo

linguini's Introduction

.NET Nuget Nuget Nuget Nuget

Linguini

Linguini is a C# implementation of Project Fluent, a localization system for natural-sounding translations with features like:

Asymmetric Localization

Natural-sounding translations with genders and grammatical cases only when necessary. Expressiveness is not limited by the grammar of the source language.

Progressive Enhancement

Translations are isolated; locale-specific logic doesn't leak to other locales. Authors can iteratively improve translations without impact on other languages.

Modular

Linguini is highly modular. You only can use the parts you need. Need just parsing? Get Linguni.Syntax. Need only Plural Rules data? Get PluralRules.Generator and connect to XML CLDR Plural rules data.

Performant

Linguini uses a zero-copy parser to parse the resources. While at the moment, there are no benchmarks, it is used by RobustToolbox as as a localization framework.

How to get it?

To install the Fluent Bundle type in your console:

dotnet add package Linguini.Bundle

You can also follow other NuGet installation instructions.

How to use it?

For a 2-minute tour of Linguini, add this to your C# code:

var bundler = LinguiniBuilder.Builder()
    .CultureInfo(new CultureInfo("en"))
    .AddResource("hello-user =  Hello, { $username }!")
    .UncheckedBuild();

var message = bundler.GetAttrMessage("hello-user",  ("username", (FluentString)"Test"));
Assert.AreEqual("Hello, Test!", message);

The 10 min tour - What do the lines mean?

Let's go line by line and see how LinguiniBuilder works.

  1. Init - This creates a LinguiniBuilder using a type-safe builder pattern.

    var bundler = LinguiniBuilder.Builder()
  2. Set the language - a translation bundle must have a language to translate to. In this case, we choose the English language.

    .CultureInfo(new CultureInfo("en"))
  3. Add a resource - a translation bundle without resources is pointless. We choose inline for ease of example, for passing files checking documentation or source code.

        .AddResource("hello-user =  Hello, { $username }!")
  4. Complete the ResourceBundle - We call UncheckedBuild() to convert a builder to a bundle. Bundle will parse its resources and report errors. Since we don't care about errors, we are fine with ResourceBundle throwing errors.

        .UncheckedBuild();
    
  5. Get hello-user term where username is Test.

    bundler.GetAttrMessage("hello-user",  ("username", (FluentString)"Test"));

Quick questions and answers

FluentBundle isn't thread-safe.

Making it concurrent could add a performance penalty; otherwise, a concurrent bundle would be the default. To make it thread-safe, add UseConcurrent() in builder:

    var bundler = LinguiniBuilder.Builder()
       .CultureInfo(new CultureInfo("en"))
       .AddResource("hello-user =  Hello, { $username }!")
       .UseConcurrent()
       .UncheckedBuild();

Or set UseConcurrent = true in FluentBundleOption passed to the factory method:

    var x = new FluentBundleOption()
    {
        UseConcurrent = true,
    };
    var bundle = FluentBundle.MakeUnchecked(defaultBundleOpt);

Isn't .ftl FreeMarker Template language?

No, it can also be Fluent, a localization system by Mozilla.

What syntax does Fluent Localization use?

For more details, see Fluent syntax guide.

Licenses

Linguini Bundle and associated projects are licensed under Apache and MIT licenses. Consult the LICENSE-MIT and LICENSE-APACHE for more detail.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.