1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.frontend.jersey.resources;
20
21 import java.util.Set;
22
23 import org.mycore.common.MCRJSONUtils;
24 import org.mycore.common.MCRSessionMgr;
25 import org.mycore.frontend.MCRFrontendUtil;
26 import org.mycore.frontend.jersey.MCRJerseyUtil;
27 import org.mycore.frontend.jersey.MCRStaticContent;
28 import org.mycore.services.i18n.MCRTranslation;
29
30 import jakarta.annotation.PostConstruct;
31 import jakarta.servlet.ServletContext;
32 import jakarta.servlet.http.HttpServletResponse;
33 import jakarta.ws.rs.GET;
34 import jakarta.ws.rs.Path;
35 import jakarta.ws.rs.PathParam;
36 import jakarta.ws.rs.Produces;
37 import jakarta.ws.rs.core.Context;
38 import jakarta.ws.rs.core.MediaType;
39
40 @Path("locale")
41 public class MCRLocaleResource {
42
43 @Context
44 private HttpServletResponse resp;
45
46 @Context
47 private ServletContext context;
48
49 private long cacheTime, startUpTime;
50
51 @PostConstruct
52 public void init() {
53 String cacheParam = context.getInitParameter("cacheTime");
54 cacheTime = cacheParam != null ? Long.parseLong(cacheParam) : (60 * 60 * 24);
55 startUpTime = System.currentTimeMillis();
56 }
57
58
59
60
61
62
63 @GET
64 @Produces(MediaType.TEXT_PLAIN)
65 @Path("language")
66 public String language() {
67 return MCRTranslation.getCurrentLocale().getLanguage();
68 }
69
70
71
72
73
74
75 @GET
76 @Produces(MCRJerseyUtil.APPLICATION_JSON_UTF8)
77 @Path("languages")
78 @MCRStaticContent
79 public String languages() {
80 Set<String> availableLanguages = MCRTranslation.getAvailableLanguages();
81 return MCRJSONUtils.getJsonArray(availableLanguages).toString();
82 }
83
84
85
86
87
88
89
90
91 @GET
92 @Produces(MCRJerseyUtil.APPLICATION_JSON_UTF8)
93 @Path("translate/{lang}/{key: .*\\*}")
94 @MCRStaticContent
95 public String translateJSON(@PathParam("lang") String lang, @PathParam("key") String key) {
96 MCRFrontendUtil.writeCacheHeaders(resp, cacheTime, startUpTime, true);
97 return MCRJSONUtils.getTranslations(key, lang);
98 }
99
100
101
102
103
104
105
106 @GET
107 @Produces(MCRJerseyUtil.APPLICATION_JSON_UTF8)
108 @Path("translate/{key: .*\\*}")
109 public String translateJSONDefault(@PathParam("key") String key) {
110 MCRFrontendUtil.writeCacheHeaders(resp, cacheTime, startUpTime, true);
111 return MCRJSONUtils.getTranslations(key.substring(0, key.length() - 1),
112 MCRSessionMgr.getCurrentSession().getCurrentLanguage());
113 }
114
115
116
117
118
119
120
121
122 @GET
123 @Produces(MediaType.TEXT_PLAIN)
124 @Path("translate/{lang}/{key: [^\\*]+}")
125 @MCRStaticContent
126 public String translateText(@PathParam("lang") String lang, @PathParam("key") String key) {
127 MCRFrontendUtil.writeCacheHeaders(resp, cacheTime, startUpTime, true);
128 return MCRTranslation.translate(key, MCRTranslation.getLocale(lang));
129 }
130
131
132
133
134
135
136
137 @GET
138 @Produces(MediaType.TEXT_PLAIN)
139 @Path("translate/{key: [^\\*]+}")
140 public String translateTextDefault(@PathParam("key") String key) {
141 MCRFrontendUtil.writeCacheHeaders(resp, cacheTime, startUpTime, true);
142 return MCRTranslation.translate(key, MCRTranslation.getCurrentLocale());
143 }
144
145 }