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 {
  Singular(context: String, content: String)
  Plural(context: String, content: Dict(Int, String))
}

Constructors

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

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.

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

Constructors

  • Revision(major: Int, minor: Int)

Dictionary of translations keyed by the original msgid.

pub type Translations =
  Dict(String, MoString)

Functions

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

Parse given MO file data.

Search Document