View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
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); //default is one day
55          startUpTime = System.currentTimeMillis();
56      }
57  
58      /**
59       * Returns the current language in ISO 639 (two character) format.
60       * 
61       * @return current language as plain text
62       */
63      @GET
64      @Produces(MediaType.TEXT_PLAIN)
65      @Path("language")
66      public String language() {
67          return MCRTranslation.getCurrentLocale().getLanguage();
68      }
69  
70      /**
71       * Returns all available languages of the application. The codes are in ISO 639 (two character) format.
72       * 
73       * @return json array of all languages available
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       * Translates a set of keys to the given language.
86       * 
87       * @param lang desired language
88       * @param key message key ending with an asterisk (e.g. component.classeditor.*)
89       * @return json object containing all keys and their corresponding translation
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      * Translates a set of keys to the current language.
102      *
103      * @param key message key ending with an asterisk (e.g. component.classeditor.*)
104      * @return json object containing all keys and their corresponding translation in current language
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      * Translates a single key to the given language.
117      * 
118      * @param lang desired language
119      * @param key the key to translate (e.g. component.classeditor.save.successful)
120      * @return translated plain text
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      * Translates a single key to the current language.
133      *
134      * @param key the key to translate (e.g. component.classeditor.save.successful)
135      * @return translated plain text
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 }