ColmB

ColmB

Gettext.extract not looking into .eex files (not using Phoenix)?

I am using EEx.eval_string in combination with eex template files as part of an email generation process. This is not a phoenix project. I am introducing gettext to do internationalisation, with calls to gettext inside the eex files. For example:

<b><%= gettext("This is a test for %{name}, %{description} test", name: name, description: description) %></b>

I noticed initially that this wouldn’t work unless I imported the gettext backend directly in the eex files, like this:

<% import Notification.Gettext %>

This works but the mix task to do the extraction into POT files doesn’t look into the EEX files. I see lots of examples of the mix gettext.extract task working inside EEX files but they are all Phoenix based and the import is being done inside one of the view files. In my case, is it possible to do the import in such a way that the extract task will realise it should include the EEX files?

Any help would be appreciated.

Marked As Solved

ColmB

ColmB

EEx.function_from_file did it! I wasn’t quite understanding what you were saying initially. Some test code for anyone else who lands here later:

defmodule Notification.EmailImpl.WelcomeEmailTemplate do
  @moduledoc false

  alias Notification.EmailImpl.EmailTemplateUtil
  import Notification.Gettext
  require EEx

  @external_resource template = Path.join([__DIR__, "template/welcome_template_email.html.eex"])

  EEx.function_from_file(:defp, :template_as_function, template, [:vars])

  def render(%{starts_on: _starts_on, ends_on: _ends_on} = variables) do
    vars = variables
    |> Map.update!(:starts_on, fn date -> EmailTemplateUtil.to_localised_date_string(date) end)
    |> Map.update!(:ends_on, fn date -> EmailTemplateUtil.to_localised_date_string(date) end)
    |> EmailTemplateUtil.compute_values()

    Gettext.with_locale("es", fn -> template_as_function(vars) end)
  end
end

and in the template:

<%= gettext("You have been enrolled in the following tutorial, %{name}, starting on %{starts_on}.", name: vars.name, starts_on: vars.starts_on) %>

Thanks LostKobrakai!

Also Liked

LostKobrakai

LostKobrakai

eval_string is executed at runtime. For gettext to pick up those values you need to use the EEx.compile_* functions.

LostKobrakai

LostKobrakai

You probably want both. Importing the backend in the module and compiling the template into that module. That’s how it works in phoenix.

Why not simply EEx.function_from_file?

LostKobrakai

LostKobrakai

Anything involving eval_* is runtime (unless you put it in a module body, which I guess you don’t). Extracting gettext strings requires code to be part of some module at compile time.

Where Next?

Popular in Questions Top

LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
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 42633 214
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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

We're in Beta

About us Mission Statement