Hello,
I was reading the Phoenix Guides on module plugs here and was confused as to how the module plug example was working. I was going to ask here for help, but in the process I believe I figured it out. (Happy to be corrected if that’s not the case).
I’ll just leave this here in case someone else is confused.
Line -3 (plug HelloPhoenix.Plugs.Locale, "en"
) is indicating that, when this plug is executed, we will invoke the HelloPhoenix.Plugs.Locale.init/1
function, passing in the argument "en"
.
The init/1
function will return a value, which will be used as the second argument to HelloPhoenix.Plugs.Locale.call/2
.
If the first clause of call/2
matches, then we know that:
- There was a
locale
URL parameter - The value of that parameter was in
@locale
We are happy with this so we assign the locale value to the connection.
If the second-clause of call/2
matches, then we know that we need to provide a default, because there was either no locale
URL parameter, or the value of it was not in @locale
. The default, again, came from whatever init/1
returned, which in the example is "en"
.