Next: Go, Previous: librep, Up: Individual Programming Languages [Contents][Index]
rust, rust-cargo
rustc, cargo
rs
"abc", r"abc", r#"abc"# etc.
—
gettext, ngettext
textdomain function
bindtextdomain function
setlocale function
$ cargo add gettext-rs
use gettextrs::*;
Note: We recommend the ‘gettext-rs’ crate.
We do not recommend the ‘gettext’ crate, because
(as of 2025) it does not handle
catalog fallback (e.g. from de_AT to de)
nor the LANGUAGE environment variable.
use
xgettext
There are three common ways of doing string formatting in Rust:
format!, println!, etc.
This facility supports only constant strings, known at compile-time.
Thus it cannot be used with translated format strings.
You would get an error such as
“error: format argument must be a string literal”.
strfmt library.
The facility cannot be recommended,
because it does not support the case where
some of the values are strings and some of the values are numbers
(without an excessive amount of contortions).
formatx library.
This is the one we recommend.
So, you have to convert the format!, println!, etc.
invocations to use formatx.
For example,
println!("Hello {}, you got {} coins.", name, left);
becomes
println!("{}", formatx!(gettext("Hello {}, you got {} coins."),
name, left)
.unwrap());
For swapped positions, a translator may translate
"Hello {}, you got {} coins."
with
"Hello, {1} coins are left for you, {0}."
fully portable
—
An example is available in the examples directory: hello-rust.
Next: Go, Previous: librep, Up: Individual Programming Languages [Contents][Index]