This plugin provides an implementation of the TimeZoneResolver interface, which is used to find the best TimeZone for the current user. The implementation, TimeZoneResolverDefault, tries to find the best TimeZone this way:
UTC
" by default.
spincast_timezone
" by default.
TimeZone
to use
inside a Route Handler
:
public void myHandler(AppRequestContext context) { TimeZone timeZoneToUse = context.getTimeZoneToUse(); //... }
An extension for Pebble, the default Templating Engine, is provided. The interface of this extension is SpincastTimeZonePebbleExtension and the default implementation is SpincastTimeZonePebbleExtensionDefault.
The main function provided by this extension, "timeZoneCookie()
", outputs a script that gets the timeZone of
the user with javascript and stores the result in a cookie.
For example, in your HTML
header:
<!doctype html> <html> <head> // ... {{ timeZoneCookie(true) }} // ... </head>
If "true
" is passed as a parameter to this function, "<script></script>
" tags will be added
around the script. If "false
" they won't.
The are a couple of options for this script in the
configurations
of the plugin. An interesting one is "isRefreshPageAfterAddingPebbleTimeZoneCookie()
": if this configuration is
"true
", then the page will be automatically reloaded the very first time the script sets the timeZone cookie. This allows your
application to always have access to the timeZone of the user, even on the very first page he visits! For this to work well, it is suggested that
you add the script quite high in the "<head>
" section of your layout, before the core of the page is loaded.
If you use the spincast-default
artifact and the standard Bootstrapper,
this plugin is already installed by default so you have nothing to do!
If you start from scratch, using the spincast-core
artifact, you can use the
plugin by :
1. Adding this Maven artifact to your project:
<dependency> <groupId>org.spincast</groupId> <artifactId>spincast-plugins-timezone-resolver</artifactId> <version>2.2.0</version> </dependency>
2. Installing the provided SpincastTimeZoneResolverPluginModule module to your Guice context.
The class implementing the SpincastPlugin interface is SpincastTimeZoneResolverPlugin.
You can bind a SpincastTimeZoneResolverConfig implementation to tweak the default configurations used by this plugin. By default, the SpincastTimeZoneResolverConfigDefault class is used as the implementation.