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
50 public class LanguageServlet extends HttpServlet {
51
52 public void service(
53 HttpServletRequest request, HttpServletResponse response)
54 throws IOException {
55
56 String path = request.getPathInfo();
57
58 if (_log.isDebugEnabled()) {
59 _log.debug("Path " + path);
60 }
61
62 if (Validator.isNotNull(path) && path.startsWith(StringPool.SLASH)) {
63 path = path.substring(1, path.length());
64 }
65
66 String[] pathArray = StringUtil.split(path, StringPool.SLASH);
67
68 if (pathArray.length == 0) {
69 _log.error("Language id is not specified");
70
71 return;
72 }
73
74 if (pathArray.length == 1) {
75 _log.error("Language key is not specified");
76
77 return;
78 }
79
80 Locale locale = LocaleUtil.fromLanguageId(pathArray[0]);
81 String key = pathArray[1];
82
83 Object[] arguments = null;
84
85 if (pathArray.length > 2) {
86 arguments = new Object[pathArray.length - 2];
87
88 System.arraycopy(pathArray, 2, arguments, 0, arguments.length);
89 }
90
91 String value = key;
92
93 try {
94 long companyId = PortalInstances.getCompanyId(request);
95
96 if ((arguments == null) || (arguments.length == 0)) {
97 value = LanguageUtil.get(companyId, locale, key);
98 }
99 else {
100 value = LanguageUtil.format(companyId, locale, key, arguments);
101 }
102 }
103 catch (Exception e) {
104 if (_log.isWarnEnabled()) {
105 _log.warn(e, e);
106 }
107 }
108
109 response.setContentType(ContentTypes.TEXT_PLAIN_UTF8);
110
111 ServletResponseUtil.write(response, value.getBytes(StringPool.UTF8));
112 }
113
114 private static Log _log = LogFactoryUtil.getLog(LanguageServlet.class);
115
116 }