Utopia Locale
Translations: languages, default and fallback locales, and placeholders.
This library is maintained by the Appwrite team.
Getting started
[]
= { = "https://github.com/appwrite/appwrite" }
Utopia Locale keeps an application's translations. You register each language as a set of keys and texts, pick a default language, and look up texts by key, with placeholders and a fallback language for keys that are not translated yet. In Rust the registered languages live in a Languages value that you load once and share; each request creates a cheap Locale that borrows it. This guide covers the common tasks.
Translate a message
Register a language from key and text pairs, create a Locale with it as the default, and read texts by key.
use ;
let mut languages = new;
languages.insert;
let locale = new?;
assert_eq!;
assert_eq!;
Fill in placeholders
Mark a variable part of a text as {{name}} and pass its value when you read the text. A placeholder you do not pass stays in the text unchanged.
use ;
let mut languages = new;
languages.insert;
let locale = new?;
let text = locale.text?;
assert_eq!;
let text = locale.text?;
assert_eq!;
Fall back to a complete language
A new language is often only partly translated. Set a fallback language that has every key, and keys the default language lacks are read from it.
use ;
let mut languages = new;
languages.insert;
languages.insert;
let mut locale = new?;
locale.set_fallback?;
assert_eq!;
assert_eq!;
assert_eq!;
Show something for missing translations
By default a key no language has is an error. Turn exceptions off to get a placeholder instead: {{key}}, your own text, or nothing. Your own text can hold placeholders too.
use ;
let mut languages = new;
languages.insert;
languages.set_exceptions;
let locale = new?;
assert_eq!;
assert_eq!;
assert_eq!;
let text = locale.text?;
assert_eq!;
Load translations from files
Keep each language in a JSON file of keys and texts. load reads a file; insert_json takes JSON you already have in memory.
use ;
let path = temp_dir.join;
write?;
let mut languages = new;
languages.load?;
languages.insert_json?;
let locale = new?;
assert_eq!;
assert_eq!;
Switch the language for each request
Load languages once at startup. For each request, create a Locale with the language the user asked for, or change the default of an existing one. translations returns every text of the current default, for example to send to a client.
use ;
let mut languages = new;
languages.insert;
languages.insert;
let requested = "he-IL";
let mut locale = new?;
assert_eq!;
assert_eq!;
locale.set_default?;
let all = locale.translations?;
assert_eq!;
assert_eq!;
Catch unknown languages and keys
With exceptions on, the default, an unknown language or a key no language has fails with an error. Check a language with contains before you use it, or handle the error and fall back.
use ;
let mut languages = new;
languages.insert;
assert!;
let error = new.unwrap_err;
assert_eq!;
let locale = new?;
let error = locale.text.unwrap_err;
assert_eq!;
Contributing
Development happens in appwrite/appwrite, where this crate lives at crates/utopia/locale. Run these commands from a checkout of it. The contributing guide covers each step.
Test
Compare with PHP
The utopia PHP extension serves this crate as Utopia\Locale\Locale, which no longer has a PHP implementation.
License
BSD-3-Clause. See LICENSE.