From 6ee1a98d371d034c6ee3c002350db059599bb068 Mon Sep 17 00:00:00 2001 From: Keisuke Kubota <40480197+kubosuke@users.noreply.github.com> Date: Wed, 13 Nov 2024 14:18:51 +0000 Subject: [PATCH] Feat/add default context when it's missing (#69) * Update extract_message.ex * Update messages_extractor.ex * Create v03.ex * set default context when missing * Update README.md * Create V04.ex * Update V04.ex * mix format --- README.md | 4 ++-- lib/kanta/migrations/postgresql/V04.ex | 23 +++++++++++++++++++ lib/kanta/migrations/postgresql/v03.ex | 1 - .../po_files/handlers/messages_extractor.ex | 3 +++ .../po_files/services/extract_message.ex | 5 ++++ 5 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 lib/kanta/migrations/postgresql/V04.ex diff --git a/README.md b/README.md index bb0735d..cecf6de 100644 --- a/README.md +++ b/README.md @@ -141,11 +141,11 @@ defmodule MyApp.Repo.Migrations.AddKantaTranslationsTable do use Ecto.Migration def up do - Kanta.Migration.up(version: 2, prefix: prefix()) # Prefix is needed if you are using multitenancy with i.e. triplex + Kanta.Migration.up(version: 3, prefix: prefix()) # Prefix is needed if you are using multitenancy with i.e. triplex end def down do - Kanta.Migration.down(version: 2, prefix: prefix()) # Prefix is needed if you are using multitenancy with i.e. triplex + Kanta.Migration.down(version: 3, prefix: prefix()) # Prefix is needed if you are using multitenancy with i.e. triplex end end ``` diff --git a/lib/kanta/migrations/postgresql/V04.ex b/lib/kanta/migrations/postgresql/V04.ex new file mode 100644 index 0000000..5de52a1 --- /dev/null +++ b/lib/kanta/migrations/postgresql/V04.ex @@ -0,0 +1,23 @@ +defmodule Kanta.Migrations.Postgresql.V04 do + @moduledoc """ + Kanta V4 Migrations + """ + + use Ecto.Migration + + def up do + Kanta.Migration.up(version: 3) + + execute( + "INSERT INTO kanta_contexts (name,inserted_at,updated_at) VALUES('default', NOW(), NOW()) ON CONFLICT (name) DO NOTHING;" + ) + + execute("UPDATE kanta_messages SET context_id=1 WHERE context_id IS NULL;") + end + + def down do + execute("UPDATE kanta_messages SET context_id=NULL WHERE context_id=1;") + + execute("DELETE FROM kanta_contexts WHERE name='default';") + end +end diff --git a/lib/kanta/migrations/postgresql/v03.ex b/lib/kanta/migrations/postgresql/v03.ex index 1561024..33ab2f5 100644 --- a/lib/kanta/migrations/postgresql/v03.ex +++ b/lib/kanta/migrations/postgresql/v03.ex @@ -4,7 +4,6 @@ defmodule Kanta.Migrations.Postgresql.V03 do """ use Ecto.Migration - alias Kanta.Utils.Colors @kanta_application_sources "kanta_application_sources" diff --git a/lib/kanta/po_files/handlers/messages_extractor.ex b/lib/kanta/po_files/handlers/messages_extractor.ex index 772ce84..cc0edb6 100644 --- a/lib/kanta/po_files/handlers/messages_extractor.ex +++ b/lib/kanta/po_files/handlers/messages_extractor.ex @@ -4,6 +4,7 @@ defmodule Kanta.POFiles.MessagesExtractor do """ @po_wildcard "**/*.po" + @default_context "default" alias Expo.{Messages, PO} @@ -38,6 +39,7 @@ defmodule Kanta.POFiles.MessagesExtractor do %Expo.Message.Singular{msgctxt: nil, msgid: msgid, msgstr: texts} -> ExtractSingularTranslation.call(%{ msgid: Enum.join(msgid), + context_name: @default_context, locale_name: locale, domain_name: domain, original_text: Enum.join(texts) @@ -55,6 +57,7 @@ defmodule Kanta.POFiles.MessagesExtractor do %Expo.Message.Plural{msgctxt: nil, msgid_plural: msgid, msgstr: plurals_map} -> ExtractPluralTranslation.call(%{ msgid: Enum.join(msgid), + context_name: @default_context, locale_name: locale, domain_name: domain, plurals_map: plurals_map, diff --git a/lib/kanta/po_files/services/extract_message.ex b/lib/kanta/po_files/services/extract_message.ex index 3a1a3d9..17a1381 100644 --- a/lib/kanta/po_files/services/extract_message.ex +++ b/lib/kanta/po_files/services/extract_message.ex @@ -8,6 +8,9 @@ defmodule Kanta.PoFiles.Services.ExtractMessage do alias Kanta.Translations alias Kanta.Translations.{Context, Domain} + @default_domain "default" + @default_context "default" + def call(attrs) do Repo.get_repo().transaction(fn -> with {:ok, domain} <- assign_domain(attrs[:domain_name]), @@ -48,6 +51,7 @@ defmodule Kanta.PoFiles.Services.ExtractMessage do {:error, :message, :not_found} -> attrs |> Map.put(:context_id, context_id) + |> Map.put(:domain_id, @default_domain) |> Translations.create_message() end end @@ -59,6 +63,7 @@ defmodule Kanta.PoFiles.Services.ExtractMessage do {:error, :message, :not_found} -> attrs + |> Map.put(:context_id, @default_context) |> Map.put(:domain_id, domain_id) |> Translations.create_message() end