Expand description
Localization - Laravel’s __('messages.welcome')/resources/lang/,
narrowed to what a translation actually needs: a flat, per-locale
key -> string map, :name-style placeholder substitution, and a
current-locale/fallback-locale chain. No @lang(...) Blade directive -
deliberately: t(key)/t_with(key, params) are plain functions, so
{{ t("messages.welcome") }} already works through the existing
{{ }} expression mechanism (any real Rust expression), with zero
changes needed to larust-view’s parser/AST/codegen - the same
“explicit Rust expression, no new template syntax” reasoning
@can/@role already established for permission checks. A dedicated
directive is a reasonable later addition, not a blocker for this to be
useful today.
§Translation files
resources/lang/{locale}.json, one flat object per locale (dot-
namespaced keys, e.g. "messages.welcome", are just an ordinary key -
there’s no real per-file namespacing the way Laravel’s PHP-array style
has, keeping the loader a single, simple format instead of two):
{ "messages.welcome": "Welcome to :app!" }Read once, lazily, from disk (CWD-relative - the same convention
.env loading already uses elsewhere in this framework, not
AppPaths-aware yet) and cached for the rest of the process - an app
with no resources/lang/ directory at all still works, every t
call simply returns its own key unchanged (Laravel’s own __()
behavior for a missing translation - never a blank string).
§Pluralization
A translated template containing | is treated as Laravel’s own
trans_choice syntax, chosen by a :count param - see t_with’s own
doc comment for the two supported forms. Automatic, not a separate
function: a plain string (no |) behaves exactly as it always has.
§Current locale
current_locale reads a task-local override if one’s been set (see
with_locale_scope/set_current_locale - larust_http::locale ::negotiate sets this from the session, once wired in), falling back
to Config::app_locale otherwise. An app that never wires the
negotiation middleware at all still gets a fully working single-locale
t/t_with - the scope only matters for per-request overrides.
Functions§
- current_
locale - The locale
t/t_withresolve against right now - the current request’s override ifset_current_localewas called inside an establishedwith_locale_scope, elseConfig::app_locale- falling back further still to [DEFAULT_LOCALE] ifApplication::new()was never called at all (larust_core::config()panics in that case;try_config()doesn’t). A validation rule (seelarust-validation, the first real caller oft_or) has to keep working in a bare unit test that never sets up anApplication- the exact scenario this guards. - set_
current_ locale - Overrides the current request’s locale for the rest of
with_locale_scope’s own future - a no-op outside an established scope (the same “inert outside a scope” contractlarust_view::push_registry::recordalready has). - t
keylooked up against the current locale, falling back toConfig::app_fallback_locale, falling back tokeyitself unchanged - Laravel’s own__('messages.welcome')behavior exactly, including “never a blank string” for a genuinely missing translation.- t_or
- Like
t, but falls back todefault- not the barekey- when neither the current nor the fallback locale has a translation for it. For a caller that already has its own sensible, hardcoded message (a framework-shipped validation rule, say) and wants it to stay overridable by a real translation file without an unresolved, literal key ever reaching a user whose app hasn’t defined one - unliket, where an app that deliberately wants “no translation yet” to be visibly obvious relies on the key itself showing through. - t_
or_ with t_or, substituting:name-style placeholders fromparams- the same relationshipt_withhas tot.- t_with
t, substituting:name-style placeholders fromparams- Laravel’s own__('messages.greeting', ['name' => 'Alice'])convention (:namein the translated string, not{name}/{{name}}).- with_
locale_ scope - Establishes a fresh, unset locale-override scope around
fut- call once per HTTP request (seelarust_http::locale::negotiate, which wraps this together with its own session lookup). Opt-in, unlikelarust_view::push_registry’s own always-on scope:current_localealready degrades correctly with no scope established at all (falls back toConfig::app_locale), so there’s nothing to lose by only paying for this where an app actually wires locale negotiation in.