File: gettext.info, Node: Rust, Next: Go, Prev: librep, Up: List of Programming Languages 16.5.12 Rust ------------ RPMs rust, rust-cargo Ubuntu packages rustc, cargo File extension ‘rs’ String syntax ‘"abc"’, ‘r"abc"’, ‘r#"abc"#’ etc. gettext shorthand -- gettext/ngettext functions ‘gettext’, ‘ngettext’ textdomain ‘textdomain’ function bindtextdomain ‘bindtextdomain’ function setlocale ‘setlocale’ function Prerequisite ‘$ 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 or emulate GNU gettext use Extractor ‘xgettext’ Formatting with positions There are three common ways of doing string formatting in Rust: • Using the built-ins ‘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". • Using the ‘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). • Using the ‘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}."’ Portability fully portable po-mode marking -- An example is available in the ‘examples’ directory: ‘hello-rust’.
