Confusion regarding config syntax

In the documentation for Gettext, I came across the following code sample:

defmodule MyApp.Gettext do
use Gettext, otp_app: :my_app, default_domain: “messages”
end

config :my_app, MyApp.Gettext, default_domain: “translations”

When I try and run this in my environment, I get the error,:
** (CompileError) iex:2: undefined function config/3

And when I looked up the documentation for config, I found that sure enough, config should be specified as <app_name>, followed by a list of key value pairs … which the sample from the gettext documentation is not.
My question is, is that a typo in the Gettext documentation? Or am I missing something else?

Hi, please use code blocks, three or single ticks ( ` ) around code when posting code samples.

Where are you using config :my_app… ? That syntax is available, usually on config files, when using import Config.

The code example in the docs actually contains two separate examples. How to configure Gettext in the module with the use macro and how to configure it in a config file (config/*.exs). So the latter is only available in configuration files, after using import Config (or use Mix.Config in the old style).

2 Likes

Thanks for your response.
I put it right under the use Gettext... line, within my module MyApp.Gettext, so not in a config file. Although it should be possible to use it outside of a config file right?

I see. I tried commenting out the use Gettext... line, and adding import Config, but still get an ‘unexpected token’ error coming from my config... line.

I’m guessing that may be because I don’t actually have any config files in my project… is that right?

It looks like it’s giving you two ways to config it and you only need one. If you don’t have config files use the first one, if you do use the config line(in your config file)

1 Like

You would need to show the full code you have.

You shouldn’t use config lines outside the config files.

ok, I appreciate the example was showing those two different methods, of which it’s only necessary to use one. I’ve got the functionality I want working using the use way, so I’ll leave fighting with config for another day. Thanks for all the replies.

1 Like