Gettext.PO

This module provides facilities for working with .po files (mainly parsing).

Summary

Dumps a Gettext.PO struct as iodata

Parses the contents of a file into a list of translations, raising if there are any errors

Parses the contents of a file into a list of translations

Parses a string into a list of translations, raising an exception if there are any errors

Parses a string into a list of translations

Types

line :: pos_integer
parse_error :: {:error, line, binary}
t :: %Gettext.PO{headers: [binary], translations: [translation], file: Path.t}

Functions

dump(po)

Specs

dump(t) :: iodata

Dumps a Gettext.PO struct as iodata.

This function dumps a Gettext.PO struct (representing a PO file) as iodata, which can later be written to a file or converted to a string with IO.iodata_to_binary/1.

Examples

After running the following code:

iodata = Gettext.PO.dump %Gettext.PO{
  headers: ["Last-Translator: Jane Doe"],
  translations: [
    %Gettext.PO.Translation{msgid: "foo", msgstr: "bar", comments: "# A comment"}
  ]
}

File.write!("/tmp/test.po", iodata)

the /tmp/test.po file would look like this:

msgid ""
msgstr ""
"Last-Translator: Jane Doe"

# A comment
msgid "foo"
msgstr "bar"
dump_headers(headers)
parse_file(path)

Specs

parse_file(Path.t) ::
  {:ok, t} |
  parse_error |
  {:error, atom}

Parses the contents of a file into a list of translations.

This function works similarly to parse_string/1 except that it takes a file and parses the contents of that file. It can return:

  • {:ok, translations}
  • {:error, line, reason} if there is an error with the contents of the .po file (e.g., a syntax error)
  • {:error, reason} if there is an error with reading the file (this error is one of the errors that can be returned by File.read/1)_

Examples

Gettext.PO.parse_file "translations.po"
#=> {:ok, [%Translation{msgid: "foo", msgstr: "bar"}]}

Gettext.PO.parse_file "nonexistent"
#=> {:error, :enoent}
parse_file!(path)

Specs

parse_file!(Path.t) :: t

Parses the contents of a file into a list of translations, raising if there are any errors.

Works like parse_file/1, except that it raises a Gettext.PO.SyntaxError exception if there’s a syntax error in the file or a File.Error error if there’s an error with reading the file.

Examples

Gettext.PO.parse_file! "nonexistent.po"
#=> ** (File.Error) could not parse file nonexistent.po: no such file or directory
parse_string(str)

Specs

parse_string(binary) :: {:ok, t} | parse_error

Parses a string into a list of translations.

This function parses a given str into a list of Gettext.PO.Translation and Gettext.PO.PluralTranslation structs. It returns {:ok, translations} if there are no errors, otherwise {:error, line, reason}.

Examples

iex> {:ok, po} = Gettext.PO.parse_string """
...> msgid "foo"
...> msgstr "bar"
...> """
iex> po.translations
[%Gettext.PO.Translation{msgid: ["foo"], msgstr: ["bar"], po_source_line: 1}]
iex> po.headers
[]

iex> Gettext.PO.parse_string "foo"
{:error, 1, "unknown keyword 'foo'"}
parse_string!(str)

Specs

parse_string!(binary) :: t

Parses a string into a list of translations, raising an exception if there are any errors.

Works exactly like parse_string/1, but returns the list of translations if there are no errors or raises a Gettext.PO.SyntaxError error if there are.

Examples

iex> Gettext.PO.parse_string!("msgid")
** (Gettext.PO.SyntaxError) 1: no space after 'msgid'