• Stars
    star
    173
  • Rank 220,124 (Top 5 %)
  • Language
    Elixir
  • License
    MIT License
  • Created about 2 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

User-friendly translations manager for Elixir/Phoenix projects.


Logo

User-friendly translations manager for Elixir/Phoenix projects.
View Demo · Report Bug · Request Feature



Note: Official documentation for Kanta library is available on hexdocs.



About The Project

If you're working on an Elixir/Phoenix project and need to manage translations, you know how time-consuming and error-prone it can be. That's where Kanta comes in. Our tool simplifies the process of managing translations by providing an intuitive interface for adding, editing, and deleting translations. Our tool also makes it easy to keep translations up-to-date as your project evolves. With Kanta, you can streamline your workflow and focus on building great software, not managing translations.

(back to top)

Getting Started

Prerequisites

  • Elixir (tested on 1.14.0)
  • Phoenix (tested on 1.7.0)
  • Ecto SQL (tested on 3.6)

Installation

The package can be installed by adding kanta to your list of dependencies in mix.exs:

def deps do
  [
    {:kanta, "~> 0.3.1"},
    {:gettext, git: "[email protected]:ravensiris/gettext.git", branch: "runtime-gettext"}
  ]
end

The dependency on this specific gettext version is because this library depends on an in-progress feature, to be included in a future release of gettext (see discussion in elixir-gettext/gettext#280 and pull request elixir-gettext/gettext#305). As of March 2023, this has been approved by an Elixir core team member, so we are eagerly awaiting for it being merged upstream.

Configuration

Add to config/config.exs file:

# config/config.exs
config :my_app, Kanta,
  endpoint: MyAppWeb.Endpoint, # Your app Endpoint module
  repo: MyApp.Repo, # Your app Repo module
  otp_name: :my_app, # Name of your OTP app
  plugins: []

Ecto repo module is used mostly for translations persistency. We also need endpoint to use VerifiedRoutes and project_root to locate the project's .po files.

Database migrations

Migrations is heavily inspired by the Oban approach. To add to the project tables necessary for the operation of Kanta and responsible for storing translations create migration with:

mix ecto.gen.migration add_kanta_translations_table

Open the generated migration file and set up up and down functions:

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
  end

  def down do
    Kanta.Migration.down(version: 2, prefix: prefix()) # Prefix is needed if you are using multitenancy with i.e. triplex
  end
end

after that run

mix ecto.migrate

Gettext module

We now need to pass information to our project's Gettext module that we want Kanta to manage translations. To do this add Kanta.Gettext.Repo as a default translation repository inside your Gettext module.

use Gettext, ..., repo: Kanta.Gettext.Repo

Kanta Supervisor

In the application.ex file of our project, we add Kanta and its configuration to the list of processes.

  def start(_type, _args) do
    children = [
      ...
      {Kanta, Application.fetch_env!(:my_app, Kanta)}
      ...
    ]
    ...
  end

Kanta UI

Inside your router.ex file we need to connect the Kanta panel using the kanta_dashboard macro.

import KantaWeb.Router

scope "/" do
  pipe_through :browser

  kanta_dashboard("/kanta")
end

(back to top)

Features

Extracting from PO files

messages

Kanta is based on the Phoenix Framework's default localization tool, GNU gettext. The process, which runs at application startup, analyzes .po files with messages and converts them to a format for convenient use with Ecto and Kanta itself.

Storing messages in the database

singular

Messages and translations from .po files are stored in tables created by the Kanta.Migration module. This allows easy viewing and modification of messages from the Kanta UI or directly from database tools. The caching mechanism prevents constant requests to the database when downloading translations, so you don't have to worry about a delay in application performance.

Translation progress

dashboard

Kanta tracks the progress of your application's translation into other languages and reports it in the user's dashboard. In the dashboard you can filter your messages by domain or context, or use a search engine. It is also possible to display only the messages that need translation to better see how much work remains to be done.

(back to top)

Plugins

DeepL

Not all of us are polyglots, and sometimes we need the help of machine translation tools. For this reason, we have provided plug-ins for communication with external services that will allow you to translate texts into another language without knowing it. As a first step, we introduced integration with DeepL API offering 500,000 characters/month for free and more in paid plans. To use DeepL API add {:kanta_deep_l_plugin, "~> 0.1.1"} to your deps and append Kanta.DeepL.Plugin to the list of plugins along with the API key from your account at DeepL. New features will then be added to the Kanta UI that will allow you to translate using this tool.

plural

# mix.exs
defp deps do 
  ...
  {:kanta_deep_l_plugin, "~> 0.1.1"}
end
# config/config.exs
config :kanta,
  ...
  plugins: [
    {Kanta.DeepL.Plugin, api_key: "YOUR_DEEPL_API_KEY"}
  ]

KantaSync

The KantaSync plugin allows you to synchronize translations between your production and staging/dev environments. It ensures that any changes made to translations in one are reflected in the others, helping you maintain consistency across different stages of development.

# mix.exs 
defp deps do 
  ...
  {:kanta_sync_plugin, "~> 0.1.0"}
end

You need to have Kanta API configured by using kanta_api macro.

# router.ex 
import KantaWeb.Router

scope "/" do 
  kanta_api("/kanta-api")
end 

Authorization

Set KANTA_SECRET_TOKEN environment variable for restricting API access. It should be generated with mix phx.gen.secret 256 and both environments must have the same KANTA_SECRET_TOKEN environment variables.

You can also disable default authorization mechanism and use your own, by passing disable_api_authorization: true option into Kanta's config.

PO Writer

Kanta was created to allow easy management of static text translations in the application, however, for various reasons like wanting a backup or parallel use of other tools like TMS etc. you may want to overwrite .po files with translations entered in Kanta. To install it append {:kanta_po_writer_plugin, git: "https://github.com/curiosum-dev/kanta_po_writer_plugin"} to your deps list. Currently, it's not on Hex because it's in a pre-release version. Then add Kanta.Plugins.POWriter to the list of plugins, and new functions will appear in the Kanta UI to allow writing to .po files.

# mix.exs
defp deps
  ...
  {:kanta_po_writer_plugin, git: "https://github.com/curiosum-dev/kanta_po_writer_plugin"},
end
# config/config.exs
config :kanta,
  ...
  plugins: [
    Kanta.POWriter.Plugin
  ]

(back to top)

Roadmap

  • Typespecs, tests, better docs
  • CI/CD
  • Gettext extract/merge automation
  • Google Translate, Yandex Translate, LibreTranslate Plugins
  • File import/export
  • Bumblebee AI translations
  • REST API

See the open issues for a full list of proposed features (and known issues).

(back to top)

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. We prefer gitflow and Conventional commits style but we don't require that. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'feat: Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)

Contact

Curiosum

MichaÅ‚ Buszkiewicz - [email protected]

Krzysztof Janiec - [email protected]

Artur ZiÄ™tkiewicz - [email protected]

(back to top)

More Repositories

1

permit

An uniform authorization library for Elixir. Supports Plug and Phoenix LiveView, aims for much more.
Elixir
105
star
2

contexted

Elixir library designed to help you manage the complexity of Phoenix contexts. It offers tools for module separation, subcontext creation, and auto-generating CRUD operations.
Elixir
45
star
3

curious_messenger

Companion repository for Phoenix LiveView Messenger app by Curiosum.dev. Part 1: https://curiosum.dev/blog/elixir-phoenix-liveview-messenger-part-1?utm_source=github&utm_medium=social, Part 2: https://curiosum.dev/blog/elixir-phoenix-liveview-messenger-part-2?utm_source=github&utm_medium=social, Part 3: https://curiosum.dev/blog/elixir-phoenix-liveview-messenger-part-3?utm_source=github&utm_medium=social, Part 4: https://curiosum.dev/blog/elixir-phoenix-liveview-messenger-part-4?utm_source=github&utm_medium=social
Elixir
34
star
4

permit_phoenix

Phoenix, Plug and LiveView integrations for the Permit authorization library.
Elixir
19
star
5

precise_pry

9
star
6

elixir-ecto-database-transactions

A companion repository to our article at Curiosum: https://curiosum.dev/blog/elixir-ecto-database-transactions
Elixir
9
star
7

permit_ecto

Elixir
8
star
8

elixir-phoenix-context-blog-post

Companion repository for blog post about maintainability aspect of Phoenix Context (https://curiosum.com/blog/elixir-phoenix-context-maintainability-guildelines)
Elixir
3
star
9

kanta_deep_l_plugin

DeepL integration for Kanta
Elixir
2
star
10

kanta_sync_plugin

Elixir
2
star
11

alias-import-require-differences

A companion repository to our article at Curiosum: https://curiosum.dev/blog/alias-import-require-use-in-elixir
2
star
12

companies-using-elixir

A companion repository to our article at Curiosum: https://curiosum.dev/blog/5-top-tier-companies-that-use-Elixir
1
star
13

concurrency-meetup

Elixir
1
star
14

elixir-meetup-exmachina

Elixir
1
star
15

kanta_po_writer_plugin

PoWriter plugin for Kanta
Elixir
1
star
16

kanta_import_export_plugin

Elixir
1
star
17

curiosum-articles

Learn more about Elixir & programming in general
1
star
18

kanta-example-app

Elixir
1
star
19

elixir-debugging

Elixir debugging playground
Elixir
1
star