Localize.Gettext.Migration (Localize v1.10.0)

Copy Markdown View Source

Converts Gettext catalogs written in Gettext's own %{name} syntax to MF2, for a backend that uses Localize.Gettext.Interpolation.

Each msgid, plural msgid and msgstr is rewritten by Localize.Gettext.Interpolation.mf2_source/1, the same reading the interpolator applies to it when it formats, so a converted message formats exactly as the original did: %{count} becomes {$count}, and the message's other text is escaped where MF2 needs it. A message that is valid MF2 and has no %{name} placeholder is left as it is; one that is not valid MF2, such as text holding a brace or beginning with ., which a gettext call in a backend with MF2 interpolation refuses, is converted as the text it is, so it shows as it did.

A msgid is the key a translation is found by, so the gettext calls that use one must change with it. convert/1 returns the msgids it renamed, for the source to be rewritten in step.

Summary

Types

A Gettext lookup in source: the call, its line, and its domain, context, msgid and plural msgid, each a string, nil where the call has none, or :dynamic where the argument is an expression.

A plural entry, by its context, msgid and plural msgid.

A msgid that conversion renamed, with its context. A plural entry converted to one .match message also names its plural msgid in :from_plural.

A msgid in a gettext call that convert_source/1 left for a person to convert.

Functions

Converts the messages of a catalog to MF2.

Converts the msgids of the gettext calls in a source file to MF2, as convert/2 converts them in a catalog.

Returns the plural entries of a catalog that convert/2 can write as one .match message each.

Returns the Gettext lookups in a source file.

Types

lookup()

@type lookup() :: %{
  call: String.t(),
  line: pos_integer(),
  domain: String.t() | :dynamic,
  context: String.t() | nil | :dynamic,
  msgid: String.t() | :dynamic,
  msgid_plural: String.t() | nil | :dynamic
}

A Gettext lookup in source: the call, its line, and its domain, context, msgid and plural msgid, each a string, nil where the call has none, or :dynamic where the argument is an expression.

plural_key()

@type plural_key() :: {String.t() | nil, String.t(), String.t()}

A plural entry, by its context, msgid and plural msgid.

rename()

@type rename() :: %{
  :context => String.t() | nil,
  :from => String.t(),
  :to => String.t(),
  optional(:from_plural) => String.t()
}

A msgid that conversion renamed, with its context. A plural entry converted to one .match message also names its plural msgid in :from_plural.

skipped()

@type skipped() :: %{line: pos_integer(), call: String.t(), reason: String.t()}

A msgid in a gettext call that convert_source/1 left for a person to convert.

Functions

convert(catalog, options \\ [])

@spec convert(Expo.Messages.t(), Keyword.t()) ::
  {:ok, Expo.Messages.t(), [rename()]} | {:error, Exception.t()}

Converts the messages of a catalog to MF2.

With the :plurals option, a plural entry becomes a singular entry whose msgid and msgstr are each one .match message on the count binding, choosing the form Gettext would choose: each CLDR plural category takes the form Gettext gives its counts, and a count Gettext gives another form, such as 0 under a French n != 1 formula, takes an exact key. The msgid's variants follow the source language's plural rules and the msgstr's the catalog's language and Plural-Forms header. An entry Gettext and CLDR disagree on beyond a few small counts cannot be written as categories, and is converted as with :plurals off; convertible_plurals/2 tells which entries can be.

Arguments

Options

  • :plurals is false, the default, to keep plural entries as plural entries with their placeholders converted, true to convert each plural entry that can be, or a set of plural_key/0, from convertible_plurals/2, to convert those alone, so a template, its translations and the calls that use them convert the same entries.

  • :only is the set of entries to convert, as {context, msgid} keys, leaving every other entry as it is. The default is nil, which converts every entry. mix localize.gettext.mf2 passes the entries whose every lookup in source it converts too.

  • :locale is the catalog's language, for its plural forms. The default is its Language header.

  • :source_locale is the language msgids are written in. The default is "en".

Returns

  • {:ok, messages, renames}, where messages is the converted catalog and renames lists each msgid and plural msgid that changed, with its context, in the order they appear, or

  • {:error, exception} when catalog is not an Expo.Messages struct or an option is not valid.

Examples

iex> {:ok, catalog} =
...>   Expo.PO.parse_string(~S'''
...>   msgid "should be at least %{count} character(s)"
...>   msgstr "doit contenir au moins %{count} caractère(s)"
...>   ''')
iex> {:ok, converted, renames} = Localize.Gettext.Migration.convert(catalog)
iex> [message] = converted.messages
iex> {IO.iodata_to_binary(message.msgid), IO.iodata_to_binary(message.msgstr)}
{"should be at least {$count} character(s)", "doit contenir au moins {$count} caractère(s)"}
iex> renames
[%{context: nil, from: "should be at least %{count} character(s)", to: "should be at least {$count} character(s)"}]

convert_source(source, options \\ [])

@spec convert_source(String.t(), Keyword.t()) ::
  {:ok, String.t(), %{rewritten: non_neg_integer(), skipped: [skipped()]}}
  | {:error, Exception.t()}

Converts the msgids of the gettext calls in a source file to MF2, as convert/2 converts them in a catalog.

Conversion is a function of the msgid alone, so a msgid converted here matches the msgid convert/1 gives its catalog entry, and the two can be converted separately. The calls are found in the text, so a call in a HEEx template or in a ~H sigil is converted as one in a module is: each Gettext macro, with or without _noop, bare or through its backend module, and each Gettext function, which takes the backend first. A msgid written as a string literal, a heredoc included, is rewritten as a string literal; one written as a sigil, a concatenation or an expression that holds a %{name} placeholder is left as it is and reported. A call in a comment or a documentation string is converted too, as it is found in text.

With the :plurals option, a plural call, ngettext or its d, p and dp forms, whose entry convert/2 writes as a .match message becomes the singular call, gettext and the rest, with that message as its msgid and the count in its bindings, written last so it wins as Gettext's does. Its domain and context must be string literals to know whether its entry is converted. Run mix format afterwards, as the bindings are written on one line.

Arguments

  • source is the text of a source file.

  • options is a keyword list of options.

Options

  • :plurals is false, the default, to leave plural calls as plural calls with their msgids converted, or the plural entries to convert, as a set of plural_key/0 for every domain or a map of domain to such a set, built from convertible_plurals/2 over the domain's catalogs.

  • :source_locale is the language msgids are written in, as for convert/2. The default is "en".

  • :default_domain is the domain of a call that names none, the backend's default domain. The default is "default".

  • :only is the entries whose calls to convert, as a set of {context, msgid} keys for every domain or a map of domain to such a set, leaving every other call as it is. The default is nil, which converts every call.

Returns

  • {:ok, source, report}, where source is the converted text and report is a map with :rewritten, the number of msgids rewritten, and :skipped, the msgids left for a person to convert, each a map with :line, :call and :reason, or

  • {:error, exception} when source is not a string or an option is not valid.

Examples

iex> Localize.Gettext.Migration.convert_source(~S|gettext("Hello %{name}", name: user.name)|)
{:ok, ~S|gettext("Hello {$name}", name: user.name)|, %{rewritten: 1, skipped: []}}

iex> Localize.Gettext.Migration.convert_source(~S|ngettext("%{count} file", "%{count} files", n)|)
{:ok, ~S|ngettext("{$count} file", "{$count} files", n)|, %{rewritten: 2, skipped: []}}

iex> keys = MapSet.new([{nil, "%{count} file", "%{count} files"}])
iex> {:ok, source, _report} =
...>   Localize.Gettext.Migration.convert_source(~S|ngettext("%{count} file", "%{count} files", n)|, plurals: keys)
iex> source
~S|gettext(".local $plural_count = {$count :number}\n.match $plural_count\none {{{$count} file}}\n* {{{$count} files}}", count: n)|

convertible_plurals(catalog, options \\ [])

@spec convertible_plurals(Expo.Messages.t(), Keyword.t()) ::
  {:ok, MapSet.t(plural_key())} | {:error, Exception.t()}

Returns the plural entries of a catalog that convert/2 can write as one .match message each.

An entry can be converted in a template when its msgid and plural msgid can be, under the source language's plural rules, and in a translation when its msgstr forms can be too, under the catalog's language and Plural-Forms header. A msgid is a key shared by the template, every translation and the calls that use it, so convert only the entries every catalog of a domain can convert: the intersection of what this function returns for each.

Arguments

Options

  • :locale is the catalog's language, for its plural forms. The default is its Language header.

  • :source_locale is the language msgids are written in. The default is "en".

Returns

  • {:ok, keys}, a set of plural_key/0, or

  • {:error, exception} when catalog is not an Expo.Messages struct or an option is not valid.

Examples

iex> {:ok, catalog} =
...>   Expo.PO.parse_string(~S'''
...>   msgid "%{count} file"
...>   msgid_plural "%{count} files"
...>   msgstr[0] ""
...>   msgstr[1] ""
...>   ''')
iex> Localize.Gettext.Migration.convertible_plurals(catalog)
{:ok, MapSet.new([{nil, "%{count} file", "%{count} files"}])}

lookups(source, options \\ [])

@spec lookups(String.t(), Keyword.t()) :: {:ok, [lookup()]} | {:error, Exception.t()}

Returns the Gettext lookups in a source file.

A msgid can only be converted where every lookup of it changes with it. A lookup with a literal msgid is converted with its catalog entry; one whose msgid is an expression, as in a translate_error/1 that translates Ecto's messages, or whose domain is, looks up msgids that cannot be known from the source, which the catalogs they may name must therefore keep. mix localize.gettext.mf2 reads the lookups to choose what it can convert safely.

Arguments

  • source is the text of a source file.

  • options is a keyword list of options.

Options

  • :default_domain is the domain of a call that names none. The default is "default".

Returns

  • {:ok, lookups}, a list of lookup/0 in the order they appear, or

  • {:error, exception} when source is not a string.

Examples

iex> {:ok, [literal, dynamic]} =
...>   Localize.Gettext.Migration.lookups(~S|gettext("Hello %{name}", name: n) <> Gettext.dgettext(MyApp.Gettext, "errors", message)|)
iex> {literal.domain, literal.msgid}
{"default", "Hello %{name}"}
iex> {dynamic.domain, dynamic.msgid}
{"errors", :dynamic}