Gettext.PO (gettext v0.19.0) View Source
This module provides facilities for working with PO (.po
) and POT (.pot
)
files (mainly parsing).
Link to this section Summary
Functions
Dumps a Gettext.PO
struct as iodata.
Parses the contents of a file into a Gettext.PO
struct.
Parses the contents of a file into a Gettext.PO
struct, raising if there
are any errors.
Parses a string into a Gettext.PO
struct.
Parses a string into a Gettext.PO
struct, raising an exception if there are
any errors.
Link to this section Types
Specs
line() :: pos_integer()
Specs
Specs
t() :: %Gettext.PO{ file: nil | Path.t(), headers: [binary()], top_of_the_file_comments: [binary()], translations: [translation()] }
Specs
translation() :: Gettext.PO.Translation.t() | Gettext.PO.PluralTranslation.t()
Link to this section Functions
Specs
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"
Specs
parse_file(Path.t()) :: {:ok, t()} | parse_error() | {:error, atom()}
Parses the contents of a file into a Gettext.PO
struct.
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, po}
{:error, line, reason}
if there is an error with the contents of the.po
file (for example, 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 byFile.read/1
)
Examples
{:ok, po} = Gettext.PO.parse_file "translations.po"
po.file
#=> "translations.po"
Gettext.PO.parse_file "nonexistent"
#=> {:error, :enoent}
Specs
Parses the contents of a file into a Gettext.PO
struct, 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 "nonexistent.po": no such file or directory
Specs
parse_string(binary()) :: {:ok, t()} | parse_error()
Parses a string into a Gettext.PO
struct.
This function parses a given str
into a Gettext.PO
struct.
It returns {:ok, po}
if there are no errors,
otherwise {:error, line, reason}
.
Examples
iex> {:ok, po} = Gettext.PO.parse_string """
...> msgid "foo"
...> msgstr "bar"
...> """
iex> [t] = po.translations
iex> t.msgid
["foo"]
iex> t.msgstr
["bar"]
iex> po.headers
[]
iex> Gettext.PO.parse_string "foo"
{:error, 1, "unknown keyword 'foo'"}
Specs
Parses a string into a Gettext.PO
struct, raising an exception if there are
any errors.
Works exactly like parse_string/1
, but returns a Gettext.PO
struct
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'