#gettext #translation #po #gnu #data #language #file

polib

Read, manipulate and store translation data in GNU gettext PO format

2 unstable releases

0.2.0 Mar 11, 2023
0.1.0 Jan 20, 2022
0.0.1 Jan 15, 2022

#79 in Internationalization (i18n)

Download history 7298/week @ 2024-07-21 6921/week @ 2024-07-28 7376/week @ 2024-08-04 6697/week @ 2024-08-11 6755/week @ 2024-08-18 6867/week @ 2024-08-25 7821/week @ 2024-09-01 5916/week @ 2024-09-08 5846/week @ 2024-09-15 6516/week @ 2024-09-22 5674/week @ 2024-09-29 6607/week @ 2024-10-06 6881/week @ 2024-10-13 5784/week @ 2024-10-20 6335/week @ 2024-10-27 5466/week @ 2024-11-03

25,336 downloads per month
Used in 5 crates

MIT license

62KB
1.5K SLoC

polib

crates.io License: MIT GitHub Actions

A Rust library to read, manipulate and write GNU gettext translation data in .po format.

Basic Concepts

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.

Example

Iterate over messages in a .po file

use 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(())
}

Remove untranslated or fuzzy entries and save to another .po file

let 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);

Fill in missing translations from some other translation service

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))?;

Compile a .po file to .mo format

mo_file::compile_from_po(Path::new(&input), Path::new(&output))?;

Documentation

Refer to docs.rs.

Dependencies

~125–265KB