Why is the gettext module created in the web context in a phoenix application?

Hey. I’m currently developing my first application using gettext and I noticed that with the default generator, the Gettext module gets created in the MyAppWeb context. Now I’m wondering why it’s put there and not in the main MyApp context.

I have a few places where I would need the Gettext module within my main context. Use cases where I would need translations inside of the MyApp context:

  • default values for Ecto schemas
  • E-Mail text

Any suggestions how I should handle that? Or should I simply move the Gettext module inside MyApp.Gettext?

3 Likes

I would move it to MyApp in that case. Being in MyAppWeb is only a suggestion in the basic project layout, you don’t need to adhere to it. Maybe some config settings need to be changed to point to the new module name though.

1 Like

Thanks. That’s probably what I’m going to do. I just wanted to make sure that I’m not missing something obvious - like that it’s absolutely discouraged to do so :slight_smile:

Locale is per process; and is usually set up through a plug so the controller context has the right locale. In your own process like in a GenServer, you need to do the setup yourself, and you will need to consider the cases when different clients are using different locales at the same time, so it may become less clear what is the right locale.

4 Likes