Guidedog マニュアル 1.0.0-beta.16
言語
このページの内容
Guidedog / ドキュメント 1.0.0-beta.16

gd_gettext

GNU gettext message catalogs: reading and writing PO/POT text, reading and writing compiled MO files (both byte orders), Plural-Forms expressions, msgmerge-style updates with fuzzy matching, and translation statistics.

State is explicit: a Catalog or a Translator instead of the C library’s process-wide domain table. Nothing panics on bad input; every failure is an Error whose Display is an Elm-style report with a hint.

use gd_gettext::Lookup;
let cat = gd_gettext::parse_po("msgid \"Hello\"\nmsgstr \"Hallo\"\n", "de.po")?;
assert_eq!(cat.text("Hello"), "Hallo");
assert_eq!(cat.plural("%d file", "%d files", 3), "%d files");

Reading, merging, and writing cost what the input is long, never its square: a PO field continued over many lines is gathered once; an MO file whose strings share bytes is refused as damaged; the fuzzy search is bounded by FUZZY_WORK_PER_BYTE; and wrapping a long line looks only at the next piece.

Structs

pub struct gd_gettext::Catalog

One message catalog: a PO, POT, or MO file’s messages.

The catalog owns everything it holds. Lookups (the crate::Lookup trait) use an index built on first use; Catalog::add_message, Catalog::set_header_field and crate::sort_by_reference drop it. After editing messages or header directly, call Catalog::build_index before the next lookup.

Fields

pub name: String

The file name, or the name given to a parser.

pub header: Option<Message>

The msgid 「」 entry; its strs[0] holds the header fields.

pub messages: Vec<Message>

The messages in file order; obsolete ones included.

pub plural: PluralRule

The compiled Plural-Forms rule; n != 1 without a header.

pub allow_fuzzy: bool

Look fuzzy translations up too (Sphinx’s gettext_allow_fuzzy_translations).

Implementations

impl Catalog
pub fn new(name: &str) -> Catalog

Returns an empty catalog named name with the germanic plural rule (n != 1).

pub fn add_message(&mut self, m: Message) -> &mut Message

Appends a message, which the catalog owns from now on, and returns a reference to it. The lookup index is rebuilt on the next lookup.

pub fn message_count(&self) -> usize

Returns the number of messages, obsolete ones included, without the header.

pub fn header_field(&self, name: &str) -> Option<&str>

Returns the value of a header field ("Name: value" line), compared case-insensitively, borrowed from the header.

pub fn set_header_field(&mut self, name: &str, value: &str)

Replaces a header field, or appends it, keeping the others in order. Setting Plural-Forms to a valid rule also replaces Catalog::plural.

pub fn charset(&self) -> &str

Returns the charset the header’s Content-Type names, or "" without one.

impl Catalog
pub fn build_index(&mut self)

(Re)builds the lookup index now, so the first lookup does not pay for it. Call it after editing messages or header directly.

pub fn pool(&self) -> &str

Returns the text every translation of the catalog is a slice of, so a caller can register it once as a source and give parsed translations exact spans.

Trait Implementations

impl Default for Catalog
fn default() -> Catalog
impl From<&MoCatalog> for Catalog
fn from(mo: &MoCatalog) -> Catalog

Copies a compiled catalog into an editable one. Its messages have no comments or references; the first entry with an empty key is the header.

impl Lookup for Catalog
fn find(&self, id: &str, ctx: Option<&str>) -> Option<Forms<'_>>

Finds a message in the index. A fuzzy message is found only when allow_fuzzy is set; obsolete and untranslated messages are never found.

fn plural_rule(&self) -> &PluralRule
pub struct gd_gettext::Message

One catalog entry.

strs holds the msgstr, or msgstr[0..n) of a plural message; an empty string is an untranslated form.

Fields

pub ctx: Option<String>

The msgctxt; Some("") (msgctxt 「」) is a context too, different from none.

pub id: String

The msgid.

pub id_plural: Option<String>

The msgid_plural of a plural message.

pub strs: Vec<String>

The msgstr, or one string per plural form.

pub comments: Vec<String>

# translator comments.

pub notes: Vec<String>

#. comments for translators, from the extractor.

pub references: Vec<String>

#: locations, file:line each.

pub flags: Vec<String>

#, flags other than fuzzy, e.g. c-format.

pub fuzzy: bool

The fuzzy flag.

pub obsolete: bool

#~: kept for reuse, never looked up.

pub previous: Option<Previous>

The #| previous msgid of a fuzzy message.

pub line: usize

The 1-based line the entry starts on in its file; 0 when built in code.

Implementations

impl Message
pub fn new(id: &str) -> Message

Returns a message with this msgid and one empty (untranslated) msgstr.

pub fn is_translated(&self) -> bool

Tells whether every form has a translation (and there is at least one).

pub fn is_header(&self) -> bool

Tells whether this is the header entry: msgid 「」 without a context, not obsolete.

pub fn is_plural(&self) -> bool

Tells whether the message has a msgid_plural.

pub fn has_flag(&self, flag: &str) -> bool

Tells whether the message carries a #, flag; "fuzzy" is Message::fuzzy.

pub fn same_key(&self, other: &Message) -> bool

Tells whether two messages have the same context and msgid.

pub struct gd_gettext::Previous

A fuzzy message’s earlier msgctxt, msgid, and msgid_plural (the #| comments msgmerge writes), so a translator sees what changed.

Fields

pub ctx: Option<String>

The previous msgctxt; Some("") is an empty context, different from none.

pub id: String

The previous msgid.

pub id_plural: Option<String>

The previous msgid_plural.

pub struct gd_gettext::Error

A problem explained the way Elm’s compiler explains one: where, what, and what to do.

The error owns its strings, so it outlives the input it was made from. Display renders the report:

-- PO SYNTAX ERROR ------------------------------------------------------ de.po:2

This string has no closing quote.

2| msgstr "b
          ^

Hint: End the string with " on the same line; continue on the next line with ...

Fields

pub kind: ErrorKind

What went wrong.

pub file: String

The file (or the name given to a parser); empty when there is none.

pub line: usize

The 1-based line of the problem; 0 when it has no line.

pub column: usize

The 1-based byte column of the problem in excerpt; 0 for none.

pub excerpt: String

The offending line; empty for none.

pub message: String

A plain explanation.

pub hint: String

How to fix it.

Implementations

impl Error
pub fn title(&self) -> &'static str

Returns the report title, e.g. PO SYNTAX ERROR.

Trait Implementations

impl fmt::Display for Error
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
impl std::error::Error for Error
pub struct gd_gettext::LoadOptions

How catalogs are loaded. Fill it with struct-update syntax: LoadOptions { allow_fuzzy: true, ..LoadOptions::default() }.

Fields

pub max_bytes: u64

The largest file accepted, in bytes; 1 GiB by default. A larger file is refused with an error of kind ErrorKind::TooLarge before anything is read.

pub allow_fuzzy: bool

Look fuzzy translations up too (Sphinx’s gettext_allow_fuzzy_translations).

Trait Implementations

impl Default for LoadOptions
fn default() -> LoadOptions
pub struct gd_gettext::Forms<'a>

The translated forms of one message, borrowed from the catalog that found them.

An editable crate::Catalog keeps each form apart in its pool; a zero-copy crate::MoCatalog keeps them as the MO file does, joined by NUL. Both look the same from here.

Implementations

impl<'a> Forms<'a>
pub fn len(&self) -> usize

Returns the number of forms.

pub fn is_empty(&self) -> bool

Tells whether there are no forms.

pub fn get(&self, k: usize) -> Option<&'a str>

Returns form k (0 is the singular), borrowed from the catalog, if it exists.

pub fn iter(&self) -> impl Iterator<Item = &'a str> + 'a

Iterates the forms in order.

pub struct gd_gettext::MergeOptions

How merge works. The default is msgmerge’s: fuzzy matching on, obsolete messages kept.

Fields

pub fuzzy_matching: bool

Look for similar messages (msgmerge without -N).

pub keep_obsolete: bool

Keep translated messages that left the template as #~.

Trait Implementations

impl Default for MergeOptions
fn default() -> MergeOptions
pub struct gd_gettext::MergeStats

What a merge’s fuzzy search did; merge_counted returns it.

Fields

pub searches: usize

msgids searched for (each distinct msgid once).

pub bounds: usize

Candidates whose byte counts were compared (the cheap bound).

pub comparisons: usize

Candidates compared in full (similarity).

pub work: usize

Bytes those comparisons read.

pub cut_short: usize

Searches the work bound stopped before they had looked everywhere.

pub struct gd_gettext::Stats

Counts of a catalog’s messages, as sphinx-intl stat reports them.

Fields

pub translated: usize

Translated and not fuzzy.

pub fuzzy: usize

Translated but fuzzy.

pub untranslated: usize

Missing a form.

pub obsolete: usize

Obsolete (#~).

Implementations

impl Stats
pub fn add(&mut self, m: &Message)

Counts one message, as sphinx-intl stat does; the header does not count. With a crate::PoReader, this counts a file without holding it.

pub struct gd_gettext::MoCatalog

A compiled catalog served from one text pool: the file’s strings.

It owns the pool (the file’s buffer, reused), a span per string, and a copy of the hash table; lookups (Lookup) borrow from the pool and allocate nothing. Make one with MoCatalog::load or MoCatalog::from_bytes; for an editable catalog, use crate::parse_mo instead.

Implementations

impl MoCatalog
pub fn load(path: impl AsRef<Path>, options: &LoadOptions) -> Result<MoCatalog, Error>

Reads and checks a compiled catalog. Fails when the file cannot be read, is larger than options.max_bytes, or as MoCatalog::from_bytes fails.

pub fn from_bytes(data: impl Into<Vec<u8>>, name: &str) -> Result<MoCatalog, Error>

Takes MO bytes and checks them once, so lookups need no checks.

Fails with ErrorKind::MoFormat when the header, a table entry, or a string is truncated or out of bounds, when strings share bytes or keys repeat (no compiler writes either), or when a string is not UTF-8; with ErrorKind::Charset for a header naming another charset, and ErrorKind::Plural for a bad Plural-Forms.

pub fn name(&self) -> &str

Returns the file name, or the name given to MoCatalog::from_bytes.

pub fn entry_count(&self) -> usize

Returns the number of entries, the header included.

pub fn search(&self) -> &MoSearch

Returns how lookups find keys.

pub fn header(&self) -> Option<&str>

Returns the header entry’s text (the translation of 「」), if there is one.

pub fn header_field(&self, name: &str) -> Option<&str>

Returns the value of a header field, compared case-insensitively.

pub fn entries(&self) -> impl Iterator<Item = MoEntry<'_>> + '_

Returns the entries in file order (sorted by key in a file msgfmt wrote).

Trait Implementations

impl Lookup for MoCatalog
fn find(&self, id: &str, ctx: Option<&str>) -> Option<Forms<'_>>
fn plural_rule(&self) -> &PluralRule
pub struct gd_gettext::MoEntry<'a>

One message of a MoCatalog, borrowed from its buffer.

Fields

pub ctx: Option<&'a str>

The msgctxt, if any.

pub id: &'a str

The msgid.

pub id_plural: Option<&'a str>

The msgid_plural of a plural message.

pub translation: &'a str

The translation as stored: plural forms joined by NUL.

Implementations

impl<'a> MoEntry<'a>
pub fn forms(&self) -> Forms<'a>

Returns the translated forms.

pub struct gd_gettext::PluralProblem

Where and why an expression was refused.

Fields

pub message: &'static str

What is wrong, in lower case without a full stop.

pub column: usize

The 1-based byte column in the expression.

pub struct gd_gettext::PluralRule

A compiled Plural-Forms header: nplurals and the plural expression.

Fields

pub nplurals: usize

The number of plural forms, 1 to MAX_NPLURALS.

Trait Implementations

impl Default for PluralRule
fn default() -> PluralRule

gettext’s rule without a header: two forms, plural = n != 1.

pub struct gd_gettext::PoReader<R>

Reads PO or POT entries one at a time from any buffered source, so a large file need not be held whole.

It is an iterator of Result<Message, Error>: the header entry first when the file has one (Message::is_header), then the messages, obsolete ones included, in file order. After an error it yields nothing more. It keeps only the current entry and the keys seen so far (to report duplicates), plus one reused line buffer.

use gd_gettext::{PoReader, Stats};
let text = "msgid \"a\"\nmsgstr \"b\"\n\nmsgid \"c\"\nmsgstr \"\"\n";
let mut stats = Stats::default();
for message in PoReader::new(text.as_bytes(), "de.po") {
    stats.add(&message?);
}
assert_eq!((stats.translated, stats.untranslated), (1, 1));

Implementations

impl<R: BufRead> PoReader<R>
pub fn new(source: R, name: &str) -> PoReader<R>

Returns a reader of source; name labels errors. There is no size limit.

pub fn plural_rule(&self) -> &PluralRule

Returns the Plural-Forms rule of the header read so far (n != 1 before it).

impl PoReader<BufReader<File>>
pub fn open(path: impl AsRef<Path>, options: &LoadOptions) -> Result<Self, Error>

Opens a PO or POT file for reading entry by entry. Fails when it cannot be opened or is larger than options.max_bytes; reading stops with the same error if it grows past the limit.

Trait Implementations

impl<R: BufRead> Iterator for PoReader<R>
type Item = Result<Message, Error>
fn next(&mut self) -> Option<Self::Item>
pub struct gd_gettext::WriteOptions

How write_po lays a catalog out. The default wraps at 76 columns, as sphinx-intl (through Babel) does, and writes previous msgids and obsolete messages.

Fields

pub width: usize

Lines are wrapped to this many characters; 0: no wrapping.

pub references: ReferenceStyle

How locations are written.

pub previous: bool

Write #| previous msgids of fuzzy messages.

pub obsolete: bool

Write #~ obsolete messages.

Trait Implementations

impl Default for WriteOptions
fn default() -> WriteOptions
pub struct gd_gettext::BoundCatalog

A catalog a translator serves for a domain.

Fields

pub domain: String

The text domain, e.g. guidedog or sphinx.

pub catalog: Box<dyn Lookup + Send + Sync>

The catalog, owned by the translator: an editable crate::Catalog, a zero-copy MoCatalog, or anything else that implements Lookup.

pub struct gd_gettext::Translator

Serves several text domains, each from a chain of catalogs: the first bound catalog that translates a message wins, as Sphinx searches locale_dirs in order before its own catalogs. It stands in for the C library’s global state (bindtextdomain, textdomain, setlocale).

Fields

pub language: String

The language catalogs are looked for, e.g. pt_BR.

pub domain: String

The current domain (textdomain); messages by default.

pub bound: Vec<BoundCatalog>

The bound catalogs, in search order.

Implementations

impl Translator
pub fn new(language: &str) -> Translator

Returns a translator for language with no catalogs bound, in the messages domain.

pub fn add_catalog(&mut self, domain: &str, catalog: impl Lookup + Send + Sync + 'static)

Binds a catalog to a domain after those already bound; the translator owns it.

pub fn text_domain(&mut self, domain: &str)

Sets the current domain, as textdomain does.

pub fn bind_text_domain(&mut self, domain: &str, dir: &Path, options: &LoadOptions) -> Result<bool, Error>

Looks for dir/<language>/LC_MESSAGES/<domain>.po, then .mo, trying the language, then its base language (pt_BR, then pt), and binds the first catalog found: a .mo file as a zero-copy MoCatalog, a .po file as an editable catalog. Returns Ok(false) when there is none; fails when the file found cannot be read or parsed, or is larger than options.max_bytes.

pub fn domain_text<'a>(&'a self, domain: &str, id: &'a str) -> &'a str

dgettext: the translation of id in a domain, or id.

pub fn domain_with_context<'a>(&'a self, domain: &str, ctx: &str, id: &'a str) -> &'a str

dpgettext: Translator::domain_text in context ctx.

pub fn domain_plural<'a>(&'a self, domain: &str, id: &'a str, id_plural: &'a str, n: u64) -> &'a str

dngettext: the form for n in a domain, or the English singular or plural.

pub fn domain_plural_with_context<'a>(&'a self, domain: &str, ctx: Option<&str>, id: &'a str, id_plural: &'a str, n: u64) -> &'a str

dnpgettext: Translator::domain_plural with an optional context.

pub fn text<'a>(&'a self, id: &'a str) -> &'a str

gettext in the current domain.

pub fn plural<'a>(&'a self, id: &'a str, id_plural: &'a str, n: u64) -> &'a str

ngettext in the current domain.

pub fn with_context<'a>(&'a self, ctx: &str, id: &'a str) -> &'a str

pgettext in the current domain.

pub fn plural_with_context<'a>(&'a self, ctx: &str, id: &'a str, id_plural: &'a str, n: u64) -> &'a str

npgettext in the current domain.

Enums

pub enum gd_gettext::ErrorKind

What kind of problem an Error reports.

Variants

Syntax

The PO text breaks the format.

Duplicate

A message is defined twice.

Charset

The catalog is not UTF-8.

Encoding

Bytes that are not valid UTF-8.

Plural

A Plural-Forms header that cannot be used.

MoFormat

A truncated or inconsistent MO file.

Io

The file could not be read or written.

Memory

There was not enough memory to hold the catalog.

TooLarge

The file is larger than crate::LoadOptions::max_bytes.

Implementations

impl ErrorKind
pub fn title(self) -> &'static str

Returns the report title of this kind, e.g. PO SYNTAX ERROR.

pub enum gd_gettext::MoSearch

How a MoCatalog finds a key.

Variants

Hash { size: u32, probes: u32 }

Through the file’s hash table: one to a few probes. probes is the most any key of the file takes, so a lookup of a missing key stops there too.

size: u32

The table’s size (a prime).

probes: u32

The most probes a key of the file takes.

Sorted

Binary search on the original-string table, which is sorted.

Permuted(Box<[u32]>)

Binary search through an order built at load, for a file whose table is not sorted (msgfmt always sorts it).

pub enum gd_gettext::ReferenceStyle

How #: locations are written.

Variants

Wrapped

#: a.rst:1 b.rst:2, wrapped at the width, as msgmerge writes.

OnePerLine

One #: line per location, as Sphinx’s gettext builder writes.

None

No locations (gettext_location = False).

Traits

pub trait gd_gettext::Lookup: fmt::Debug

The gettext lookups, for any catalog that can find a message’s forms.

Implementors provide Lookup::find and Lookup::plural_rule; the C library’s gettext, ngettext, pgettext and npgettext follow from them. Every string returned is borrowed from the catalog, or is the id passed in.

fn find(&self, id: &str, ctx: Option<&str>) -> Option<Forms<'_>>

Returns the translated forms of a message; ctx None means no context. A message without a translation is not found.

fn plural_rule(&self) -> &PluralRule

Returns the catalog’s compiled Plural-Forms rule.

fn translation(&self, id: &str, ctx: Option<&str>) -> Option<&str>

Returns a message’s singular translation when it has a non-empty one.

fn text<'a>(&'a self, id: &'a str) -> &'a str

gettext: returns the translation of id, or id itself.

fn with_context<'a>(&'a self, ctx: &str, id: &'a str) -> &'a str

pgettext: returns the translation of id in context ctx, or id itself.

fn plural<'a>(&'a self, id: &'a str, id_plural: &'a str, n: u64) -> &'a str

ngettext: returns the form for n, or the English singular (n == 1) or plural when there is no translation or the form is empty.

fn plural_with_context<'a>(&'a self, ctx: Option<&str>, id: &'a str, id_plural: &'a str, n: u64) -> &'a str

npgettext: Lookup::plural with an optional context.

Implementors

impl Lookup for Catalog
impl Lookup for MoCatalog

Functions

pub fn gd_gettext::format_stats(s: &Stats) -> String

Returns sphinx-intl’s summary line: 3 translated, 1 fuzzy, 2 untranslated.

pub fn gd_gettext::merge(def: &Catalog, reference: &Catalog, options: &MergeOptions) -> Catalog

Updates a translation (def) to a new template (reference), as msgmerge and sphinx-intl update do, and returns the result as a new catalog; both inputs are borrowed. The result has the template’s messages in the template’s order, with its locations, notes, and flags:

  • a message found in def keeps its translation, translator comments, and fuzzy flag;
  • a new message similar to an old translated one gets that translation, marked fuzzy, with the old msgid kept as its previous msgid (#|);
  • old translated messages the template lost become obsolete (#~).

The header is def’s, with the template’s POT-Creation-Date.

pub fn gd_gettext::merge_counted(def: &Catalog, reference: &Catalog, options: &MergeOptions) -> (Catalog, MergeStats)

merge, also returning what its fuzzy search did.

pub fn gd_gettext::similarity(a: &str, b: &str) -> f64

2·M / (|a| + |b|), where M is the length of the longest common subsequence of bytes: 1 for equal strings, 0 for strings with nothing in common. It is the measure msgmerge’s fstrcmp approximates. Long strings are compared by their first 2000 bytes.

pub fn gd_gettext::sort_by_reference(cat: &mut Catalog)

Orders messages by their first location: file name, then line number as a number, then msgid; messages without locations go last. Obsolete messages keep their place after the live ones. Each message’s own locations are sorted the same way, as Sphinx sorts them.

pub fn gd_gettext::stats(cat: &Catalog) -> Stats

Counts a catalog’s messages as sphinx-intl stat does; the header does not count.

pub fn gd_gettext::load_mo(path: impl AsRef<Path>, options: &LoadOptions) -> Result<crate::Catalog, Error>

Reads a compiled catalog into an editable crate::Catalog (strings copied). Use MoCatalog::load to look up without copying.

Fails as MoCatalog::load fails.

pub fn gd_gettext::parse_mo(data: &[u8], name: &str) -> Result<crate::Catalog, Error>

Decodes MO bytes into an editable crate::Catalog that copies what it keeps, so data is only borrowed. Fails as MoCatalog::from_bytes fails, or with ErrorKind::Memory when the copy cannot be allocated.

pub fn gd_gettext::save_mo(cat: &Catalog, path: impl AsRef<Path>, include_fuzzy: bool) -> Result<(), Error>

Compiles a catalog into a file, through a buffer. See write_mo_to.

pub fn gd_gettext::write_mo(cat: &Catalog, include_fuzzy: bool) -> Result<Vec<u8>, Error>

Compiles a catalog as msgfmt does and returns the bytes. See write_mo_to.

pub fn gd_gettext::write_mo_to(out: &mut impl Write, cat: &Catalog, include_fuzzy: bool) -> Result<(), Error>

Compiles a catalog into out as msgfmt does: the header and every translated message, sorted by key, with a hash table, little-endian. Fuzzy messages are left out unless include_fuzzy; obsolete and untranslated ones always are; of messages with the same key, the first is written. The output depends only on the catalog.

The strings are written straight from the catalog; only the tables are built in memory. Pass a buffered writer: entries are written piece by piece. Fails with ErrorKind::Io when writing fails, and ErrorKind::MoFormat when the output would pass the format’s 4 GiB of offsets.

pub fn gd_gettext::compile_plural(expr: &str, nplurals: usize) -> Result<PluralRule, PluralProblem>

Compiles a plural expression for nplurals forms. Fails with the first problem found: a syntax error, nplurals out of 1 to 32, or an expression past the node or depth bounds.

pub fn gd_gettext::parse_plural_forms(value: &str) -> Result<PluralRule, Error>

Compiles a Plural-Forms header value such as nplurals=2; plural=(n != 1);. Fails with an error of kind ErrorKind::Plural whose excerpt is the value and whose column points at the problem; the caller fills in the file and line.

pub fn gd_gettext::plural_forms_for(language: &str) -> &'static str

Returns a Plural-Forms header value for a new catalog of a language: the rules the GNU gettext manual lists for common languages, and the germanic rule (n != 1) for others. pt_BR and pt-BR find pt_BR, then pt.

pub fn gd_gettext::plural_index(rule: &PluralRule, n: u64) -> usize

Returns the msgstr index for n; a result out of range selects form 0, as the C library does.

pub fn gd_gettext::load_po(path: impl AsRef<Path>, options: &LoadOptions) -> Result<Catalog, Error>

Reads a PO or POT file into an editable catalog, entry by entry through a buffer.

Fails when the file cannot be read or is larger than options.max_bytes, or as parse_po fails.

pub fn gd_gettext::parse_po(text: &str, name: &str) -> Result<Catalog, Error>

Parses PO or POT text into an editable catalog; name labels errors and the catalog, and text is only borrowed.

Fails on the first syntax error, a duplicate message, a header naming a charset other than UTF-8, or an invalid Plural-Forms header; the error names the line and column.

pub fn gd_gettext::parse_po_bytes(data: &[u8], name: &str) -> Result<Catalog, Error>

parse_po for bytes that may not be UTF-8: fails with ErrorKind::Encoding at the first byte that does not start a character.

pub fn gd_gettext::escape_string(s: &str) -> Cow<'_, str>

Escapes a value for a PO string body: backslash, quote, and the control characters C names (others as octal). Borrows s when nothing needs escaping.

pub fn gd_gettext::save_po(cat: &Catalog, path: impl AsRef<Path>, options: &WriteOptions) -> Result<(), Error>

Writes a catalog to a file as write_po formats it, entry by entry through a buffer. Fails with ErrorKind::Io when the file cannot be written.

pub fn gd_gettext::write_po(cat: &Catalog, options: &WriteOptions) -> String

Returns a catalog as PO text: the header, then the messages in order, then the obsolete ones, each entry laid out as msgmerge lays it out. The output depends on nothing but the catalog and the options, so files diff cleanly.

pub fn gd_gettext::write_po_into(out: &mut String, cat: &Catalog, options: &WriteOptions)

write_po appending to out, so a caller can reuse one buffer for many catalogs.

pub fn gd_gettext::write_po_to(out: &mut impl Write, cat: &Catalog, options: &WriteOptions) -> Result<(), Error>

write_po streaming into out one entry at a time, through one reused buffer, so the whole text is never held. Pass a buffered writer for a file. Fails with ErrorKind::Io when writing fails.

pub fn gd_gettext::find_catalog_file(dir: &Path, language: &str, domain: &str) -> Option<PathBuf>

Returns the path of a domain’s catalog for a language, dir/<lang>/LC_MESSAGES/<domain>.po (or .mo), trying each of language_candidates; None when there is none.

pub fn gd_gettext::language_candidates(language: &str) -> Vec<String>

Lists the catalog directory names to try for a language, most specific first: pt-BR gives pt-BR, pt_BR, and pt; sr@latin gives it, then sr. Empty for an empty language.

Constants

pub const gd_gettext::FUZZY_MIN_WORK: usize = 1 << 26

The least work a merge’s fuzzy search is allowed.

pub const gd_gettext::FUZZY_WORK_PER_BYTE: usize = 256

Bytes a merge’s fuzzy search may read per byte of the two catalogs』 msgids.

pub const gd_gettext::FUZZY_THRESHOLD: f64 = 0.6

msgmerge’s threshold: a changed message inherits the translation of the most similar old message when their similarity is at least this.

pub const gd_gettext::MAX_NPLURALS: usize = 32

The most plural forms a catalog may have.

pub const gd_gettext::MAX_PLURAL_DEPTH: usize = 100

The deepest a plural expression may nest (parser levels).

pub const gd_gettext::MAX_PLURAL_NODES: usize = 128

The most nodes a compiled plural expression may have.