用elixir gettext标记要翻译的字符串列表(在编译时)



我正在尝试标记这个字符串列表可翻译,以便它出现在。pot文件中。

import XXXWeb.Gettext
@crop_types ~w(grape strawberry potato tomato rice wheat corn peas hops carrot soy sunflower other) 
|> Enum.map(&(XXXWeb.Gettext.dgettext_noop("crop_types", &1)))

根据文档,当我运行mix gettext.extract --merge时,这应该导致字符串在通常的。pot文件中可翻译。

我想避免使用"get gettext动态翻译,并在编译时显式标记要翻译的字符串,以便将来当我添加字符串而忘记其中一种语言的翻译时,不会丢失任何内容。

得到的错误是:

== Compilation error in file lib/XXX/fields/crop_types.ex ==
** (ArgumentError) Gettext macros expect translation keys (msgid and msgid_plural),
domains, and comments to expand to strings at compile-time, but the given msgid
doesn't. This is what the macro received:
{:x1, [line: 5], :elixir_fn}

我读到这是由于AST首先尝试运行gettext,然后运行enum函数,这不是我想要的。但是那部分我不太确定,所以请纠正我。

我该怎么做呢?

编辑:为了安全起见,这里是我的gettext模块:

defmodule MyAppWeb.Gettext do
use Gettext, otp_app: :myapp
end

我认为这种方法应该有效:

defmodule XXXWeb.Gettext do
use Gettext, otp_app: :thing
defmacro extract_gettext_strings(crops) do
{crop_types, _} = Code.eval_quoted(crops)
for crop <- crop_types do
quote do
XXXWeb.Gettext.dgettext_noop("crop_types", unquote(crop))
end
end
end
end
defmodule G do
require XXXWeb.Gettext
XXXWeb.Gettext.extract_gettext_strings(~w(grape strawberry potato tomato rice wheat corn peas hops carrot soy sunflower other))
end

最新更新