configure gettext

This commit is contained in:
shibao 2022-02-07 19:30:21 -05:00
parent be8d3be4b3
commit 24fa837478
5 changed files with 23 additions and 8 deletions

View File

@ -52,6 +52,7 @@ You can use the following environment variables to configure Cannery in
- `SECRET_KEY_BASE`: Secret key base used to sign cookies. Must be generated
with `docker exec -it cannery mix phx.gen.secret` and set for server to start.
- `REGISTRATION`: Controls if user sign-up should be invite only or set to public. Set to `public` to enable public registration. Defaults to `invite`.
- `LOCALE`: Sets a custom locale. Defaults to `en_US`.
# Contribution

View File

@ -40,6 +40,9 @@ config :cannery, Cannery.Mailer, adapter: Swoosh.Adapters.Local
# Swoosh API client is needed for adapters other than SMTP.
config :swoosh, :api_client, false
# Gettext
config :gettext, :default_locale, "en_US"
# Configure esbuild (the version is required)
# config :esbuild,
# version: "0.14.0",

View File

@ -12,6 +12,9 @@ if System.get_env("PHX_SERVER") && System.get_env("RELEASE_NAME") do
config :cannery, CanneryWeb.Endpoint, server: true
end
# Set locale
Gettext.put_locale(System.get_env("LOCALE") || "en_US")
maybe_ipv6 = if System.get_env("ECTO_IPV6"), do: [:inet6], else: []
database_url =

View File

@ -17,12 +17,11 @@ defmodule CanneryWeb.ErrorHelpers do
assigns = %{extra_class: extra_class, form: form, field: field}
~H"""
<%= for error <- Keyword.get_values(@form.errors, @field) do %>
<span class={"invalid-feedback #{@extra_class}"}
phx-feedback-for={input_name(@form, @field)}>
<%= translate_error(error) %>
</span>
<% end %>
<%= for error <- Keyword.get_values(@form.errors, @field) do %>
<span class={"invalid-feedback #{@extra_class}"} phx-feedback-for={input_name(@form, @field)}>
<%= translate_error(error) %>
</span>
<% end %>
"""
end
@ -61,9 +60,18 @@ defmodule CanneryWeb.ErrorHelpers do
@spec changeset_errors(Changeset.t()) :: String.t()
def changeset_errors(changeset) do
changeset
|> Changeset.traverse_errors(fn error -> error |> translate_error() end)
|> changeset_error_map()
|> Enum.map_join(". ", fn {key, errors} ->
"#{key |> humanize()}: #{errors |> Enum.join(", ")}"
end)
end
@doc """
Displays all errors from a changeset in a key value map
"""
@spec changeset_error_map(Changeset.t()) :: %{atom() => [String.t()]}
def changeset_error_map(changeset) do
changeset
|> Changeset.traverse_errors(fn error -> error |> translate_error() end)
end
end

View File

@ -69,7 +69,7 @@ defmodule Cannery.MixProject do
setup: ["deps.get", "compile", "ecto.setup", "cmd npm install --prefix assets"],
"ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
"ecto.reset": ["ecto.drop", "ecto.setup"],
format: ["cmd npm run format --prefix assets", "format"],
format: ["cmd npm run format --prefix assets", "format", "gettext.extract"],
test: [
"cmd npm run test --prefix assets",
"dialyzer",