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