Repository: elixir-gettext/gettext
Pull request #235 (2020-01-11 22:24:22)

dialyzer warnings are gone
see https://github.com/elixir-gettext/gettext/issues/233#issuecomment-573359495


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7f8339b..47ae7fa 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
 # Changelog
 
+## v0.17.3
+
+* Add `lgettext/4` back which was removeed in v0.17.2 - note `lgettext/4` is private API and may be removed in future once again
+
 ## v0.17.2
 
 * Support `pgettext`
diff --git a/lib/gettext.ex b/lib/gettext.ex
index 8942581..9259ceb 100644
--- a/lib/gettext.ex
+++ b/lib/gettext.ex
@@ -697,7 +697,7 @@ defmodule Gettext do
       Gettext.dgettext(MyApp.Gettext, "errors", "signup form", "%{name} is not a valid name", name: "Meg")
       #=> "Meg non è un nome valido"
   """
-  @spec dpgettext(module, binary, binary, binary, bindings) :: binary
+  @spec dpgettext(module, binary, binary | nil, binary, bindings) :: binary
   def dpgettext(backend, domain, msgctxt, msgid, bindings) when is_list(bindings) do
     dpgettext(backend, domain, msgctxt, msgid, Map.new(bindings))
   end
@@ -816,7 +816,8 @@ defmodule Gettext do
       #=> "Errore"
 
   """
-  @spec dpngettext(module, binary, binary, binary, binary, non_neg_integer, bindings) :: binary
+  @spec dpngettext(module, binary, binary | nil, binary, binary, non_neg_integer, bindings) ::
+          binary
   def dpngettext(backend, domain, msgctxt, msgid, msgid_plural, n, bindings \\ %{})
 
   def dpngettext(backend, domain, msgctxt, msgid, msgid_plural, n, bindings)
diff --git a/lib/gettext/compiler.ex b/lib/gettext/compiler.ex
index b50e6f4..26075f2 100644
--- a/lib/gettext/compiler.ex
+++ b/lib/gettext/compiler.ex
@@ -58,10 +58,9 @@ defmodule Gettext.Compiler do
 
       unquote(macros())
 
-      # These are the two functions we generated inside the backend. Here we define the bodyless
-      # clauses.
-      def lgettext(locale, domain, msgctxt, msgid, bindings)
-      def lngettext(locale, domain, msgctxt, msgid, msgid_plural, n, bindings)
+      # These are the two functions we generated inside the backend.
+      def lgettext(locale, domain, msgctxt \\ nil, msgid, bindings)
+      def lngettext(locale, domain, msgctxt \\ nil, msgid, msgid_plural, n, bindings)
 
       unquote(compile_po_files(env, translations_dir, opts))
 
diff --git a/lib/gettext/extractor.ex b/lib/gettext/extractor.ex
index e5b94f1..756d5a3 100644
--- a/lib/gettext/extractor.ex
+++ b/lib/gettext/extractor.ex
@@ -361,6 +361,6 @@ defmodule Gettext.Extractor do
           "plural translation with msgid '#{IO.iodata_to_binary(t.msgid)}' has a non-empty msgstr"
   end
 
-  defp blank?(nil), do: true
-  defp blank?(str), do: IO.iodata_length(str) == 0
+  defp blank?(str) when not is_nil(str), do: IO.iodata_length(str) == 0
+  defp blank?(_), do: true
 end
diff --git a/lib/gettext/merger.ex b/lib/gettext/merger.ex
index 0417e5a..4947565 100644
--- a/lib/gettext/merger.ex
+++ b/lib/gettext/merger.ex
@@ -183,7 +183,6 @@ defmodule Gettext.Merger do
   over the new PO file as they're meant to be comments generated by tools or
   comments directed to developers.
   """
-  @spec new_po_file(Path.t(), Path.t(), String.t(), Keyword.t()) :: iodata
   def new_po_file(po_file, pot_file, locale, opts) when is_binary(locale) and is_list(opts) do
     pot = PO.parse_file!(pot_file)
     opts = put_plural_forms_opt(opts, pot.headers, locale)
diff --git a/lib/gettext/po/plural_translation.ex b/lib/gettext/po/plural_translation.ex
index 8d01fbb..7ec824a 100644
--- a/lib/gettext/po/plural_translation.ex
+++ b/lib/gettext/po/plural_translation.ex
@@ -31,7 +31,7 @@ defmodule Gettext.PO.PluralTranslation do
 
   @type t :: %__MODULE__{
           msgid: [binary],
-          msgid_plural: [binary],
+          msgid_plural: [binary] | nil,
           msgstr: %{non_neg_integer => [binary]},
           msgctxt: [binary] | nil,
           comments: [binary],
@@ -45,11 +45,11 @@ defmodule Gettext.PO.PluralTranslation do
 
   defstruct msgid: nil,
             msgid_plural: nil,
-            msgstr: nil,
+            msgstr: [],
             msgctxt: nil,
             comments: [],
             extracted_comments: [],
             references: [],
             flags: MapSet.new(),
-            po_source_line: nil
+            po_source_line: 1
 end
diff --git a/lib/gettext/po/translation.ex b/lib/gettext/po/translation.ex
index 7fd0d1d..0c67a83 100644
--- a/lib/gettext/po/translation.ex
+++ b/lib/gettext/po/translation.ex
@@ -42,11 +42,11 @@ defmodule Gettext.PO.Translation do
   @enforce_keys [:msgid]
 
   defstruct msgid: nil,
-            msgstr: nil,
+            msgstr: [],
             msgctxt: nil,
             comments: [],
             extracted_comments: [],
             references: [],
             flags: MapSet.new(),
-            po_source_line: nil
+            po_source_line: 1
 end
diff --git a/mix.exs b/mix.exs
index 27f9a20..910eb54 100644
--- a/mix.exs
+++ b/mix.exs
@@ -1,7 +1,7 @@
 defmodule Gettext.Mixfile do
   use Mix.Project
 
-  @version "0.17.2"
+  @version "0.17.3"
 
   @description "Internationalization and localization through gettext"
   @repo_url "https://github.com/elixir-lang/gettext"
@@ -48,7 +48,7 @@ defmodule Gettext.Mixfile do
 
   defp deps do
     [
-      {:ex_doc, "~> 0.19", only: :dev}
+      {:ex_doc, "~> 0.19", only: :docs}
     ]
   end
 end


Test patch:
diff --git a/test/gettext_test.exs b/test/gettext_test.exs
index 9e640c6..589bd51 100644
--- a/test/gettext_test.exs
+++ b/test/gettext_test.exs
@@ -241,11 +241,11 @@ defmodule GettextTest do
   test "strings are concatenated before generating function clauses" do
     msgid = "Concatenated and long string"
 
-    assert Translator.lgettext("it", "default", nil, msgid, %{}) ==
+    assert Translator.lgettext("it", "default", msgid, %{}) ==
              {:ok, "Stringa lunga e concatenata"}
 
-    # assert Translator.lgettext("it", "default", msgid, %{}) ==
-    #         {:ok, "Stringa lunga e concatenata"}
+    assert Translator.lgettext("it", "default", nil, msgid, %{}) ==
+             {:ok, "Stringa lunga e concatenata"}
 
     msgid = "A friend"
     msgid_plural = "%{count} friends"
@@ -254,7 +254,7 @@ defmodule GettextTest do
              {:ok, "Un amico"}
   end
 
-  test "lgettext/4: default handle_missing_binding preserves key" do
+  test "lgettext/5: default handle_missing_binding preserves key" do
     msgid = "My name is %{name} and I'm %{age}"
 
     assert Translator.lgettext("it", "interpolations", nil, msgid, %{name: "José"}) ==
@@ -270,7 +270,7 @@ defmodule GettextTest do
                "%{name}, missing translation!\")"
   end
 
-  test "lgettext/4: interpolation works when a translation is missing" do
+  test "lgettext/5: interpolation works when a translation is missing" do
     msgid = "Hello %{name}, missing translation!"
 
     assert Translator.lgettext("pl", "foo", nil, msgid, %{name: "Samantha"}) ==
@@ -285,7 +285,7 @@ defmodule GettextTest do
              {:missing_bindings, "Hello %{name}", [:name]}
   end
 
-  test "lgettext/4: fallbacks to handle_missing_translation if no translation is found" do
+  test "lgettext/5: fallbacks to handle_missing_translation if no translation is found" do
     msgid = "Hello %{name}"
     bindings = %{name: "Jane"}