Skip to main content

Crate utopia_locale

Crate utopia_locale 

Source
Expand description

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.

PHPRust
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 $fallbackLocale::default, Locale::fallback; assigned unchecked with Locale::with
$locale->getText($key, $default, $placeholders)Locale::text, $default as Missing
Locale::DEFAULT_DYNAMIC_KEYDEFAULT_DYNAMIC_KEY
$locale->getTranslations()Locale::translations
\Exception, \TypeErrorError::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.

Structs§

Languages
Every registered language, and whether missing languages and keys are errors.
Locale
A view of Languages with a default and an optional fallback language.

Enums§

Error
Why a lookup or a language registration failed.
Missing
What Locale::text returns for a key no language has.

Constants§

DEFAULT_DYNAMIC_KEY
A marker text that, passed as Missing::Text, means Missing::Key.

Type Aliases§

Translations
Translations of one language, in file order.