<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="348145" data-post-id="348145">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="maennchen" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/maennchen/120/32742_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  maennchen
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>This works with a slight change (needs a quote / unquote cycle, not sure why):</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule DummyGettext do
  defmacro dummy_macro(input, _lang \\ "en") do
    # gettext macros require literals at this point (not within quote do … end block)
    IO.inspect(input)
    is_binary(input) == false &amp;&amp; raise "gettext fail here"
  end

  def get_locales, do: ["en"]
end

defmodule MyLib.DSL do
  defmacro __using__(_opts \\ []) do
    quote do
      import MyLib.DSL

      require DummyGettext

      def translated_input(input, locale \\ "en")
    end
  end

  defmacro some_dsl(input) do
    quote do
      def translated_input(unquote(input), locale) do
        DummyGettext.dummy_macro(unquote(input), locale)
      end
    end
  end
end

defmodule MyApp.Example do
  use MyLib.DSL

  # this one obviously works
  some_dsl "text to translate"

  # my macros are fine with that, but gettext requires literal at compile time
  # so below code fails
  for data &lt;- ["first", "second", "third"] do
    some_dsl unquote(data)
  end
end
</code></pre>
<p>There’s no need to create a function implementation per message / locale since gettext will already do that for you.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348145" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/extract-gettext-translations-inside-macro/67821/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-348145" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348145"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="348147" data-post-id="348147">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Eiji" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Eiji/120/36743_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Eiji
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="maennchen" data-post="12" data-topic="67821">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/maennchen/48/32742_2.png" class="avatar"> maennchen:</div>
<blockquote>
<p>This works with a slight change (needs a quote / unquote cycle, not sure why):</p>
</blockquote>
</aside>
<p>Thanks, but this is not best for 2 reasons:</p>
<ol>
<li><code>data</code> is better than <code>unquote(data)</code> - my DSL designed to be simply, so everyone can use it without …</li>
<li><code>not sure why ):</code> <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></li>
</ol>
<p>My main issue was about extracting message and private APIs. Generating a <code>.pot</code> file is best in my case.</p>
<aside class="quote no-group" data-username="maennchen" data-post="12" data-topic="67821">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/maennchen/48/32742_2.png" class="avatar"> maennchen:</div>
<blockquote>
<p>There’s no need to create a function implementation per message / locale since gettext will already do that for you.</p>
</blockquote>
</aside>
<p>That’s why it’s a small example. It’s part of a bigger DSL designed for fast runtime execution i.e. the <code>gettext</code> calls would happen only in compile time and yes in my case it makes sense, a very bad … I mean good … very good one! <img src="https://forum.elixirforum.com/images/emoji/apple/smiling_imp.png?v=15" title=":smiling_imp:" class="emoji" alt=":smiling_imp:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348147" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/extract-gettext-translations-inside-macro/67821/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-348147" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348147"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="348148" data-post-id="348148">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="maennchen" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/maennchen/120/32742_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  maennchen
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="Eiji" data-post="13" data-topic="67821">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eiji/48/36743_2.png" class="avatar"> Eiji:</div>
<blockquote>
<p><code>data</code> is better than <code>unquote(data)</code> - my DSL designed to be simply, so everyone can use it without …</p>
</blockquote>
</aside>
<p>True, it’s a bit ugly. I wonder if that variable could be expanded somehow since unquote clearly also seems to be able to.</p>
<aside class="quote no-group" data-username="Eiji" data-post="13" data-topic="67821">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eiji/48/36743_2.png" class="avatar"> Eiji:</div>
<blockquote>
<p>My main issue was about extracting message and private APIs. Generating a <code>.pot</code> file is best in my case.</p>
</blockquote>
</aside>
<p>Sure, that works as well. Just make sure to either use a different domain from normal gettext or to not add the <code>elixir-autogen</code> flag. Otherwise you will get conflicts.</p>
<aside class="quote no-group" data-username="Eiji" data-post="13" data-topic="67821">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eiji/48/36743_2.png" class="avatar"> Eiji:</div>
<blockquote>
<p>i.e. the <code>gettext</code> calls would happen only in compile time</p>
</blockquote>
</aside>
<p>Gettext is already defining a function body for each message / locale:</p>
<p><a href="https://github.com/elixir-gettext/gettext/blob/3a6e81ce64ae580af300c2abf825fef310314b2c/lib/gettext/compiler.ex#L605-L615" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/elixir-gettext/gettext/blob/3a6e81ce64ae580af300c2abf825fef310314b2c/lib/gettext/compiler.ex#L605-L615</a></p>
<p>Doing it again will not make it faster <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348148" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/extract-gettext-translations-inside-macro/67821/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-348148" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348148"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="348150" data-post-id="348150">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Eiji" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Eiji/120/36743_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Eiji
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="maennchen" data-post="14" data-topic="67821">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/maennchen/48/32742_2.png" class="avatar"> maennchen:</div>
<blockquote>
<p>Sure, that works as well. Just make sure to either use a different domain from normal gettext or to not add the <code>elixir-autogen</code> flag. Otherwise you will get conflicts.</p>
</blockquote>
</aside>
<p>No need to worry about that. I’m using a custom domain every time. <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>Also I’m not using any flag, because I generate a <code>.pot</code> file every time the module is re-compiled, so any new change will regenerate the <strong>template</strong> (pot file). Then I let <code>gettext</code> handle <code>.po</code> files.</p>
<aside class="quote no-group" data-username="maennchen" data-post="14" data-topic="67821">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/maennchen/48/32742_2.png" class="avatar"> maennchen:</div>
<blockquote>
<p>Gettext is already defining a function body for each message / locale:</p>
</blockquote>
</aside>
<p>Good point, I did not know that … It looks very complicated, so let’s summarise it if anyone is interested:</p>
<ol>
<li>Function in <code>Gettext</code> module calls a generated <code>backend</code> function</li>
<li>When compiling backend (<code>use Gettext.Backend,  …</code>) <code>@before_compile Gettext.Compiler</code> is generated</li>
<li>So before backend module compiles a compiler module is then generating a function from <code>.po</code> files</li>
</ol>
<aside class="quote no-group" data-username="maennchen" data-post="14" data-topic="67821">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/maennchen/48/32742_2.png" class="avatar"> maennchen:</div>
<blockquote>
<p>Doing it again will not make it faster <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
</blockquote>
</aside>
<p>I understand that, but I still use it as I’m not returning just translated text, so I can simply translate a text at compile time or work with <code>quoted</code> gettext call when dealing with AST from DSL. <img src="https://forum.elixirforum.com/images/emoji/apple/thinking.png?v=15" title=":thinking:" class="emoji" alt=":thinking:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348150" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/extract-gettext-translations-inside-macro/67821/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-348150" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348150"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>