-
Notifications
You must be signed in to change notification settings - Fork 18
108 base on gettext 026 #126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
624ff4e
Update gettext version to 0.26.0+
arturz ff88a3c
Create Kanta gettext adapter
arturz 892289b
Create custom gettext module
arturz 0a3a043
Remove Gettext repo module
arturz 3cb0848
Add elixirc_paths and postgrex
arturz 87c6f44
Update Gettext configuration instruction
arturz 162ddf2
Configure test section in config file
arturz 27ba432
Handle interpolations in messages from DB or cache
arturz c30b430
Add create_locale function
arturz 1aa2c9f
Set up tests helpers
arturz c7daeb9
Remove TODO
arturz 0e3b729
Pass bindings into adapter functions
arturz c5c4f28
Create fixtures for tests
arturz 92ba19e
Create basic test cases for Kanta.Gettext module
arturz 0f57bf0
Add multi_messages fixtures
arturz b0771f0
Create extractor test
arturz 4964824
Create macros test
arturz b554a2a
Create modified gettext test
arturz 5cb544b
Remove unused alias
arturz 4ac0d48
Change typespec to satisfy both 1.14 and 1.15+ formatters
arturz a26baa5
Update GitHub workflow
arturz 2cbb064
Delegate all public functions in Gettext module
arturz 47d024c
Update warnings to use Kanta.Gettext
arturz 7fd4746
Merge pull request #113 from curiosum-dev/develop
jk-lamb 6688ac3
Gettext 0.26.0 compatibility
jk-lamb d267726
Fix: non async tests for DB-related operation.
jk-lamb 6e86ced
Minor fixes
jk-lamb 58e9674
Update README.md
jk-lamb f823aaa
Merge branch 'develop' into 108-base-on-gettext-026
jk-lamb 040f1fb
Update mix.exs: Relax gettext dependency constraint (0.26.*)
jk-lamb ef09ad0
Update README.md
jk-lamb 0eb26a9
Update CHANGELOG.md
jk-lamb 9fdeefd
Add missing @moduledoc annotations
jk-lamb 3af9c65
Update README.md
jk-lamb 465a285
Add migration step to GitHub Actions database setup
jk-lamb c0afd52
Address PR review feedback
jk-lamb 1bbb126
Address PR review feedback
jk-lamb bb1666b
Move recompilation flags to _build directory
jk-lamb 27e71af
Merge branch '108-base-on-gettext-026' of https://github.com/curiosum…
jk-lamb 18fc101
Rename cached_db test file to match module name
jk-lamb c66121c
Fix line break in cached DB test
jk-lamb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,6 +80,12 @@ If you're working on an Elixir/Phoenix project and need to manage translations, | |
</ul> | ||
</li> | ||
<li><a href="#roadmap">Roadmap</a></li> | ||
<li> | ||
<a href="#development">Development</a> | ||
<ul> | ||
<li><a href="#running-tests">Running Tests</a></li> | ||
</ul> | ||
</li> | ||
<li><a href="#contributing">Contributing</a></li> | ||
<li><a href="#license">License</a></li> | ||
<li><a href="#contact">Contact</a></li> | ||
|
@@ -116,13 +122,10 @@ by adding `kanta` to your list of dependencies in `mix.exs`: | |
def deps do | ||
[ | ||
{:kanta, "~> 0.4.2"}, | ||
{: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: | ||
|
@@ -148,6 +151,12 @@ mix ecto.gen.migration add_kanta_translations_table | |
|
||
Open the generated migration file and set up `up` and `down` functions. | ||
|
||
**Current Migration Versions:** | ||
- PostgreSQL: **v4** (adds default context support for Gettext 0.26 backend) | ||
- SQLite: **v3** (adds default context support for Gettext 0.26 backend) | ||
|
||
If you're upgrading from an earlier version of Kanta, update your migration version to the latest. | ||
|
||
### PostgreSQL | ||
|
||
```elixir | ||
|
@@ -160,25 +169,52 @@ defmodule MyApp.Repo.Migrations.AddKantaTranslationsTable do | |
|
||
# We specify `version: 1` because we want to rollback all the way down including the first migration. | ||
def down do | ||
Kanta.Migration.down(version: 4, prefix: prefix()) # Prefix is needed if you are using multitenancy with i.e. triplex | ||
Kanta.Migration.down(version: 1, prefix: prefix()) # Prefix is needed if you are using multitenancy with i.e. triplex | ||
end | ||
end | ||
``` | ||
|
||
after that run | ||
### SQLite | ||
|
||
```elixir | ||
defmodule MyApp.Repo.Migrations.AddKantaTranslationsTable do | ||
use Ecto.Migration | ||
|
||
def up do | ||
Kanta.Migration.up(version: 3) | ||
end | ||
|
||
# We specify `version: 1` because we want to rollback all the way down including the first migration. | ||
def down do | ||
Kanta.Migration.down(version: 1) | ||
end | ||
end | ||
``` | ||
|
||
After that run: | ||
|
||
```bash | ||
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. | ||
Configuring Gettext requires just a single change. | ||
|
||
Wherever you have: | ||
|
||
```elixir | ||
use Gettext, ..., repo: Kanta.Gettext.Repo | ||
use Gettext, backend: YourApp.Gettext | ||
``` | ||
|
||
replace it with: | ||
|
||
```elixir | ||
use Kanta.Gettext, backend: YourApp.Gettext | ||
``` | ||
|
||
If you're using a Gettext version lower than 0.26, refer to the [official documentation](https://github.com/elixir-gettext/gettext) for migration instructions. | ||
|
||
## Kanta Supervisor | ||
|
||
In the `application.ex` file of our project, we add Kanta and its configuration to the list of processes. | ||
|
@@ -222,7 +258,9 @@ Kanta is based on the Phoenix Framework's default localization tool, GNU gettext | |
|
||
<img style="margin-top: 1rem; margin-bottom: 1rem;" src="./singular.png" alt="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. | ||
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. | ||
|
||
With Gettext 0.26+, Kanta uses a custom backend adapter system (`Kanta.Backend.Adapter.CachedDB`) that fetches translations from the database/cache at runtime instead of compiled PO files. 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 | ||
|
||
|
@@ -323,6 +361,25 @@ See the [open issues](https://github.com/curiosum-dev/kanta/issues) for a full l | |
|
||
<p align="right">(<a href="#readme-top">back to top</a>)</p> | ||
|
||
# Development | ||
|
||
## Running Tests | ||
|
||
If you're contributing to Kanta development, you'll need to run the test suite. The tests require a PostgreSQL database. | ||
|
||
### Prerequisites for Development | ||
- PostgreSQL 15+ (for running tests) | ||
- All prerequisites listed in [Getting Started](#prerequisites) | ||
|
||
### Test Setup | ||
|
||
First-time setup (or if tests are failing due to database issues): | ||
|
||
```bash | ||
# Setup test database and run migrations | ||
MIX_ENV=test mix ecto.drop && MIX_ENV=test mix ecto.create && MIX_ENV=test mix ecto.migrate | ||
``` | ||
|
||
<!-- CONTRIBUTING --> | ||
|
||
## Contributing | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
defmodule Kanta.Backend do | ||
@moduledoc """ | ||
Kanta.Backend is a module that provides an enhanced Gettext backend with database support. | ||
|
||
It extends the standard Gettext functionality by: | ||
1. First checking for translations in the database | ||
2. Falling back to PO file translations if not found in the database | ||
|
||
## Usage | ||
|
||
```elixir | ||
defmodule MyApp.Gettext do | ||
use Kanta.Backend, otp_app: :my_app | ||
end | ||
``` | ||
|
||
## Options | ||
|
||
* `:otp_app` - The OTP application that contains the backend | ||
* `:priv` - The directory where the translations are stored (defaults to "priv/YOUR_MODULE") | ||
* `:kanta_adapter` - The adapter module to use for database lookups (defaults to `Kanta.Backend.Adapter.CachedDB`) | ||
|
||
it also accepts all the Gettext.Backend options. See the official Gettext documentation for more details. | ||
|
||
|
||
""" | ||
alias Kanta.Utils.ModuleFolder | ||
require Logger | ||
|
||
defmacro __using__(opts) do | ||
quote bind_quoted: [opts: opts] do | ||
require Logger | ||
@flag_file Path.join([Mix.Project.build_path(), "kanta_recompile", ".gettext_recompiled"]) | ||
@adapter Keyword.get(opts, :kanta_adapter, Kanta.Backend.Adapter.CachedDB) | ||
opts = Keyword.drop(opts, [:kanta_adapter]) | ||
# Generate fallback Gettext backend form PO files | ||
use Kanta.Backend.GettextFallback, opts | ||
|
||
# When `mix gettext extract` create POT/PO files based on this backend usage (ex. getext(...) call) across the application codebase. | ||
if Gettext.Extractor.extracting?() do | ||
use Gettext.Backend, opts | ||
|
||
Kanta.Utils.GettextRecompiler.setup_recompile_flag(@flag_file) | ||
else | ||
opts = Keyword.merge(opts, priv: "priv/#{ModuleFolder.safe_folder_name(__MODULE__)}") | ||
use Gettext.Backend, opts | ||
end | ||
|
||
def __mix_recompile__?() do | ||
Kanta.Utils.GettextRecompiler.needs_recompile?(@flag_file) | ||
end | ||
|
||
def __gettext__(:known_locales) do | ||
backend = fallback_backend() | ||
Gettext.known_locales(backend) | ||
end | ||
|
||
def handle_missing_translation(locale, domain, msgctxt, msgid, bindings) do | ||
case Kanta.Backend.Adapter.CachedDB.lgettext( | ||
locale, | ||
domain, | ||
msgctxt, | ||
msgid, | ||
bindings | ||
) do | ||
{:ok, translation} -> | ||
{:ok, translation} | ||
|
||
{:error, :not_found} -> | ||
backend = fallback_backend() | ||
backend.lgettext(locale, domain, msgctxt, msgid, bindings) | ||
end | ||
end | ||
|
||
def handle_missing_plural_translation( | ||
locale, | ||
domain, | ||
msgctxt, | ||
msgid, | ||
msgid_plural, | ||
n, | ||
bindings | ||
) do | ||
case Kanta.Backend.Adapter.CachedDB.lngettext( | ||
locale, | ||
domain, | ||
msgctxt, | ||
msgid, | ||
msgid_plural, | ||
n, | ||
bindings | ||
) do | ||
{:ok, translation} -> | ||
{:ok, translation} | ||
|
||
{:error, :not_found} -> | ||
backend = fallback_backend() | ||
|
||
backend.lngettext( | ||
locale, | ||
domain, | ||
msgctxt, | ||
msgid, | ||
msgid_plural, | ||
n, | ||
bindings | ||
) | ||
end | ||
end | ||
|
||
defp fallback_backend() do | ||
Module.concat(__MODULE__, GettextFallbackBackend) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.