Repository: elixir-gettext/gettext
Pull request #296 (2021-12-06 14:58:47)

Separate Flag to detect autogenerated Translations
Follow up from https://github.com/elixir-gettext/gettext/pull/295#issuecomment-983994697

maennchen: @josevalim i like the long name because it is very descriptive of what it is used for and storage is cheap. I like elixir-autogen most of your suggestions.
josevalim: @whatyouhide what is your pick? :)
whatyouhide: `ex-autogen` would be my pick, if we're going for shortness. Otherwise `elixir-gen` or `elixir-autogen` are both fine!
maennchen: @josevalim / @whatyouhide I changed it to `elixir-autogen`.
maennchen: @whatyouhide Should've read your comment more carefully. `ex-autogen` is also a good option. I'll change it.
josevalim: Just one minor comment and we can ship it.

Patch:
diff --git a/lib/gettext/extractor.ex b/lib/gettext/extractor.ex
index 1e85770..0172c0f 100644
--- a/lib/gettext/extractor.ex
+++ b/lib/gettext/extractor.ex
@@ -20,7 +20,7 @@ defmodule Gettext.Extractor do
     PO.PluralTranslation
   }
 
-  @extracted_translations_flag "elixir-format"
+  @extracted_translations_flag "ex-autogen"
 
   @doc """
   Enables translation extraction.
@@ -152,7 +152,8 @@ defmodule Gettext.Extractor do
       msgctxt: if(msgctxt != nil, do: [msgctxt], else: nil),
       msgid_plural: [msgid_plural],
       msgstr: %{0 => [""], 1 => [""]},
-      flags: MapSet.new([@extracted_translations_flag]),
+      # TODO: Get Format Flag dynamically when extracting
+      flags: MapSet.new([@extracted_translations_flag, "elixir-format"]),
       references: [{Path.relative_to_cwd(file), line}],
       extracted_comments: extracted_comments
     }
@@ -163,7 +164,8 @@ defmodule Gettext.Extractor do
       msgid: [msgid],
       msgctxt: if(msgctxt != nil, do: [msgctxt], else: nil),
       msgstr: [""],
-      flags: MapSet.new([@extracted_translations_flag]),
+      # TODO: Get Format Flag dynamically when extracting
+      flags: MapSet.new([@extracted_translations_flag, "elixir-format"]),
       references: [{Path.relative_to_cwd(file), line}],
       extracted_comments: extracted_comments
     }
diff --git a/lib/gettext/po/translations.ex b/lib/gettext/po/translations.ex
index 0fe6cd1..d868915 100644
--- a/lib/gettext/po/translations.ex
+++ b/lib/gettext/po/translations.ex
@@ -18,7 +18,7 @@ defmodule Gettext.PO.Translations do
 
   ## Examples
 
-      iex> t = %Gettext.PO.Translation{msgid: "foo", flags: MapSet.new(["elixir-format"])}
+      iex> t = %Gettext.PO.Translation{msgid: "foo", flags: MapSet.new(["ex-autogen"])}
       iex> Gettext.PO.Translations.autogenerated?(t)
       true
 
@@ -29,7 +29,8 @@ defmodule Gettext.PO.Translations do
   """
   @spec autogenerated?(PO.translation()) :: boolean
   def autogenerated?(%module{flags: flags} = _translation) when is_translation(module) do
-    MapSet.member?(flags, "elixir-format")
+    # TODO: Remove elixir-format flag to detect autogenerated translations in future releases
+    MapSet.member?(flags, "ex-autogen") or MapSet.member?(flags, "elixir-format")
   end
 
   @doc """
diff --git a/lib/mix/tasks/gettext.extract.ex b/lib/mix/tasks/gettext.extract.ex
index 97f4263..80f6dbf 100644
--- a/lib/mix/tasks/gettext.extract.ex
+++ b/lib/mix/tasks/gettext.extract.ex
@@ -14,6 +14,10 @@ defmodule Mix.Tasks.Gettext.Extract do
   and `:priv` options given by Gettext modules when they call `use Gettext`. One
   POT file is generated for each translation domain.
 
+  All automatically extracted translations are assigned the `ex-autogen` flag.
+  If a translation from the POT is no longer present and has the `ex-autogen`
+  flag, the translation will be removed.
+
   If you would like to verify that your POT files are up to date with the
   current state of the codebase, you can provide the `--check-up-to-date`
   flag. This is particularly useful for automated checks and in CI systems.


Test patch:
diff --git a/test/gettext/extractor_test.exs b/test/gettext/extractor_test.exs
index c09fabd..4be9020 100644
--- a/test/gettext/extractor_test.exs
+++ b/test/gettext/extractor_test.exs
@@ -283,13 +283,13 @@ defmodule Gettext.ExtractorTest do
        #. some comment
        #. some other comment
        #. one more comment
-       #, elixir-format
+       #, elixir-format, ex-autogen
        #: foo.ex:16
        #: foo.ex:19
        msgid "foo"
        msgstr ""
 
-       #, elixir-format
+       #, elixir-format, ex-autogen
        #: foo.ex:21
        msgctxt "test"
        msgid "context based translation"
@@ -300,7 +300,7 @@ defmodule Gettext.ExtractorTest do
        msgid ""
        msgstr ""
 
-       #, elixir-format
+       #, elixir-format, ex-autogen
        #: foo.ex:17
        msgid "one error"
        msgid_plural "%{count} errors"
@@ -312,7 +312,7 @@ defmodule Gettext.ExtractorTest do
        msgid ""
        msgstr ""
 
-       #, elixir-format
+       #, elixir-format, ex-autogen
        #: foo.ex:20
        msgid "hi"
        msgstr ""
diff --git a/test/mix/tasks/gettext.extract_test.exs b/test/mix/tasks/gettext.extract_test.exs
index 451c058..7c42a4b 100644
--- a/test/mix/tasks/gettext.extract_test.exs
+++ b/test/mix/tasks/gettext.extract_test.exs
@@ -38,7 +38,7 @@ defmodule Mix.Tasks.Gettext.ExtractTest do
     assert output =~ "Extracted priv/gettext/default.pot"
 
     assert read_file("priv/gettext/default.pot") =~ """
-           #, elixir-format
+           #, elixir-format, ex-autogen
            #: lib/my_app.ex:7
            msgid "hello"
            msgstr ""
@@ -60,7 +60,7 @@ defmodule Mix.Tasks.Gettext.ExtractTest do
     end)
 
     assert read_file("priv/gettext/it/LC_MESSAGES/my_domain.po") == """
-           #, elixir-format
+           #, elixir-format, ex-autogen
            #: lib/other.ex:3
            msgid "other"
            msgstr ""