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