Skip to content

Commit 7405f84

Browse files
authored
Fix concatenation of sigils (#412)
1 parent 6a97765 commit 7405f84

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

lib/gettext/macros.ex

+19-5
Original file line numberDiff line numberDiff line change
@@ -709,15 +709,29 @@ defmodule Gettext.Macros do
709709
"""
710710
end
711711

712-
# We support nil too in order to fall back to a nil context and always use the *p
713-
# variants of the Gettext macros.
712+
validated_expand_to_binary(term, env, raiser)
713+
end
714+
715+
defp validated_expand_to_binary({:<>, _, pieces}, env, raiser) do
716+
Enum.map_join(pieces, &validated_expand_to_binary(&1, env, raiser))
717+
end
718+
719+
defp validated_expand_to_binary({:<<>>, _, pieces}, env, raiser) do
720+
Enum.map_join(pieces, &validated_expand_to_binary(&1, env, raiser))
721+
end
722+
723+
# We support nil too in order to fall back to a nil context and always use the *p
724+
# variants of the Gettext macros.
725+
defp validated_expand_to_binary(term, _env, _raiser)
726+
when is_binary(term) or is_nil(term) do
727+
term
728+
end
729+
730+
defp validated_expand_to_binary(term, env, raiser) do
714731
case Macro.expand(term, env) do
715732
term when is_binary(term) or is_nil(term) ->
716733
term
717734

718-
{:<<>>, _, pieces} = term ->
719-
if Enum.all?(pieces, &is_binary/1), do: Enum.join(pieces), else: raiser.(term)
720-
721735
other ->
722736
raiser.(other)
723737
end

test/gettext/macros_test.exs

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,19 @@ defmodule Gettext.MacrosTest do
1515
@backend Gettext.MacrosTest.Translator
1616
@gettext_msgid "Hello world"
1717

18-
describe "gettext/2" do
18+
describe "gettext/1" do
1919
test "supports binary-ish msgid at compile-time" do
2020
Gettext.put_locale(@backend, "it")
2121
assert gettext("Hello world") == "Ciao mondo"
2222
assert gettext(@gettext_msgid) == "Ciao mondo"
2323
assert gettext(~s(Hello world)) == "Ciao mondo"
24+
assert gettext("Hello " <> "world") == "Ciao mondo"
25+
assert gettext("Hello " <> "wor" <> "ld") == "Ciao mondo"
26+
assert gettext(<<"Hello world">>) == "Ciao mondo"
27+
assert gettext(<<"Hello ", "world">>) == "Ciao mondo"
28+
assert gettext("Hello " <> <<"wor", "ld">>) == "Ciao mondo"
29+
assert gettext("Hello " <> ~s(world)) == "Ciao mondo"
30+
assert gettext(~S(Hello ) <> ~s(world)) == "Ciao mondo"
2431
end
2532
end
2633

0 commit comments

Comments
 (0)