Hello,
build a hello world with gettext-ng
so that show the output dependent on LANG Variable
but this did not work on Windows… why ?
The *.mo files are in the same diretory
de_DE.UTF-8.mo
en_US.UTF-8.mo
fr_FR.UTF-8.mo
Linux
$ export RUST_BACKTRACE=full
$ ./hello_world
Filename: en_US.UTF-8.mo
Hello World!
Windows
C:\Users\admin\Desktop\hello_world>set RUST_BACKTRACE=full
C:\Users\admin\Desktop\hello_world>hello_world.exe
thread ‘main’ panicked at ‘called Result::unwrap()
on an Err
value: NotPresent’, src/main.rs:4:43
stack backtrace:
0: 0x7ff78c054ee2 -
1: 0x7ff78c017d6b -
2: 0x7ff78c031957 -
3: 0x7ff78c05842d -
4: 0x7ff78c0581ca -
5: 0x7ff78c058974 -
6: 0x7ff78c05859e -
7: 0x7ff78c0584f9 -
8: 0x7ff78c0584e4 -
9: 0x7ff78c016ce5 -
10: 0x7ff78c0184d3 -
11: 0x7ff78c0158d4 -
12: 0x7ff78c0116d0 -
13: 0x7ff78c015d03 -
14: 0x7ff78c0113ae -
15: 0x7ff78c0114e6 -
16: 0x7ffdaf707034 -
17: 0x7ffdb0b426a1 -
cargo new hello_world
cd hello_world
cargo add gettext-ng
cargo.toml
[profile.release]
opt-level = ‘z’ # Optimize for size
lto = true # Enable link-time optimization
codegen-units = 1 # Reduce number of codegen units to increase optimizations
panic = ‘abort’ # Abort on panic
strip = true # Strip symbols from binary*
vi src/main.rs
use std::fs::File;
use gettext_ng::Catalog;
fn main() {
let lang_code = std::env::var(“LANG”).unwrap();
let filename = format!("{}.mo", lang_code);
let f = File::open(filename).expect(“could not open the catalog”);
let catalog = Catalog::parse(f).expect(“could not parse the catalog”);// Will print out the French translation // if it is found in the parsed file // or "Name" otherwise. println!("{}", catalog.gettext("Name"));
}
vi en_US.UTF-8.po
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"PO-Revision-Date: 2023-09-08 21:39+0200\n"
"Last-Translator: developer <developer@w541>\n"
"Language-Team: English\n"
"Language: en_US\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=ASCII\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
msgid "Name"
msgstr "Hello World!"
vi de_DE.UTF-8.po
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"PO-Revision-Date: 2023-09-08 21:46+0200\n"
"Last-Translator: developer <developer@w541>\n"
"Language-Team: German <translation-team-de@lists.sourceforge.net>\n"
"Language: de\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=ASCII\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
msgid "Name"
msgstr "Hallo Welt!"
vi fr_FR.UTF-8.po
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"PO-Revision-Date: 2023-09-08 21:34+0200\n"
"Last-Translator: developer <developer@w541>\n"
"Language-Team: French <traduc@traduc.org>\n"
"Language: fr\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=ASCII\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
msgid "Name"
msgstr "Bonjour le monde!"
msgfmt en_US.UTF-8.po -o en_US.UTF-8.mo
msgfmt de_DE.UTF-8.po -o de_DE.UTF-8.mo
msgfmt fr_FR.UTF-8.po -o fr_FR.UTF-8.mocargo run
export LANG=de_DE.UTF-8
cargo run
export LANG=fr_FR.UTF-8
cargo run
build for windows
cargo build --target x86_64-pc-windows-gnu --release
build for linux
cargo build --release