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