Skip to content

Commit 167ecd4

Browse files
committed
Add an example of internationalization within a Ring/Compojure web application using the Spring mechanisms.
1 parent 3d26e1e commit 167ecd4

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

resources/applicationContext.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
5+
<import resource="security.xml"/>
6+
<import resource="messages.xml"/>
7+
</beans>

resources/messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
greeting=Hello to you, {0}

resources/messages.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:p="http://www.springframework.org/schema/p"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
6+
<bean name="messageSource"
7+
class="org.springframework.context.support.ResourceBundleMessageSource"
8+
p:basename="messages"/>
9+
</beans>

src/ring_spring_security/core.clj

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@
1212
(:use hiccup.core)
1313
(:use hiccup.page-helpers))
1414

15-
(declare index-html example-html)
15+
(declare index-html example-html messages-html)
1616

1717
(defroutes main-routes
1818
(GET "/" req (index-html req))
1919
(GET "/admin" req (example-html req))
2020
(GET "/user" req (example-html req))
2121
(GET "/other" req (example-html req))
22+
(GET "/messages" req (messages-html req))
2223
(route/resources "/")
2324
(route/not-found "Page not found"))
2425

26+
;Some general utilities and wrappers
27+
2528
(defn application-context
2629
([req]
2730
(org.springframework.web.context.support.WebApplicationContextUtils/getWebApplicationContext
@@ -33,16 +36,30 @@
3336
(defn security-context []
3437
(org.springframework.security.core.context.SecurityContextHolder/getContext))
3538

39+
(defn message [req key & args]
40+
(let [context (application-context req)]
41+
(.getMessage
42+
context
43+
(if (keyword? key) (name key) key)
44+
(into-array java.lang.Object args)
45+
(.getLocale (:servlet-request req)))))
46+
3647
(defn wrap-reload-spring [app]
3748
"Reload the Spring application context on every HTTP request"
3849
(fn [req]
3950
(let [application-context (application-context req)]
4051
(.refresh application-context)
4152
(app req))))
4253

54+
(defn wrap-dump [app]
55+
(fn [req]
56+
(println req)
57+
(app req)))
58+
4359
(def app
4460
(-> (handler/site main-routes)
45-
; (wrap-reload-spring)
61+
(wrap-reload-spring)
62+
(wrap-dump)
4663
(wrap-reload '(ring-spring-security.core))
4764
(wrap-stacktrace)))
4865

@@ -70,6 +87,12 @@
7087
[:li (link-to "/user" "/user")]
7188
[:li (link-to "/other" "/other")]]))
7289

90+
(defn messages-html [req]
91+
(layout
92+
[:p "Here's some messages in your locale:"]
93+
[:ul
94+
[:li (escape-html (message req :greeting "asdf"))]]))
95+
7396
(defn- boot-spring
7497
"Initialize a Jetty server for Spring and also Spring Security"
7598
([server handler context-config-location]
@@ -94,7 +117,7 @@
94117
{:port 9090
95118
:join? join?
96119
:configurator (fn [server]
97-
(boot-spring server #'app "classpath:security.xml"))})]
120+
(boot-spring server #'app "classpath:applicationContext.xml"))})]
98121
;this is a hack to get around the handler ring-adapter-jetty forces on us
99122
(doseq [handler (remove #(= (class %) org.mortbay.jetty.servlet.Context) (seq (.getHandlers server)))]
100123
(.removeHandler server handler))))

0 commit comments

Comments
 (0)