Skip to content

Commit

Permalink
Merge pull request #79 from curiosum-dev/fix/align-with-main
Browse files Browse the repository at this point in the history
Fix/align with main
  • Loading branch information
jan-swiatek authored Sep 16, 2024
2 parents bdc79c0 + 8f710f7 commit ae667e0
Show file tree
Hide file tree
Showing 24 changed files with 469 additions and 88 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<ul>
<li><a href="#po-writer">PO Writer</a></li>
<li><a href="#deepl">DeepL</a></li>
<li><a href="#kantasync">Translation synchronization</a></li>
</ul>
</li>
<li><a href="#roadmap">Roadmap</a></li>
Expand Down Expand Up @@ -336,6 +337,8 @@ Distributed under the MIT License. See `LICENSE.txt` for more information.

## Contact

[Curiosum](https://curiosum.com)

Michał Buszkiewicz - michal@curiosum.com

Krzysztof Janiec - krzysztof.janiec@curiosum.com
Expand Down
32 changes: 32 additions & 0 deletions docs/how-to-write-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,38 @@ end

![Preview of plugin's panel added to the Kanta dashboard](assets/images/dashboard.png)

#### Need async or more control over the process?
You can use `DashboardLive` instead. Which would render as a child live view.
This way you can write async code and have a separate process for your component.
**lib/dashboard_live**:

```elixir
defmodule YourPluginName.DashboardLive do
@moduledoc """
Phoenix child live view for Kanta dashboard
"""

use Phoenix.LiveView, container: {:div, style: "grid-column: 1 / -1;"}

@impl true
def render(assigns) do
~H"""
<div class="bg-white dark:bg-stone-900 overflow-hidden shadow rounded-lg">
<div class="flex flex-col items-center justify-center px-4 py-5 sm:p-6">
<div class="text-3xl font-bold text-primary dark:text-accent-light">status: <%= @status %></div>
<div class="text-slate-600 dark:text-content-light font-medium text-lg">YourPluginName</div>
</div>
</div>
"""
end

@impl true
def mount(_params, _session, socket) do
{:ok, socket}
end
end
```

### Adding custom form to translation edit view

Enhance the translation edit view with a touch of personalization, by creating a component named `YourPluginName.FormComponent`.
Expand Down
38 changes: 36 additions & 2 deletions lib/kanta/gettext/repo.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule Kanta.Gettext.Repo do
alias Kanta.Utils.Compilation

alias Kanta.Translations.{
Context,
Domain,
Expand All @@ -15,6 +17,14 @@ defmodule Kanta.Gettext.Repo do
end

def get_translation(locale, domain, msgctxt, msgid, opts) do
if Compilation.compiling?() do
msgid
else
do_get_translation(locale, domain, msgctxt, msgid, opts)
end
end

defp do_get_translation(locale, domain, msgctxt, msgid, opts) do
default_locale = Application.get_env(:kanta, :default_locale) || "en"

with {:ok, %Locale{id: locale_id}} <-
Expand All @@ -39,7 +49,7 @@ defmodule Kanta.Gettext.Repo do
) do
if is_nil(text) do
if locale != default_locale do
get_translation(default_locale, domain, msgctxt, msgid, opts)
do_get_translation(default_locale, domain, msgctxt, msgid, opts)
else
:not_found
end
Expand All @@ -61,6 +71,30 @@ defmodule Kanta.Gettext.Repo do
plural_form,
opts
) do
if Compilation.compiling?() do
if plural_form == 1, do: msgid, else: msgid_plural
else
do_get_plural_translation(
locale,
domain,
msgctxt,
msgid,
msgid_plural,
plural_form,
opts
)
end
end

defp do_get_plural_translation(
locale,
domain,
msgctxt,
msgid,
msgid_plural,
plural_form,
opts
) do
default_locale = Application.get_env(:kanta, :default_locale) || "en"

with {:ok, %Locale{id: locale_id, plurals_header: plurals_header}} <-
Expand Down Expand Up @@ -88,7 +122,7 @@ defmodule Kanta.Gettext.Repo do
) do
if is_nil(text) do
if locale != default_locale do
get_plural_translation(
do_get_plural_translation(
default_locale,
domain,
msgctxt,
Expand Down
1 change: 1 addition & 0 deletions lib/kanta/migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ defmodule Kanta.Migration do
defp migrator do
case repo().__adapter__() do
Ecto.Adapters.Postgres -> Kanta.Migrations.Postgresql
Ecto.Adapters.SQLite3 -> Kanta.Migrations.SQLite3
end
end
end
81 changes: 81 additions & 0 deletions lib/kanta/migrations/sqlite3.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
defmodule Kanta.Migrations.SQLite3 do
@moduledoc false

@behaviour Kanta.Migration

use Ecto.Migration

@initial_version 1
@current_version 1

@doc false
def initial_version, do: @initial_version

@doc false
def current_version, do: @current_version

@impl Kanta.Migration
def up(opts) do
opts = with_defaults(opts, @current_version)
initial = migrated_version(opts)

cond do
initial == 0 ->
change(@initial_version..opts.version, :up, opts)

initial < opts.version ->
change((initial + 1)..opts.version, :up, opts)

true ->
:ok
end
end

@impl Kanta.Migration
def down(opts) do
opts = with_defaults(opts, @initial_version)
initial = max(migrated_version(opts), @initial_version)

if initial >= opts.version do
change(initial..opts.version, :down, opts)
end
end

@impl Kanta.Migration
def migrated_version(opts) do
opts = with_defaults(opts, @initial_version)

repo = Map.get_lazy(opts, :repo, fn -> repo() end)
query = "PRAGMA user_version"

case repo.query(query, [], log: false) do
{:ok, %{rows: [[version]]}} when is_integer(version) -> version
_ -> 0
end
end

defp change(range, direction, opts) do
for index <- range do
pad_idx = String.pad_leading(to_string(index), 2, "0")

[__MODULE__, "V#{pad_idx}"]
|> Module.concat()
|> apply(direction, [opts])
end

case direction do
:up -> record_version(opts, Enum.max(range))
:down -> record_version(opts, Enum.min(range) - 1)
end
end

defp record_version(_opts, 0), do: :ok

defp record_version(_opts, version) do
execute "PRAGMA user_version = #{version}"
end

defp with_defaults(opts, version) do
Enum.into(opts, %{version: version})
end
end
149 changes: 149 additions & 0 deletions lib/kanta/migrations/sqlite3/v01.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
defmodule Kanta.Migrations.SQLite3.V01 do
@moduledoc false

use Ecto.Migration

@kanta_locales "kanta_locales"
@kanta_domains "kanta_domains"
@kanta_contexts "kanta_contexts"
@kanta_messages "kanta_messages"
@kanta_singular_translations "kanta_singular_translations"
@kanta_plural_translations "kanta_plural_translations"

def up(opts) do
[
&up_locales/1,
&up_contexts/1,
&up_domains/1,
&up_messages/1,
&up_singular_translations/1,
&up_plural_translations/1
]
|> Enum.each(&apply(&1, [opts]))
end

def down(opts) do
[
&down_plural_translations/1,
&down_singular_translations/1,
&down_messages/1,
&down_domains/1,
&down_contexts/1,
&down_locales/1
]
|> Enum.each(&apply(&1, [opts]))
end

defp up_locales(_opts) do
create_if_not_exists table(@kanta_locales) do
add(:iso639_code, :string)
add(:name, :string)
add(:native_name, :string)
add(:family, :string)
add(:wiki_url, :string)
add(:colors, {:array, :string})
add(:plurals_header, :string)
timestamps()
end

create_if_not_exists unique_index(@kanta_locales, [:iso639_code])
end

defp up_domains(_opts) do
create_if_not_exists table(@kanta_domains) do
add(:name, :string)
add(:description, :text)
add(:color, :string, null: false, default: "#7E37D8")
timestamps()
end

create_if_not_exists unique_index(@kanta_domains, [:name])
end

defp up_contexts(_opts) do
create_if_not_exists table(@kanta_contexts) do
add(:name, :string)
add(:description, :text)
add(:color, :string, null: false, default: "#7E37D8")
timestamps()
end

create_if_not_exists unique_index(@kanta_contexts, [:name])
end

defp up_messages(_opts) do
create_if_not_exists table(@kanta_messages) do
add(:msgid, :text)

add(:message_type, :string,
null: false,
check: %{name: "message_type_check", expr: "message_type IN ('singular', 'plural')"}
)

add(:domain_id, references(@kanta_domains), null: true)
add(:context_id, references(@kanta_contexts), null: true)
timestamps()
end

execute "ALTER TABLE #{@kanta_messages} ADD COLUMN searchable TEXT AS (msgid) VIRTUAL"

create_if_not_exists unique_index(@kanta_messages, [:context_id, :domain_id, :msgid])
end

defp up_singular_translations(_opts) do
create_if_not_exists table(@kanta_singular_translations) do
add(:original_text, :text)
add(:translated_text, :text, null: true)
add(:locale_id, references(@kanta_locales))
add(:message_id, references(@kanta_messages))
timestamps()
end

create_if_not_exists unique_index(@kanta_singular_translations, [:locale_id, :message_id])

execute "ALTER TABLE #{@kanta_singular_translations} ADD COLUMN searchable TEXT AS (translated_text) VIRTUAL"
end

defp up_plural_translations(_opts) do
create_if_not_exists table(@kanta_plural_translations) do
add(:nplural_index, :integer)
add(:original_text, :text)
add(:translated_text, :text, null: true)
add(:locale_id, references(@kanta_locales))
add(:message_id, references(@kanta_messages))
timestamps()
end

create_if_not_exists unique_index(@kanta_plural_translations, [
:locale_id,
:message_id,
:nplural_index
])

execute "ALTER TABLE #{@kanta_plural_translations} ADD COLUMN searchable TEXT AS (translated_text) VIRTUAL"
end

defp down_locales(_opts) do
drop table(@kanta_locales)
end

defp down_domains(_opts) do
drop table(@kanta_domains)
end

defp down_contexts(_opts) do
drop table(@kanta_contexts)
end

defp down_messages(_opts) do
drop table(@kanta_messages)
end

defp down_singular_translations(_opts) do
drop table(@kanta_singular_translations)
end

defp down_plural_translations(_opts) do
drop table(@kanta_plural_translations)
end
end
Loading

0 comments on commit ae667e0

Please sign in to comment.