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.
| 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.
Structs§
- Languages
- Every registered language, and whether missing languages and keys are errors.
- Locale
- A view of
Languageswith a default and an optional fallback language.
Enums§
- Error
- Why a lookup or a language registration failed.
- Missing
- What
Locale::textreturns for a key no language has.
Constants§
- DEFAULT_
DYNAMIC_ KEY - A marker text that, passed as
Missing::Text, meansMissing::Key.
Type Aliases§
- Translations
- Translations of one language, in file order.