Skip to content

Commit

Permalink
Interpolation Behaviour Message Format (#294) (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
maennchen authored Dec 7, 2021
1 parent 7083c5e commit b3315d0
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 8 deletions.
3 changes: 3 additions & 0 deletions lib/gettext/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ defmodule Gettext.Compiler do

default_domain = opts[:default_domain] || @default_domain

interpolation = opts[:interpolation] || Gettext.Interpolation.Default

quote do
@behaviour Gettext.Backend

Expand All @@ -38,6 +40,7 @@ defmodule Gettext.Compiler do
def __gettext__(:known_locales), do: unquote(known_locales)
def __gettext__(:default_locale), do: unquote(default_locale)
def __gettext__(:default_domain), do: unquote(default_domain)
def __gettext__(:interpolation), do: unquote(interpolation)

# The manifest lives in the root of the priv
# directory that contains .po/.pot files.
Expand Down
28 changes: 21 additions & 7 deletions lib/gettext/extractor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,17 @@ defmodule Gettext.Extractor do
"""
@spec extract(Macro.Env.t(), module, binary, binary, binary | {binary, binary}, [binary]) :: :ok
def extract(%Macro.Env{} = caller, backend, domain, msgctxt, id, extracted_comments) do
format_flag = backend.__gettext__(:interpolation).message_format()

translation =
create_translation_struct(id, msgctxt, caller.file, caller.line, extracted_comments)
create_translation_struct(
id,
msgctxt,
caller.file,
caller.line,
extracted_comments,
format_flag
)

ExtractorAgent.add_translation(backend, domain, translation)
end
Expand Down Expand Up @@ -146,26 +155,31 @@ defmodule Gettext.Extractor do
update_in(translation.references, &Enum.sort/1)
end

defp create_translation_struct({msgid, msgid_plural}, msgctxt, file, line, extracted_comments) do
defp create_translation_struct(
{msgid, msgid_plural},
msgctxt,
file,
line,
extracted_comments,
format_flag
) do
%PluralTranslation{
msgid: [msgid],
msgctxt: if(msgctxt != nil, do: [msgctxt], else: nil),
msgid_plural: [msgid_plural],
msgstr: %{0 => [""], 1 => [""]},
# TODO: Get Format Flag dynamically when extracting
flags: MapSet.new([@extracted_translations_flag, "elixir-format"]),
flags: MapSet.new([@extracted_translations_flag, format_flag]),
references: [{Path.relative_to_cwd(file), line}],
extracted_comments: extracted_comments
}
end

defp create_translation_struct(msgid, msgctxt, file, line, extracted_comments) do
defp create_translation_struct(msgid, msgctxt, file, line, extracted_comments, format_flag) do
%Translation{
msgid: [msgid],
msgctxt: if(msgctxt != nil, do: [msgctxt], else: nil),
msgstr: [""],
# TODO: Get Format Flag dynamically when extracting
flags: MapSet.new([@extracted_translations_flag, "elixir-format"]),
flags: MapSet.new([@extracted_translations_flag, format_flag]),
references: [{Path.relative_to_cwd(file), line}],
extracted_comments: extracted_comments
}
Expand Down
6 changes: 6 additions & 0 deletions lib/gettext/interpolation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ defmodule Gettext.Interpolation do
message :: String.t(),
bindings :: map()
) :: Macro.t()

@doc """
Defines the gettext message format
https://www.gnu.org/software/gettext/manual/html_node/PO-Files.html#index-msgstr
"""
@callback message_format :: String.t()
end
3 changes: 3 additions & 0 deletions lib/gettext/interpolation/default.ex
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,7 @@ defmodule Gettext.Interpolation.Default do

{:<<>>, [], parts}
end

@impl Gettext.Interpolation
def message_format, do: "elixir-format"
end
2 changes: 1 addition & 1 deletion lib/gettext/po/translations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule Gettext.PO.Translations do
@doc """
Tells whether a translation was manually entered or generated by Gettext.
As of now, a translation is considered autogenerated if it has the "elixir-format" flag.
As of now, a translation is considered autogenerated if it has the "ex-autogen" flag.
## Examples
Expand Down
3 changes: 3 additions & 0 deletions test/gettext_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ defmodule GettextTest.TranslatorWithDuckInterpolator.Interpolator do
{:ok, "quack #{unquote(message)} #{inspect(unquote(bindings))} quack"}
end
end

@impl Gettext.Interpolation
def message_format, do: "duck-format"
end

defmodule GettextTest.TranslatorWithDuckInterpolator do
Expand Down

0 comments on commit b3315d0

Please sign in to comment.