View Source Expo.Po (expo v0.1.0-beta.6)

.po / .pot file handler

Link to this section Summary

Functions

Dumps a Expo.Messages struct as iodata.

Parses the contents of a file into a Expo.Messages struct, raising if there are any errors.

Parses the contents of a file into a Expo.Messages struct.

Parses a string into a Expo.Messages struct, raising an exception if there are any errors.

Parses a string into a Expo.Messages struct.

Link to this section Types

Link to this type

duplicate_messages_error()

View Source
@type duplicate_messages_error() ::
  {:error,
   {:duplicate_messages,
    [
      {message :: String.t(), new_line :: pos_integer(),
       old_line :: pos_integer()}
    ]}}
@type file_error() :: {:error, File.posix()}
@type parse_error() ::
  {:error, {:parse_error, message :: String.t(), line :: pos_integer()}}
@type parse_options() :: [{:file, Path.t()}]

Link to this section Functions

@spec compose(messages :: Expo.Messages.t()) :: iodata()

Dumps a Expo.Messages struct as iodata.

This function dumps a Expo.Messages 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

Examples

After running the following code:

iodata = Expo.Po.compose %Expo.Messages{
  headers: ["Last-Translator: Jane Doe"],
  messages: [
    %Expo.Message.Singular{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"
Link to this function

parse_file!(path, opts \\ [])

View Source
@spec parse_file!(Path.t(), opts :: parse_options()) ::
  Expo.Messages.t() | no_return()

Parses the contents of a file into a Expo.Messages struct, raising if there are any errors.

Works like parse_file/1, except that it raises a Expo.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

Examples

Expo.Po.parse_file! "nonexistent.po"
#=> ** (File.Error) could not parse "nonexistent.po": no such file or directory
Link to this function

parse_file(path, opts \\ [])

View Source
@spec parse_file(path :: Path.t(), opts :: parse_options()) ::
  {:ok, Expo.Messages.t()}
  | parse_error()
  | duplicate_messages_error()
  | file_error()

Parses the contents of a file into a Expo.Messages 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 by File.read/1)

examples

Examples

{:ok, po} = Expo.Po.parse_file "messages.po"
po.file
#=> "messages.po"

Expo.Po.parse_file "nonexistent"
#=> {:error, :enoent}
Link to this function

parse_string!(str, opts \\ [])

View Source
@spec parse_string!(content :: String.t(), opts :: parse_options()) ::
  Expo.Messages.t() | no_return()

Parses a string into a Expo.Messages struct, raising an exception if there are any errors.

Works exactly like parse_string/1, but returns a Expo.Messages struct if there are no errors or raises a Expo.Po.SyntaxError error if there are.

examples

Examples

iex> po = Expo.Po.parse_string! """
...> msgid "foo"
...> msgstr "bar"
...> """
iex> [t] = po.messages
iex> t.msgid
["foo"]
iex> t.msgstr
["bar"]
iex> po.headers
[]

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

iex> Expo.Po.parse_string!("""
...> msgid "test"
...> msgstr ""
...>
...> msgid "test"
...> msgstr ""
...> """)
** (Expo.Po.DuplicateMessagesError) 4: found duplicate on line 4 for msgid: 'test'
Link to this function

parse_string(content, opts \\ [])

View Source
@spec parse_string(content :: binary(), opts :: parse_options()) ::
  {:ok, Expo.Messages.t()} | parse_error() | duplicate_messages_error()

Parses a string into a Expo.Messages struct.

This function parses a given str into a Expo.Messages struct. It returns {:ok, po} if there are no errors, otherwise {:error, line, reason}.

examples

Examples

iex> {:ok, po} = Expo.Po.parse_string """
...> msgid "foo"
...> msgstr "bar"
...> """
iex> [t] = po.messages
iex> t.msgid
["foo"]
iex> t.msgstr
["bar"]
iex> po.headers
[]

iex> Expo.Po.parse_string "foo"
{:error, {:parse_error, "unknown keyword 'foo'", 1}}