localize_it – Simple and fast library for localization
A tiny, zero-allocation localization system for Rust designed with #![no_std] support.
This crate provides a macro-based API for defining compile-time locales and localized string expressions without using dynamic memory, hash maps, or runtime lookups. All localized strings are stored in fixed-size arrays, and the active locale is stored in an atomic variable, making access extremely fast and thread-safe.
Features
- O(1) localization lookup
- Zero allocations
no_stdcompatible- Thread-safe global locale (
AtomicUsize) - Macro-based API
- No external dependencies
Usage
Add the following to your Cargo.toml:
[]
= "1.0.0"
Example
The recommended usage pattern is to create a dedicated locale module that contains locale initialization and grouped expression modules. For example:
src/
├─ main.rs
└─ locale/
├─ mod.rs # Locale initialization
├─ error.rs # Expression package for error messages
└─ ui.rs # Expression package for UI elements
// mod.rs
use init_locale;
// Initialize locale with two languages
init_locale!;
// error.rs
use crateExpression;
// Expressions can be created manually without the macro
pub const COOKIE_BUTTON: Expression = ;
// ui.rs
use crateLocale;
use expression;
// Recommended way to create expressions using the macro
expression!;
// main.rs (or wherever you want to use the localized strings)
use crateerror;
use crateui;
use crate;
use localize;
How It Works
- Locales are defined as an
enumwith#[repr(usize)] - Each localized string is compiled into a fixed-size
&'static strarray - The active locale is stored as an atomic integer
- Localization resolves to a simple array index operation
- Invalid locale values safely fall back to the first locale variant
License
This project is licensed under either of
at your option.