cloudiful-bevy-localization
Reusable Bevy localization runtime for apps that generate their own static locale registry at build time.
What it provides
LocalizationDefinition: static description of fallback locale, supported locales, locale sources, and declared keysLocaleSource: one TOML payload bound to a locale + namespaceLocale: runtime locale handle with serialization supportTextKey: runtime text key handle with serialization supportLocalization: Bevy resource for lookup, formatting, locale switching, and table accessLocalizationPlugin: registers a definition and inserts theLocalizationresourceLocalizationLoadError: structured load/validation failureregister_definition(...): definition registry hook used by the plugin and helper typeslocale_name_key_id(...): helper forcommon.locale_name.<locale>key generation
What it does not provide
- scanning downstream
assets/i18n - generating app-specific key constants
- embedding downstream
OUT_DIRartifacts inside the crate - editor tooling, extraction, or translation workflows
This crate expects the downstream app to generate or hand-author a static registry and then pass that definition into the plugin.
Usage
Generate a static registry in the downstream app and pass it into the plugin.
LocalizationPlugin::new(...) registers the definition first and then inserts
the Localization resource built from that definition.
use *;
use ;
const KEYS: & = &;
const SOURCES: & = &;
static LOCALIZATION: LocalizationDefinition = LocalizationDefinition ;
At runtime, use the resource for lookup, formatting, locale switching, and locale display text:
use *;
use ;
Validation Rules
Definitions are validated at load time. A load fails when any of these rules are broken:
- the fallback locale table is missing
- a listed locale has no table
- a declared
TextKeyis missing from any locale - a
common.locale_name.<locale>entry is missing for any supported locale - a locale table contains keys that are not declared
- placeholder names differ from the fallback locale for the same key
- a locale source references a locale not listed in
LocalizationDefinition - a TOML value is not a string or nested table
- the same flattened key appears more than once
LocaleSource.contents TOML is flattened by namespace, so:
[]
= "Nested"
under namespace common becomes common.nested.label.
Runtime Behavior
Localization::text(...)looks up the current locale first, then the fallback locale, and panics only if both are missingLocalization::format_text(...)performs simple{name}replacement on the selected textLocalization::lookup(...)andlookup_id(...)returnOption<&str>without panickingLocalization::table(...)returns the flattened table for one localeLocalization::locale_display_text(...)looks upcommon.locale_name.<locale>in the current locale, then fallback locale, and finally returns the raw locale id
Locale and Key Helpers
Locale::new(...)andTextKey::new(...)create static handlesLocale::available()reads available locales from the active registered definitionLocale::from_serialized(...)normalizes case and punctuation when matching serialized locale idsTextKey::from_id(...)resolves an id against the active registered definition