localize_it - Simple and fast localization library
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.
You can manually control (via init_locale) the locale state or use the built-in locale storage (via init_locale_with_storage).
Quick Example
With built-in locale storage:
use ;
init_locale_with_storage!;
expression!;
With manual control:
use ;
init_locale!;
expression!;
Features
- O(1) localization lookup
- Zero allocations
- Macro-based API
- No external dependencies
no_stdcompatible
Design Constraints
- Not possible to update or add the translations without recompiling
- No plans to add automatic gender agreement, numeral declension, etc.
Example With Recommended Project Structure
The recommended usage pattern is to create a dedicated locale module that contains locale initialization and grouped expression modules.
src/
├─ main.rs
└─ locale/
├─ mod.rs # Locale initialization
├─ error.rs # Expression module for error messages
└─ ui.rs # Expression module for UI elements
// mod.rs
use init_locale_with_storage;
// Initialize locale with two languages
init_locale_with_storage!;
// error.rs
use Expression;
use expression;
// Create expression using the macro
expression!;
// ui.rs
use Locale;
use expression;
// Create another expression using the macro
expression!;
// main.rs (or wherever you want to use the localized strings)
use crate;
use localize;
Usage
Add the following to your Cargo.toml:
[]
= "1.2.1"
How It Works
- Locales are defined as an
enumwith#[repr(usize)] - Expressions as an array of
&'static str - Built-in locale storage implemented as
AtomicUsizewithRelaxedordering - Localization resolves to a simple array index operation
License
This project is licensed under either of
at your option.