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 - `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. 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`. - `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 # 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. # Swoosh API client is needed for adapters other than SMTP.
config :swoosh, :api_client, false config :swoosh, :api_client, false
# Gettext
config :gettext, :default_locale, "en_US"
# Configure esbuild (the version is required) # Configure esbuild (the version is required)
# config :esbuild, # config :esbuild,
# version: "0.14.0", # 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 config :cannery, CanneryWeb.Endpoint, server: true
end end
# Set locale
Gettext.put_locale(System.get_env("LOCALE") || "en_US")
maybe_ipv6 = if System.get_env("ECTO_IPV6"), do: [:inet6], else: [] maybe_ipv6 = if System.get_env("ECTO_IPV6"), do: [:inet6], else: []
database_url = database_url =

View File

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

View File

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