fluent-locale 0.5.0

A library for language and locale negotiation.
Documentation

Fluent Locale

Fluent Locale is a library for language and locale identifier negotiation.

crates.io Build Status Coverage Status

Introduction

This is a Rust implementation of fluent-locale library which is a part of Project Fluent.

The library uses unic-langid and unic-locale to retrieve and operate on Unicde Language and Locale Identifiers. The library provides algorithm for negotiating between lists of locales.

Usage

use fluent_locale::negotiate::NegotiationStrategy;
use fluent_locale::negotiate_languages;

let supported = negotiate_languages(
  &["de-DE", "fr-FR", "en-US"],
  &["de-DE", "de-AT", "fr-CA", "fr", "en-GB", "en", "en-US", "it"],
  Some("en-US"),
  &NegotiationStrategy::Filtering
);

See docs.rs for more examples.

Status

The implementation is in early stage, but is complete according to fluent-locale corpus of tests, which means that it parses, serializes and negotiates as expected.

The remaining work is on the path to 1.0 is to gain in-field experience of using it, add more tests and ensure that bad input is correctly handled.

Compatibility

The API is based on [UTS 35][] definition of [Unicode Locale Identifier][] and is aiming to parse and serialize all locale identifiers according to that definition.

Note: Unicode Locale Identifier is similar, but different, from what BCP47 specifies under the name Language Tag. For most locale management and negotiation needs, the Unicode Locale Identifier used in this crate is likely a better choice, but in some case, like HTTP Accepted Headers, you may need the complete BCP47 Language Tag implementation which this crate does not provide.

Parsed locale identifiers are stored as Locale objects compatible with ECMA402's Intl.Locale and allow for operations on locale identifier subtags and unicode extension keys as defined by RFC6067 and Unicode UTS35

Language negotiation algorithms are custom Project Fluent solutions, based on RFC4647.

The current API only allows for operations on basic language subtags (language, script, region, variants) and unicode extension keys. Other subtags will be parsed and serialized, but there is no API access to them when operating on the Locale object.

The language negotiation strategies aim to replicate the best-effort matches with the most limited amount of data. The algorithm returns reasonable results without any database, but the results can be improved with either limited or full CLDR likely-subtags database.

The result is a balance chosen for Project Fluent and may differ from other implementations of language negotiation algorithms which may choose different tradeoffs.

Alternatives

Although Fluent Locale aims to stay close to W3C Accepted Languages, it does not aim to implement the full behavior and some aspects of the language negotiation strategy recommended by W3C, such as weights, are not a target right now.

For such purposes, rust-language-tags crate seems to be a better choice.

Performance

There has not been a significant performance work being done on the library yet, so we expect there are some low hanging fruit waiting for someone to find them.

At the moment performance is comparable to previously mentioned language-tags crate for parsing a sample list of language tags based on this crate's benchmark code:

running 2 tests
test bench_locale(fluent-locale)  ... bench:       1,773 ns/iter (+/- 48)
test bench_locale(language-tags) ... bench:       1,982 ns/iter (+/- 280)

Develop

cargo build
cargo test
cargo bench