Skip to content

Commit

Permalink
Feat/add default context when it's missing (#69)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kubosuke authored Nov 13, 2024
1 parent 8e5e9ed commit 6ee1a98
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
23 changes: 23 additions & 0 deletions lib/kanta/migrations/postgresql/V04.ex
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion lib/kanta/migrations/postgresql/v03.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ defmodule Kanta.Migrations.Postgresql.V03 do
"""

use Ecto.Migration

alias Kanta.Utils.Colors

@kanta_application_sources "kanta_application_sources"
Expand Down
3 changes: 3 additions & 0 deletions lib/kanta/po_files/handlers/messages_extractor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Kanta.POFiles.MessagesExtractor do
"""

@po_wildcard "**/*.po"
@default_context "default"

alias Expo.{Messages, PO}

Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions lib/kanta/po_files/services/extract_message.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 6ee1a98

Please sign in to comment.