poreader/
lib.rs

1//! Translation catalogues are key part of any localization infrastructure. They contain the lists
2//! of messages from the application, possibly disambiguated with identifiers or contexts, and
3//! corresponding translations.
4//!
5//! Catalogs are usually stored in one of two formats: [Portable Objects (`.po`)][PO], used
6//! primarily by [GNU gettext][gettext], and [XML Localisation Interchange File Format
7//! (XLIFF)][XLIFF], a more generic OASIS open standard.
8//!
9//! These formats can be converted to each other, and to and from many others, using
10//! [translate-toolkit][tt].
11//!
12//! [XLIFF] is quite flexible and can be used in different ways, but this library focuses
13//! primarily on using it in a way [gettext] and [translate-toolkit][tt] work, namely with separate
14//! catalogue for each language.
15//!
16//! Example:
17//! ```rust
18//! use poreader::PoParser;
19//!
20//! use std::{env::args, fs::File, io::Result};
21//!
22//! fn main() -> Result<()> {
23//!     // Filename
24//!     let filename = match args().skip(1).next() {
25//!         Some(v) => v,
26//!         None => {
27//!             eprintln!("No file specified");
28//!
29//!             return Ok(());
30//!         }
31//!     };
32//!
33//!     // Open a file
34//!     let file = File::open(filename)?;
35//!
36//!     // Create PO parser
37//!     let parser = PoParser::new();
38//!
39//!     // Create PO reader
40//!     let reader = parser.parse(file)?;
41//!
42//!     // Read PO file by iterating on units
43//!     for unit in reader {
44//!         let unit = unit?;
45//!
46//!         // Show `msgid`
47//!         println!(" - {}", unit.message().get_id())
48//!     }
49//!
50//!     Ok(())
51//! }
52//! ```
53//!
54//! [PO]: https://www.gnu.org/software/gettext/manual/html_node/PO-Files.html
55//! [XLIFF]: https://www.oasis-open.org/committees/xliff/
56//! [gettext]: https://www.gnu.org/software/gettext/
57//! [tt]: http://toolkit.translatehouse.org/
58
59extern crate locale_config;
60extern crate regex;
61
62mod enums;
63mod po;
64
65pub mod comment;
66pub mod error;
67pub mod note;
68pub mod plural;
69pub mod unit;
70
71pub use self::{
72    enums::{Message, Origin, State},
73    po::{PoParser, PoReader},
74};
75
76use locale_config::LanguageRange;
77use std::collections::HashMap;
78
79/// Catalogue reader.
80///
81/// Defines common interface of catalogue readers. Read the units by simply iterating over the
82/// reader. The other methods are for the important metadata.
83pub trait CatalogueReader: Iterator<Item = Result<unit::Unit, error::Error>> {
84    /// The target language of the translation
85    fn target_language(&self) -> &LanguageRange<'static>;
86
87    /// Notes in the header entry
88    fn header_notes(&self) -> &Vec<note::Note>;
89
90    /// Comments in the header entry
91    fn header_comments(&self) -> &Vec<comment::Comment>;
92
93    /// Header properties as a map
94    fn header_properties(&self) -> &HashMap<String, String>;
95
96    // TODO: More attributes, possibly a generic API
97}