forked from pentacent/keila
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add locale column to Users - Add Plug for setting Gettext locale - Add instructions to CONTRIBUTING.md - Add German translation
- Loading branch information
Showing
25 changed files
with
2,993 additions
and
80 deletions.
There are no files selected for viewing
This file contains 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,81 @@ | ||
# Contributing to Keila | ||
|
||
This document describes how to contribute code or translations to the Keila | ||
project. | ||
|
||
## Development Setup | ||
|
||
Before you get started with developing Keila, you need to set up your development | ||
environment. | ||
|
||
### Setup with VS Code and [DevContainer](https://code.visualstudio.com/docs/remote/containers) | ||
* Clone the repository: | ||
`git clone https://github.com/pentacent/keila.git` | ||
* Install the [Remote Container](https://github.com/microsoft/vscode-dev-containers) | ||
extension in Visual Code | ||
* Click on the  | ||
icon in the bottom left corner and select `Reopen in Container` or look for | ||
`Reopen in Container` in the command panel (`Ctrl+Shift+P`) | ||
* Wait for the containers to build and install all the dependencies needed to | ||
run Keila, including [PostgreSQL](https://www.postgresql.org/) | ||
and [Elixir](https://elixir-lang.org/install.html) | ||
* Open a terminal from VS Code and proceed with the instructions from the | ||
[Run Keila](#run-keila) section | ||
|
||
### Setup with other editors | ||
Start by cloning the repository: `git clone https://github.com/pentacent/keila.git` | ||
|
||
#### Install Dependencies | ||
[Install Elixir](https://elixir-lang.org/install.html) on your machine and launch | ||
an an instance of [PostgreSQL](https://www.postgresql.org/). | ||
|
||
#### Run Keila | ||
|
||
* Install dependencies with `mix deps.get` | ||
* Install dependencies and set up the database with `mix setup` | ||
* Start Keila server with `mix phx.server` | ||
|
||
Now you can visit [`localhost:4000`](http://localhost:4000) from your browser. | ||
|
||
## Contributing Code | ||
Code contributions to Keila are welcome! | ||
|
||
If you don’t know where to start, take a look at these [Good First Issues](https://github.com/pentacent/keila/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22). | ||
|
||
### Contribution Guidelines | ||
Before you commit code to Keila, make sure to run `mix format` to ensure | ||
consistent formatting. | ||
|
||
|
||
## Translating Keila | ||
Keila uses the [Gettext format](https://en.wikipedia.org/wiki/Gettext) for | ||
translations of the interface. | ||
|
||
Translation files are located in `priv/gettext`. | ||
|
||
### Getting Started | ||
|
||
Before you create a new translation or modify an existing translation, you need | ||
to extract and update all strings from the code: | ||
`mix gettext.extract --merge` | ||
|
||
### Modify existing translations | ||
Next you can use the editor of your choice to either edit the existing `.po` | ||
files in `priv/gettext`. A convenient Open Source program for editing | ||
translation files is [Poedit](https://poedit.net). | ||
|
||
### Creating a new translation | ||
1) Use the `.pot` templates in `priv/gettext` to create translation files for your | ||
language . New translations should be stored in | ||
`priv/gettext/:language/LC_MESSAGES`. | ||
|
||
2) Find the line `config :keila, KeilaWeb.Gettext` in `config/config.exs` and | ||
add the language code of the new language to the `:locales` list. | ||
|
||
3) Find the line `def available_locales() do` in `lib/keila_web/gettext.ex` and | ||
add a tuple with the native language name and the language code to the list | ||
returned by the function. | ||
|
||
## Finalizing your changes | ||
Run `mix gettext.merge priv/gettext` to compile your changes. Make sure to | ||
commit all changes to `.pot`, `.po` and the compiled `.mo`. |
This file contains 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 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 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 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 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 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 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,68 @@ | ||
defmodule KeilaWeb.PutLocalePlug do | ||
@moduledoc """ | ||
Plug for setting the Gettext locale. | ||
When the `:current_user` assign is available and has a locale set, the locale | ||
is taken from the user’s settings. Otherwise the locales from the | ||
`accept-language` header are used. | ||
""" | ||
|
||
alias Keila.Auth.User | ||
|
||
@spec init(list()) :: list() | ||
def init(_), do: [] | ||
|
||
@spec call(Plug.Conn.t(), list()) :: Plug.Conn.t() | ||
def call(conn, _opts) do | ||
case conn.assigns[:current_user] do | ||
%User{locale: locale} when is_binary(locale) -> put_locale(locale) | ||
_other -> put_locale_from_headers(conn) | ||
end | ||
|
||
conn | ||
end | ||
|
||
defp put_locale(locale) do | ||
locales = Application.get_env(:keila, KeilaWeb.Gettext) |> Keyword.fetch!(:locales) | ||
|
||
if locale in locales do | ||
Gettext.put_locale(locale) | ||
:ok | ||
else | ||
:error | ||
end | ||
end | ||
|
||
defp put_locale_from_headers(conn) do | ||
accepted_locales = get_accepted_locales(conn) | ||
|
||
# We’re not actually trying to find this value, but this is the easiest | ||
# way to run the `put_locale` side effect until we’ve found a locale that | ||
# is supported | ||
Enum.find(accepted_locales, fn locale -> | ||
put_locale(locale) == :ok | ||
end) | ||
end | ||
|
||
defp get_accepted_locales(conn) do | ||
conn | ||
|> Plug.Conn.get_req_header("accept-language") | ||
|> Enum.join(",") | ||
|> String.split(",") | ||
|> Enum.map(&String.trim/1) | ||
|> Enum.map(fn language -> | ||
case String.split(language, ";q=") do | ||
[language] -> | ||
{language, 1.0} | ||
|
||
[language, quality] -> | ||
case Float.parse(quality) do | ||
{quality, _} -> {language, quality} | ||
:error -> {language, 1.0} | ||
end | ||
end | ||
end) | ||
|> Enum.sort_by(&elem(&1, 1), :desc) | ||
|> Enum.map(&elem(&1, 0)) | ||
end | ||
end |
This file contains 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 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
Oops, something went wrong.