Locale
RustIn Appwrite#
Appwrite runs the Rust crate: the PHP extension serves the library and none of its PHP is left.
Rust 720 lines (463 code)split
| Part | Code | Comments | Blank | Total | Files |
|---|---|---|---|---|---|
| Rust source | 463 | 201 | 56 | 720 | 1 |
Comments include doc comments. Each side counts its src/.
Served from Rust (1)
Utopia\Locale\LocaleInstall#
cargo add utopia-localeGetting started#
Common tasks with Locale, each a complete program you can copy and run.
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 utopia_locale::{Languages, Locale, Missing, Translations};
let mut languages = Languages::new();
languages.insert(
"en-US",
Translations::from_iter([
("hello".to_owned(), "Hello".to_owned()),
("world".to_owned(), "World".to_owned()),
]),
);
let locale = Locale::new(&languages, "en-US")?;
assert_eq!(locale.text("hello", Missing::Key, &[])?.as_deref(), Some("Hello"));
assert_eq!(locale.text("world", Missing::Key, &[])?.as_deref(), Some("World"));use Utopia\Locale\Locale;
Locale::setLanguageFromArray('en-US', [
'hello' => 'Hello',
'world' => 'World',
]);
$locale = new Locale('en-US');
echo $locale->getText('hello'), "\n"; // Hello
echo $locale->getText('world'), "\n"; // WorldFill 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 utopia_locale::{Languages, Locale, Missing, Translations};
let mut languages = Languages::new();
languages.insert(
"en-US",
Translations::from_iter([(
"likes".to_owned(),
"You have {{likes}} likes and {{comments}} comments.".to_owned(),
)]),
);
let locale = Locale::new(&languages, "en-US")?;
let text = locale.text("likes", Missing::Key, &[("likes", "12"), ("comments", "55")])?;
assert_eq!(text.as_deref(), Some("You have 12 likes and 55 comments."));
let text = locale.text("likes", Missing::Key, &[("likes", "12")])?;
assert_eq!(text.as_deref(), Some("You have 12 likes and {{comments}} comments."));use Utopia\Locale\Locale;
Locale::setLanguageFromArray('en-US', [
'likes' => 'You have {{likes}} likes and {{comments}} comments.',
]);
$locale = new Locale('en-US');
echo $locale->getText('likes', placeholders: ['likes' => 12, 'comments' => 55]), "\n"; // You have 12 likes and 55 comments.
echo $locale->getText('likes', placeholders: ['likes' => 12]), "\n"; // You have 12 likes and {{comments}} comments.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 utopia_locale::{Languages, Locale, Missing, Translations};
let mut languages = Languages::new();
languages.insert(
"en-US",
Translations::from_iter([
("hello".to_owned(), "Hello".to_owned()),
("world".to_owned(), "World".to_owned()),
]),
);
languages.insert("he-IL", Translations::from_iter([("hello".to_owned(), "שלום".to_owned())]));
let mut locale = Locale::new(&languages, "he-IL")?;
locale.set_fallback("en-US")?;
assert_eq!(locale.text("hello", Missing::Key, &[])?.as_deref(), Some("שלום"));
assert_eq!(locale.text("world", Missing::Key, &[])?.as_deref(), Some("World"));
assert_eq!(locale.fallback(), Some("en-US"));use Utopia\Locale\Locale;
Locale::setLanguageFromArray('en-US', ['hello' => 'Hello', 'world' => 'World']);
Locale::setLanguageFromArray('he-IL', ['hello' => 'שלום']);
$locale = new Locale('he-IL');
$locale->setFallback('en-US');
echo $locale->getText('hello'), "\n"; // שלום
echo $locale->getText('world'), "\n"; // World
echo $locale->fallback, "\n"; // en-USShow 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 utopia_locale::{Languages, Locale, Missing, Translations};
let mut languages = Languages::new();
languages.insert("en-US", Translations::from_iter([("hello".to_owned(), "Hello".to_owned())]));
languages.set_exceptions(false);
let locale = Locale::new(&languages, "en-US")?;
assert_eq!(locale.text("missing", Missing::Key, &[])?.as_deref(), Some("{{missing}}"));
assert_eq!(locale.text("missing", Missing::Text("A custom text"), &[])?.as_deref(), Some("A custom text"));
assert_eq!(locale.text("missing", Missing::Null, &[])?, None);
let text = locale.text("missing", Missing::Text("Sorry {{name}}, missing text"), &[("name", "Matej")])?;
assert_eq!(text.as_deref(), Some("Sorry Matej, missing text"));use Utopia\Locale\Locale;
Locale::setLanguageFromArray('en-US', ['hello' => 'Hello']);
Locale::$exceptions = false;
$locale = new Locale('en-US');
echo $locale->getText('missing'), "\n"; // {{missing}}
echo $locale->getText('missing', default: 'A custom text'), "\n"; // A custom text
var_dump($locale->getText('missing', default: null)); // NULL
echo $locale->getText('missing', 'Sorry {{name}}, missing text', ['name' => 'Matej']), "\n"; // Sorry Matej, missing textLoad 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 utopia_locale::{Languages, Locale, Missing};
let path = std::env::temp_dir().join("utopia-locale-guide-hi-IN.json");
std::fs::write(&path, r#"{"hello": "Namaste", "world": "Duniya"}"#)?;
let mut languages = Languages::new();
languages.load("hi-IN", &path)?;
languages.insert_json("en-US", br#"{"hello": "Hello"}"#)?;
let locale = Locale::new(&languages, "hi-IN")?;
assert_eq!(locale.text("world", Missing::Key, &[])?.as_deref(), Some("Duniya"));
assert_eq!(languages.names().collect::<Vec<_>>(), ["hi-IN", "en-US"]);use Utopia\Locale\Locale;
$path = sys_get_temp_dir() . '/utopia-locale-guide-hi-IN.json';
file_put_contents($path, '{"hello": "Namaste", "world": "Duniya"}');
Locale::setLanguageFromJSON('hi-IN', $path);
Locale::setLanguageFromArray('en-US', ['hello' => 'Hello']);
$locale = new Locale('hi-IN');
echo $locale->getText('world'), "\n"; // Duniya
echo implode(', ', Locale::getLanguages()), "\n"; // hi-IN, en-USSwitch 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 utopia_locale::{Languages, Locale, Missing, Translations};
let mut languages = Languages::new();
languages.insert(
"en-US",
Translations::from_iter([("hello".to_owned(), "Hello".to_owned()), ("bye".to_owned(), "Bye".to_owned())]),
);
languages.insert("he-IL", Translations::from_iter([("hello".to_owned(), "שלום".to_owned())]));
let requested = "he-IL";
let mut locale = Locale::new(&languages, requested)?;
assert_eq!(locale.default(), "he-IL");
assert_eq!(locale.text("hello", Missing::Key, &[])?.as_deref(), Some("שלום"));
locale.set_default("en-US")?;
let all = locale.translations()?;
assert_eq!(all.len(), 2);
assert_eq!(all.keys().collect::<Vec<_>>(), ["hello", "bye"]);use Utopia\Locale\Locale;
Locale::setLanguageFromArray('en-US', ['hello' => 'Hello', 'bye' => 'Bye']);
Locale::setLanguageFromArray('he-IL', ['hello' => 'שלום']);
$requested = 'he-IL';
$locale = new Locale($requested);
echo $locale->default, "\n"; // he-IL
echo $locale->getText('hello'), "\n"; // שלום
$locale->setDefault('en-US');
$all = $locale->getTranslations();
echo count($all), "\n"; // 2
echo implode(', ', array_keys($all)), "\n"; // hello, byeCatch 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 utopia_locale::{Error, Languages, Locale, Missing, Translations};
let mut languages = Languages::new();
languages.insert("en-US", Translations::from_iter([("hello".to_owned(), "Hello".to_owned())]));
assert!(!languages.contains("fr-FR"));
let error = Locale::new(&languages, "fr-FR").unwrap_err();
assert_eq!(error, Error::Exception("Locale not found".into()));
let locale = Locale::new(&languages, "en-US")?;
let error = locale.text("missing", Missing::Key, &[]).unwrap_err();
assert_eq!(error.to_string(), r#"Key named "missing" not found"#);use Utopia\Locale\Locale;
Locale::setLanguageFromArray('en-US', ['hello' => 'Hello']);
var_dump(in_array('fr-FR', Locale::getLanguages())); // bool(false)
try {
new Locale('fr-FR');
} catch (\Exception $e) {
echo $e->getMessage(), "\n"; // Locale not found
}
$locale = new Locale('en-US');
try {
$locale->getText('missing');
} catch (\Exception $e) {
echo $e->getMessage(), "\n"; // Key named "missing" not found
}Reference#
Translate text into the user's language, with a fallback language and
{{placeholder}} substitution.
Register every language once in a Languages value, from maps or JSON
files, then create a Locale per request that borrows it and looks up
texts with Locale::text. Whether a missing language or key is an
error is a setting of the Languages.
PHP: ports utopia-php/locale. PHP keeps every language, and whether
missing keys throw, in static state shared by all Locale objects; here
that state is the Languages value.
| PHP | Rust |
|---|---|
Locale::$language (static) | Languages |
Locale::$exceptions (static) | Languages::set_exceptions, Languages::exceptions |
Locale::setLanguageFromArray($name, $translations) | Languages::insert |
Locale::setLanguageFromJSON($name, $path) | Languages::load (reading) and Languages::insert_json (decoding) |
Locale::getLanguages() | Languages::names |
new Locale($default) | Locale::new |
$locale->setDefault($name), setFallback($name) | Locale::set_default, Locale::set_fallback |
public $default, public $fallback | Locale::default, Locale::fallback; assigned unchecked with Locale::with |
$locale->getText($key, $default, $placeholders) | Locale::text, $default as Missing |
Locale::DEFAULT_DYNAMIC_KEY | DEFAULT_DYNAMIC_KEY |
$locale->getTranslations() | Locale::translations |
\Exception, \TypeError | Error::Exception, Error::Type |
PHP: translations are strings. PHP stores whatever json_decode returns for a
translation file; a file that is not JSON (or is unreadable) becomes a
language PHP cannot use, and so it is here: lookups in it fail with the
same TypeError (see Error::Type). A file whose values are not all
strings is refused (Error::Unsupported), a recorded deviation.
API#
What utopia_locale exports at its root.