kielet/mo

An MO file parser.

Both little and big endian files are supported.

All strings must be UTF-8.

Types

pub type Endianness {
  BigEndian
  LittleEndian
}

Constructors

  • BigEndian
  • LittleEndian
pub type Header {
  Header(
    revision: Revision,
    string_count: Int,
    og_table_offset: Int,
    trans_table_offset: Int,
    ht_size: Int,
    ht_offset: Int,
  )
}

Constructors

  • Header(
      revision: Revision,
      string_count: Int,
      og_table_offset: Int,
      trans_table_offset: Int,
      ht_size: Int,
      ht_offset: Int,
    )

    Arguments

    • string_count

      Amount of strings in file.

    • og_table_offset

      Offset to table of strings in original language.

    • trans_table_offset

      Offset to translations table.

    • ht_size

      Hash table size (not used by this parser).

    • ht_offset

      Hash table offset.

An MO file can contain metadata that looks quite like HTTP headers. The most important data here are the plural forms with the key Plural-Forms.

pub type MetaData =
  Dict(String, String)
pub type Mo {
  Mo(
    endianness: Endianness,
    header: Header,
    translations: Translations,
    metadata: MetaData,
  )
}

Constructors

  • Mo(
      endianness: Endianness,
      header: Header,
      translations: Translations,
      metadata: MetaData,
    )

A translation in the file. The context is a free-form text that can be added as extra context for the translators.

pub type MoString {
  SingularStr(context: option.Option(String), content: String)
  PluralStr(
    context: option.Option(String),
    content: Dict(Int, String),
  )
}

Constructors

  • SingularStr(context: option.Option(String), content: String)
  • PluralStr(
      context: option.Option(String),
      content: Dict(Int, String),
    )

A message ID consisting of possible context and the message text. The translations are unique based on these IDs, i.e. the MO may only contain one of any given context-content pair.

pub type Msgid {
  NoContext(content: String)
  WithContext(context: String, content: String)
}

Constructors

  • NoContext(content: String)

    The message did not have context. Note that this is not identical to having context that is an empty string.

  • WithContext(context: String, content: String)
pub type ParseError {
  MagicNumberNotFound
  MalformedHeader
  UnknownRevision(Revision)
  OffsetPastEnd(Int)
  MalformedOffsetTableEntry(BitArray)
  StringNotUTF8(BitArray)
  MetaItemMissing
  MetaItemIsNotSingular
  PluralFormWithZeroEntries(index: Int)
}

Constructors

  • MagicNumberNotFound
  • MalformedHeader
  • UnknownRevision(Revision)
  • OffsetPastEnd(Int)

    An offset was given that either pointed directly out of bounds or the data it pointed to would have been too big for the file.

  • MalformedOffsetTableEntry(BitArray)
  • StringNotUTF8(BitArray)
  • MetaItemMissing

    Metadata is contained as a “translation” for the msgid “” (empty string). If this translation is missing, this error is returned.

  • MetaItemIsNotSingular

    The metadata item is a plural.

  • PluralFormWithZeroEntries(index: Int)

    The translation with index i was a plural form but it had no entries.

pub type Revision {
  Revision(major: Int, minor: Int)
}

Constructors

  • Revision(major: Int, minor: Int)

A translation string to output. The context is moved to Msgid.

pub type Translation {
  Singular(content: String)
  Plural(content: Dict(Int, String))
}

Constructors

  • Singular(content: String)
  • Plural(content: Dict(Int, String))

Dictionary of translations keyed by the original msgid.

pub type Translations =
  Dict(Msgid, Translation)

Functions

pub fn parse(mo: BitArray) -> Result(Mo, ParseError)

Parse given MO file data.

Search Document