1
19
20 package com.liferay.portal.servlet;
21
22 import com.liferay.portal.kernel.language.LanguageUtil;
23 import com.liferay.portal.kernel.log.Log;
24 import com.liferay.portal.kernel.log.LogFactoryUtil;
25 import com.liferay.portal.kernel.util.ContentTypes;
26 import com.liferay.portal.kernel.util.LocaleUtil;
27 import com.liferay.portal.kernel.util.StringPool;
28 import com.liferay.portal.kernel.util.StringUtil;
29 import com.liferay.portal.kernel.util.Validator;
30 import com.liferay.portal.util.PortalInstances;
31 import com.liferay.util.servlet.ServletResponseUtil;
32
33 import java.io.IOException;
34
35 import java.util.Locale;
36
37 import javax.servlet.http.HttpServlet;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40
41
47 public class LanguageServlet extends HttpServlet {
48
49 public void service(
50 HttpServletRequest request, HttpServletResponse response)
51 throws IOException {
52
53 String path = request.getPathInfo();
54
55 if (_log.isDebugEnabled()) {
56 _log.debug("Path " + path);
57 }
58
59 if (Validator.isNotNull(path) && path.startsWith(StringPool.SLASH)) {
60 path = path.substring(1, path.length());
61 }
62
63 String[] pathArray = StringUtil.split(path, StringPool.SLASH);
64
65 if (pathArray.length == 0) {
66 _log.error("Language id is not specified");
67
68 return;
69 }
70
71 if (pathArray.length == 1) {
72 _log.error("Language key is not specified");
73
74 return;
75 }
76
77 Locale locale = LocaleUtil.fromLanguageId(pathArray[0]);
78 String key = pathArray[1];
79
80 Object[] arguments = null;
81
82 if (pathArray.length > 2) {
83 arguments = new Object[pathArray.length - 2];
84
85 System.arraycopy(pathArray, 2, arguments, 0, arguments.length);
86 }
87
88 String value = key;
89
90 try {
91 long companyId = PortalInstances.getCompanyId(request);
92
93 if ((arguments == null) || (arguments.length == 0)) {
94 value = LanguageUtil.get(companyId, locale, key);
95 }
96 else {
97 value = LanguageUtil.format(companyId, locale, key, arguments);
98 }
99 }
100 catch (Exception e) {
101 if (_log.isWarnEnabled()) {
102 _log.warn(e, e);
103 }
104 }
105
106 response.setContentType(ContentTypes.TEXT_PLAIN_UTF8);
107
108 ServletResponseUtil.write(response, value.getBytes(StringPool.UTF8));
109 }
110
111 private static Log _log = LogFactoryUtil.getLog(LanguageServlet.class);
112
113 }