From 22abc7a8d0337529d92974ea29ab44a1d2c109ec Mon Sep 17 00:00:00 2001 From: shibao Date: Wed, 25 Jan 2023 21:50:14 -0500 Subject: [PATCH] improve locale --- config/runtime.exs | 21 +++++++++++---------- lib/lokal_web/router.ex | 8 +++++--- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/config/runtime.exs b/config/runtime.exs index b1532fb3..456d8e52 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -13,17 +13,18 @@ if System.get_env("PHX_SERVER") && System.get_env("RELEASE_NAME") do end # Set default locale -config :gettext, :default_locale, System.get_env("LOCALE") || "en_US" +config :gettext, :default_locale, System.get_env("LOCALE", "en_US") maybe_ipv6 = if System.get_env("ECTO_IPV6") == "true", do: [:inet6], else: [] database_url = if config_env() == :test do - System.get_env("TEST_DATABASE_URL") || + System.get_env( + "TEST_DATABASE_URL", "ecto://postgres:postgres@localhost/lokal_test#{System.get_env("MIX_TEST_PARTITION")}" + ) else - System.get_env("DATABASE_URL") || - "ecto://postgres:postgres@lokal-db/lokal" + System.get_env("DATABASE_URL", "ecto://postgres:postgres@lokal-db/lokal") end host = @@ -38,7 +39,7 @@ interface = config :lokal, Lokal.Repo, # ssl: true, url: database_url, - pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"), + pool_size: String.to_integer(System.get_env("POOL_SIZE", "10")), socket_options: maybe_ipv6 config :lokal, LokalWeb.Endpoint, @@ -47,10 +48,10 @@ config :lokal, LokalWeb.Endpoint, # See the documentation on https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html # for details about using IPv6 vs IPv4 and loopback vs public addresses. ip: interface, - port: String.to_integer(System.get_env("PORT") || "4000") + port: String.to_integer(System.get_env("PORT", "4000")) ], server: true, - registration: System.get_env("REGISTRATION") || "invite" + registration: System.get_env("REGISTRATION", "invite") if config_env() == :prod do # The secret key base is used to sign/encrypt cookies and other secrets. @@ -74,12 +75,12 @@ if config_env() == :prod do config :lokal, Lokal.Mailer, adapter: Swoosh.Adapters.SMTP, relay: System.get_env("SMTP_HOST") || raise("No SMTP_HOST set!"), - port: System.get_env("SMTP_PORT") || 587, + port: System.get_env("SMTP_PORT", 587), username: System.get_env("SMTP_USERNAME") || raise("No SMTP_USERNAME set!"), password: System.get_env("SMTP_PASSWORD") || raise("No SMTP_PASSWORD set!"), ssl: System.get_env("SMTP_SSL") == "true", - email_from: System.get_env("EMAIL_FROM") || "no-reply@#{System.get_env("HOST")}", - email_name: System.get_env("EMAIL_NAME") || "Lokal" + email_from: System.get_env("EMAIL_FROM", "no-reply@#{System.get_env("HOST")}"), + email_name: System.get_env("EMAIL_NAME", "Lokal") # ## Using releases # diff --git a/lib/lokal_web/router.ex b/lib/lokal_web/router.ex index a826ad17..c99d9956 100644 --- a/lib/lokal_web/router.ex +++ b/lib/lokal_web/router.ex @@ -11,15 +11,17 @@ defmodule LokalWeb.Router do plug :protect_from_forgery plug :put_secure_browser_headers plug :fetch_current_user - plug :put_user_locale, default: Application.get_env(:gettext, :default_locale, "en_US") + plug :put_user_locale end - defp put_user_locale(%{assigns: %{current_user: %{locale: locale}}} = conn, default: default) do + defp put_user_locale(%{assigns: %{current_user: %{locale: locale}}} = conn, _opts) do + default = Application.fetch_env!(:gettext, :default_locale) Gettext.put_locale(locale || default) conn |> put_session(:locale, locale || default) end - defp put_user_locale(conn, default: default) do + defp put_user_locale(conn, _opts) do + default = Application.fetch_env!(:gettext, :default_locale) Gettext.put_locale(default) conn |> put_session(:locale, default) end