Routex.Extension.RuntimeCallbacks (Routex v1.2.0-rc.0)
View SourceThe RuntimeCallbacks extension enable to configure callback functions
-triggered by the Plug pipeline and LiveViews handle_params- by providing a
list of {module, function, arguments}
tuples. This is particularly useful
for integrating with internationalization libraries like:
- Gettext - Set language for translations
- Fluent - Set language for translations
- Cldr - Set locale for the Cldr suite
In combination with...
This extension calls other functions with values from Routex.Attrs
during
runtime. These attributes can be set by other extensions such as
Routex.Extension.Alternatives
(compile time) and
Routex.Extension.SimpleLocale
(run time)
Options
runtime_callbacks
- List of{module, function, arguments}
tuples. Any argument being a list starting with:attrs
is transformed intoget_in(attrs(), rest)
.
Example Configuration
defmodule MyApp.RoutexBackend do
use Routex.Backend,
extensions: [
Routex.Extension.Attrs,
+ Routex.Extension.RuntimeCallbacks
],
runtime_callbacks: [
+ # Set Gettext locale from :language attribute
+ {Gettext, :put_locale, [[:attrs, :language]]},
+ # Set CLDR locale from :locale attribute
+ {Cldr, :put_locale, [MyApp.Cldr, [:attrs, :locale]]}
]
end
Error Handling
The extension validates all callbacks during compilation to ensure the specified modules and functions exist:
- Checks if the module is loaded
- Verifies the function exists with correct arity
- Raises a compile-time error if validation fails
Example error:
** (RuntimeError) Gettext does not provide set_locale/1.
Please check the value of :runtime_callbacks in the Routex backend module
## Routex.Attrs
Requires
- none
Sets
- none
## Helpers runtime_callbacks(attrs :: T.attrs) :: :ok
Summary
Functions
A Phoenix Lifecycle Hook fetching the attributes from the socket and calling helper function runtime_callbacks/1
A plug fetching the attributes from the connection and calling helper function runtime_callbacks/1