Skip to main content

Crate larust_lang

Crate larust_lang 

Source
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_with resolve against right now - the current request’s override if set_current_locale was called inside an established with_locale_scope, else Config::app_locale - falling back further still to [DEFAULT_LOCALE] if Application::new() was never called at all (larust_core::config() panics in that case; try_config() doesn’t). A validation rule (see larust-validation, the first real caller of t_or) has to keep working in a bare unit test that never sets up an Application - 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” contract larust_view::push_registry::record already has).
t
key looked up against the current locale, falling back to Config::app_fallback_locale, falling back to key itself 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 to default - not the bare key - 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 - unlike t, 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 from params - the same relationship t_with has to t.
t_with
t, substituting :name-style placeholders from params - Laravel’s own __('messages.greeting', ['name' => 'Alice']) convention (:name in the translated string, not {name}/{{name}}).
with_locale_scope
Establishes a fresh, unset locale-override scope around fut - call once per HTTP request (see larust_http::locale::negotiate, which wraps this together with its own session lookup). Opt-in, unlike larust_view::push_registry’s own always-on scope: current_locale already degrades correctly with no scope established at all (falls back to Config::app_locale), so there’s nothing to lose by only paying for this where an app actually wires locale negotiation in.