ctl10n
ctl10n (compile time localization) provides you a simple way to embed messages
into binary file without embedding them into source. Internally, ctl10n generates
a simple macro_rules! macro tr!() from the provided a TOML file with strings.
Basic usage
Add ctl10n to your build-dependencies in your Cargo.toml.
If you want to use include_strings you'll need it in dependencies as well.
[]
= "example"
= "0.1"
= "2018"
[]
= "0.1.0"
[]
= "0.1.0"
Add the following to your build.rs:
use ctl10n;
This will generate the file $OUT_DIR/strings.rs from strings.toml.
The TOML file with strings must be a table where all values are strings. Example strings.toml:
= "Some message"
= "Some message with {arg}"
You should include strings.rs somewhere (for example, in lib.rs) to use the generated
macro. You can do this by calling the macro ctl10n::include_strings!() or manually,
using include!().
After including the macro it can be used like this:
use ctl10n;
include_strings!;
Output of this code (assuming strings.toml from above):
Some message
Some message with foobar
Some message with foobaz
Trying to use an unknown key or wrong format arguments is a compile-time error.
Multiple locales
You can use environment variables to provide a different locale at compile time:
use ctl10n;
use env;
LOCALE=de cargo build