tugayac

tugayac

Trouble Adding Gettext Support to Elixir Library

Hey folks :waving_hand:,

I’m one of the maintainers of the Lotus Web library and we had a request to add i18n support for the library. The requester was nice enough to help with a PR here, but I’m having issues getting it to work locally.

If anyone has experience adding Gettext support to libraries, I could use some help understanding the following (posting here because I don’t believe this is a Phoenix-related issue):

  1. Why would Gettext not pick up the correct translations, even though the locale seems to be set correctly in the LiveView process? (see PR comment for details)
  2. Are we adding Gettext support to Lotus Web in the right way, with the goal of allowing users of the library to provide their own translations?
  3. Is there a recommended way of adding Gettext to Elixir libraries? (I couldn’t find any suggestions in the Gettext docs).

Here’s a diff of what I’m trying (also included in the PR discussion) in another Elixir app that uses this branch:

diff --git a/config/config.exs b/config/config.exs
index 5062c76e..32e717e0 100644
--- a/config/config.exs
+++ b/config/config.exs
+config :lotus_web, Lotus.Web.Gettext,
+  priv: "priv/lotus_gettext",
+  locales: ~w(en tr),
+  default_locale: "tr"
+
 import_config "#{config_env()}.exs"
diff --git a/lib/accomplish_web/router.ex b/lib/accomplish_web/router.ex
index 951b8e80..defb9e4c 100644
--- a/lib/accomplish_web/router.ex
+++ b/lib/accomplish_web/router.ex
@@ -48,7 +48,7 @@ defmodule AccomplishWeb.Router do
   scope "/admin", AccomplishWeb.Admin do
     pipe_through :admin
 
-    lotus_dashboard("/lotus")
+    lotus_dashboard("/lotus", on_mount: [{AccomplishWeb.LocaleHook, :default}])
   end
diff --git a/mix.exs b/mix.exs
index ffd13aab..e1e36812 100644
--- a/mix.exs
+++ b/mix.exs
@@ -87,7 +87,7 @@ defmodule Accomplish.MixProject do
-      {:lotus_web, "~> 0.6.2"},
+      {:lotus_web, path: "../lotus_web_branch"},

Here’s the Gettext directory structure in my app:


For reference, default_domain in the PR (for the Lotus Web library) is set to lotus, hence lotus.po.

Marked As Solved

derek-zhou

derek-zhou

Each of the libraries from the dependency is a OTP application with it’s own priv dir, and priv/gettext dir. So, I am not sure if you can achieve e your goal of “allowing users of the library to provide their own translations”. Translations are supposed to be faithful; so can you just accept the translation upstream?

Last Post!

tugayac

tugayac

@LostKobrakai I was referring to using the Gettext functions instead of macros when I said “dynamic translation features of Gettext” (i.e. this section of the Gettext docs), in which case the compile order shouldn’t matter (because translations should be fetched at runtime, but maybe I misunderstand how Gettext works?).

With Gettext macros, yes what you said makes sense :+1:

Given the context I’d probably agree with the suggestion to keep the translations as part of the package though. I can see users wanting to have the UI localized, but less so the ask for customizing the localizations beyond what would be useful to all users.

Yep, we decided to go that direction :slightly_smiling_face:

Where Next?

Popular in Questions Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40042 209
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement