Crates.io | polib |
lib.rs | polib |
version | 0.2.0 |
source | src |
created_at | 2022-01-15 19:19:11.720173 |
updated_at | 2023-03-11 08:04:09.525548 |
description | Read, manipulate and store translation data in GNU gettext PO format. |
homepage | |
repository | https://github.com/brettdong/polib |
max_upload_size | |
id | 514502 |
size | 67,884 |
polib
A Rust library to read, manipulate and write GNU gettext translation data in .po
format.
A Message represents a translation of a text entry from the source language to a target language.
A Catalog holds a collection of Messages, and is stored in a .po
file.
.po
fileuse polib::po_file;
use std::error::Error;
use std::path::Path;
fn main() -> Result<(), Box<dyn Error>> {
let catalog = po_file::parse(Path::new("foo.po"))?;
for message in catalog.messages() {
if message.is_translated() {
if message.is_singular() {
println!("{} => {}", message.msgid(), message.msgstr()?);
} else { // message.is_plural()
println!("{} => {}", message.msgid(), message.msgstr_plural()?.join(", "));
}
} else {
println!("{} is untranslated", message.msgid());
}
}
Ok(())
}
.po
filelet mut catalog = po_file::parse(Path::new(&input_file))?;
let mut filtered: usize = 0;
for mut message in catalog.messages_mut() {
if !message.is_translated() || message.is_fuzzy() {
message.delete();
filtered += 1;
}
}
po_file::write(&catalog, Path::new(&output_file))?;
println!("{} untranslated or fuzzy translations removed.", filtered);
let mut catalog = po_file::parse(Path::new(&input_file))?;
for mut message in catalog.messages_mut() {
if !message.is_translated() {
if message.is_singular() {
message.set_msgstr(/* some 3rdparty provided */translate(message.msgid()))?;
}
}
}
po_file::write(&catalog, Path::new(&output_file))?;
.po
file to .mo
formatmo_file::compile_from_po(Path::new(&input), Path::new(&output))?;
Refer to docs.rs.