Source Code Cross Referenced for LanguageImpl.java in  » Portal » liferay-portal-4.4.2 » com » liferay » portal » language » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » liferay portal 4.4.2 » com.liferay.portal.language 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003:         *
004:         * Permission is hereby granted, free of charge, to any person obtaining a copy
005:         * of this software and associated documentation files (the "Software"), to deal
006:         * in the Software without restriction, including without limitation the rights
007:         * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008:         * copies of the Software, and to permit persons to whom the Software is
009:         * furnished to do so, subject to the following conditions:
010:         *
011:         * The above copyright notice and this permission notice shall be included in
012:         * all copies or substantial portions of the Software.
013:         *
014:         * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015:         * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016:         * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017:         * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018:         * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019:         * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020:         * SOFTWARE.
021:         */package com.liferay.portal.language;
022:
023:        import com.liferay.portal.kernel.language.Language;
024:        import com.liferay.portal.kernel.language.LanguageWrapper;
025:        import com.liferay.portal.kernel.util.GetterUtil;
026:        import com.liferay.portal.kernel.util.JavaConstants;
027:        import com.liferay.portal.kernel.util.LocaleUtil;
028:        import com.liferay.portal.kernel.util.ParamUtil;
029:        import com.liferay.portal.kernel.util.StringPool;
030:        import com.liferay.portal.kernel.util.Validator;
031:        import com.liferay.portal.security.auth.CompanyThreadLocal;
032:        import com.liferay.portal.util.CookieKeys;
033:        import com.liferay.portal.util.PortalUtil;
034:        import com.liferay.portal.util.PropsValues;
035:        import com.liferay.portal.util.WebAppPool;
036:        import com.liferay.util.CollectionFactory;
037:        import com.liferay.util.Time;
038:
039:        import java.text.MessageFormat;
040:
041:        import java.util.Locale;
042:        import java.util.Map;
043:        import java.util.MissingResourceException;
044:        import java.util.ResourceBundle;
045:
046:        import javax.portlet.ActionRequest;
047:        import javax.portlet.PortletConfig;
048:        import javax.portlet.RenderRequest;
049:
050:        import javax.servlet.http.Cookie;
051:        import javax.servlet.http.HttpServletRequest;
052:        import javax.servlet.http.HttpServletResponse;
053:        import javax.servlet.jsp.PageContext;
054:
055:        import org.apache.commons.logging.Log;
056:        import org.apache.commons.logging.LogFactory;
057:        import org.apache.struts.Globals;
058:        import org.apache.struts.taglib.TagUtils;
059:        import org.apache.struts.util.MessageResources;
060:
061:        /**
062:         * <a href="LanguageImpl.java.html"><b><i>View Source</i></b></a>
063:         *
064:         * @author Brian Wing Shun Chan
065:         * @author Andrius Vitkauskas
066:         *
067:         */
068:        public class LanguageImpl implements  Language {
069:
070:            public static final String DEFAULT_ENCODING = "UTF-8";
071:
072:            public String format(Locale locale, String pattern, Object argument) {
073:                long companyId = CompanyThreadLocal.getCompanyId();
074:
075:                return format(companyId, locale, pattern,
076:                        new Object[] { argument });
077:            }
078:
079:            public String format(Locale locale, String pattern,
080:                    Object[] arguments) {
081:                long companyId = CompanyThreadLocal.getCompanyId();
082:
083:                return format(companyId, locale, pattern, arguments);
084:            }
085:
086:            public String format(long companyId, Locale locale, String pattern,
087:                    Object argument) {
088:
089:                return format(companyId, locale, pattern,
090:                        new Object[] { argument });
091:            }
092:
093:            public String format(long companyId, Locale locale, String pattern,
094:                    Object[] arguments) {
095:
096:                String value = null;
097:
098:                try {
099:                    pattern = get(companyId, locale, pattern);
100:
101:                    if (arguments != null) {
102:                        Object[] formattedArguments = new Object[arguments.length];
103:
104:                        for (int i = 0; i < arguments.length; i++) {
105:                            formattedArguments[i] = get(companyId, locale,
106:                                    arguments[i].toString());
107:                        }
108:
109:                        value = MessageFormat.format(pattern,
110:                                formattedArguments);
111:                    } else {
112:                        value = pattern;
113:                    }
114:                } catch (Exception e) {
115:                    if (_log.isWarnEnabled()) {
116:                        _log.warn(e.getMessage());
117:                    }
118:                }
119:
120:                return value;
121:            }
122:
123:            public String format(PageContext pageContext, String pattern,
124:                    Object argument) {
125:
126:                return format(pageContext, pattern, new Object[] { argument },
127:                        true);
128:            }
129:
130:            public String format(PageContext pageContext, String pattern,
131:                    Object argument, boolean translateArguments) {
132:
133:                return format(pageContext, pattern, new Object[] { argument },
134:                        translateArguments);
135:            }
136:
137:            public String format(PageContext pageContext, String pattern,
138:                    Object[] arguments) {
139:
140:                return format(pageContext, pattern, arguments, true);
141:            }
142:
143:            public String format(PageContext pageContext, String pattern,
144:                    Object[] arguments, boolean translateArguments) {
145:
146:                String value = null;
147:
148:                try {
149:                    pattern = get(pageContext, pattern);
150:
151:                    if (arguments != null) {
152:                        Object[] formattedArguments = new Object[arguments.length];
153:
154:                        for (int i = 0; i < arguments.length; i++) {
155:                            if (translateArguments) {
156:                                formattedArguments[i] = get(pageContext,
157:                                        arguments[i].toString());
158:                            } else {
159:                                formattedArguments[i] = arguments[i];
160:                            }
161:                        }
162:
163:                        value = MessageFormat.format(pattern,
164:                                formattedArguments);
165:                    } else {
166:                        value = pattern;
167:                    }
168:                } catch (Exception e) {
169:                    if (_log.isWarnEnabled()) {
170:                        _log.warn(e.getMessage());
171:                    }
172:                }
173:
174:                return value;
175:            }
176:
177:            public String format(PageContext pageContext, String pattern,
178:                    LanguageWrapper argument) {
179:
180:                return format(pageContext, pattern,
181:                        new LanguageWrapper[] { argument }, true);
182:            }
183:
184:            public String format(PageContext pageContext, String pattern,
185:                    LanguageWrapper argument, boolean translateArguments) {
186:
187:                return format(pageContext, pattern,
188:                        new LanguageWrapper[] { argument }, translateArguments);
189:            }
190:
191:            public String format(PageContext pageContext, String pattern,
192:                    LanguageWrapper[] arguments) {
193:
194:                return format(pageContext, pattern, arguments, true);
195:            }
196:
197:            public String format(PageContext pageContext, String pattern,
198:                    LanguageWrapper[] arguments, boolean translateArguments) {
199:
200:                String value = null;
201:
202:                try {
203:                    pattern = get(pageContext, pattern);
204:
205:                    if (arguments != null) {
206:                        Object[] formattedArguments = new Object[arguments.length];
207:
208:                        for (int i = 0; i < arguments.length; i++) {
209:                            if (translateArguments) {
210:                                formattedArguments[i] = arguments[i]
211:                                        .getBefore()
212:                                        + get(pageContext, arguments[i]
213:                                                .getText())
214:                                        + arguments[i].getAfter();
215:                            } else {
216:                                formattedArguments[i] = arguments[i]
217:                                        .getBefore()
218:                                        + arguments[i].getText()
219:                                        + arguments[i].getAfter();
220:                            }
221:                        }
222:
223:                        value = MessageFormat.format(pattern,
224:                                formattedArguments);
225:                    } else {
226:                        value = pattern;
227:                    }
228:                } catch (Exception e) {
229:                    if (_log.isWarnEnabled()) {
230:                        _log.warn(e.getMessage());
231:                    }
232:                }
233:
234:                return value;
235:            }
236:
237:            public String get(Locale locale, String key) {
238:                long companyId = CompanyThreadLocal.getCompanyId();
239:
240:                return get(companyId, locale, key, key);
241:            }
242:
243:            public String get(long companyId, Locale locale, String key) {
244:                return get(companyId, locale, key, key);
245:            }
246:
247:            public String get(long companyId, Locale locale, String key,
248:                    String defaultValue) {
249:
250:                if (key == null) {
251:                    return null;
252:                }
253:
254:                String value = null;
255:
256:                try {
257:                    MessageResources resources = (MessageResources) WebAppPool
258:                            .get(String.valueOf(companyId),
259:                                    Globals.MESSAGES_KEY);
260:
261:                    if (resources == null) {
262:
263:                        // LEP-4505
264:
265:                        ResourceBundle bundle = ResourceBundle.getBundle(
266:                                "content/Language", locale);
267:
268:                        value = bundle.getString(key);
269:                    } else {
270:                        value = resources.getMessage(locale, key);
271:                    }
272:                } catch (Exception e) {
273:                    if (_log.isWarnEnabled()) {
274:                        _log.warn(e.getMessage());
275:                    }
276:                }
277:
278:                if (value == null) {
279:                    value = defaultValue;
280:                }
281:
282:                return value;
283:            }
284:
285:            public String get(PageContext pageContext, String key) {
286:                return get(pageContext, key, key);
287:            }
288:
289:            public String get(PageContext pageContext, String key,
290:                    String defaultValue) {
291:
292:                if (key == null) {
293:                    return null;
294:                }
295:
296:                String value = null;
297:
298:                try {
299:                    value = TagUtils.getInstance().message(pageContext, null,
300:                            null, key);
301:                } catch (Exception e) {
302:                    _log.error(e);
303:                }
304:
305:                if (value == null) {
306:
307:                    // LEP-2849
308:
309:                    HttpServletRequest req = (HttpServletRequest) pageContext
310:                            .getRequest();
311:
312:                    PortletConfig portletConfig = (PortletConfig) req
313:                            .getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
314:
315:                    if (portletConfig != null) {
316:                        Locale locale = req.getLocale();
317:
318:                        ResourceBundle bundle = portletConfig
319:                                .getResourceBundle(locale);
320:
321:                        try {
322:                            value = bundle.getString(key);
323:                        } catch (MissingResourceException mre) {
324:                        }
325:                    }
326:                }
327:
328:                if (value == null) {
329:                    value = defaultValue;
330:                }
331:
332:                return value;
333:            }
334:
335:            public Locale[] getAvailableLocales() {
336:                return _getInstance()._locales;
337:            }
338:
339:            public String getCharset(Locale locale) {
340:                return _getInstance()._getCharset(locale);
341:            }
342:
343:            public String getLanguageId(ActionRequest req) {
344:                HttpServletRequest httpReq = PortalUtil
345:                        .getHttpServletRequest(req);
346:
347:                return getLanguageId(httpReq);
348:            }
349:
350:            public String getLanguageId(RenderRequest req) {
351:                HttpServletRequest httpReq = PortalUtil
352:                        .getHttpServletRequest(req);
353:
354:                return getLanguageId(httpReq);
355:            }
356:
357:            public String getLanguageId(HttpServletRequest req) {
358:                String languageId = ParamUtil.getString(req, "languageId");
359:
360:                if (Validator.isNotNull(languageId)) {
361:                    return languageId;
362:                }
363:
364:                Locale locale = (Locale) req.getSession().getAttribute(
365:                        Globals.LOCALE_KEY);
366:
367:                if (locale == null) {
368:                    languageId = CookieKeys.getCookie(req,
369:                            CookieKeys.GUEST_LANGUAGE_ID);
370:
371:                    if (Validator.isNotNull(languageId)) {
372:                        locale = LocaleUtil.fromLanguageId(languageId);
373:                    }
374:                }
375:
376:                return getLanguageId(locale);
377:            }
378:
379:            public String getLanguageId(Locale locale) {
380:                return LocaleUtil.toLanguageId(locale);
381:            }
382:
383:            public Locale getLocale(String languageCode) {
384:                return _getInstance()._getLocale(languageCode);
385:            }
386:
387:            public String getTimeDescription(PageContext pageContext,
388:                    Long milliseconds) {
389:
390:                return getTimeDescription(pageContext, milliseconds.longValue());
391:            }
392:
393:            public String getTimeDescription(PageContext pageContext,
394:                    long milliseconds) {
395:
396:                String desc = Time.getDescription(milliseconds);
397:
398:                String value = null;
399:
400:                try {
401:                    int pos = desc.indexOf(StringPool.SPACE);
402:
403:                    int x = GetterUtil.getInteger(desc.substring(0, pos));
404:
405:                    value = x
406:                            + " "
407:                            + get(pageContext, desc.substring(pos + 1,
408:                                    desc.length()).toLowerCase());
409:                } catch (Exception e) {
410:                    if (_log.isWarnEnabled()) {
411:                        _log.warn(e.getMessage());
412:                    }
413:                }
414:
415:                return value;
416:            }
417:
418:            public void updateCookie(HttpServletResponse res, Locale locale) {
419:                String languageId = LocaleUtil.toLanguageId(locale);
420:
421:                Cookie languageIdCookie = new Cookie(
422:                        CookieKeys.GUEST_LANGUAGE_ID, languageId);
423:
424:                languageIdCookie.setPath(StringPool.SLASH);
425:                languageIdCookie.setMaxAge(CookieKeys.MAX_AGE);
426:
427:                CookieKeys.addCookie(res, languageIdCookie);
428:            }
429:
430:            private static LanguageImpl _getInstance() {
431:                Long companyIdObj = new Long(CompanyThreadLocal.getCompanyId());
432:
433:                LanguageImpl instance = (LanguageImpl) _instances
434:                        .get(companyIdObj);
435:
436:                if (instance == null) {
437:                    instance = new LanguageImpl();
438:
439:                    _instances.put(companyIdObj, instance);
440:                }
441:
442:                return instance;
443:            }
444:
445:            private LanguageImpl() {
446:                String[] localesArray = PropsValues.LOCALES;
447:
448:                _locales = new Locale[localesArray.length];
449:                _localesByLanguageCode = CollectionFactory.getHashMap();
450:                _charEncodings = CollectionFactory.getHashMap();
451:
452:                for (int i = 0; i < localesArray.length; i++) {
453:                    String languageId = localesArray[i];
454:
455:                    int x = languageId.indexOf(StringPool.UNDERLINE);
456:
457:                    String language = languageId.substring(0, x);
458:                    //String country = languageId.substring(x + 1, languageId.length());
459:
460:                    Locale locale = LocaleUtil.fromLanguageId(languageId);
461:
462:                    _locales[i] = locale;
463:                    _localesByLanguageCode.put(language, locale);
464:                    _charEncodings.put(locale.toString(), DEFAULT_ENCODING);
465:                }
466:            }
467:
468:            private String _getCharset(Locale locale) {
469:                return DEFAULT_ENCODING;
470:            }
471:
472:            private Locale _getLocale(String languageCode) {
473:                return (Locale) _localesByLanguageCode.get(languageCode);
474:            }
475:
476:            private static Log _log = LogFactory.getLog(LanguageImpl.class);
477:
478:            private static Map _instances = CollectionFactory.getSyncHashMap();
479:
480:            private Locale[] _locales;
481:            private Map _localesByLanguageCode;
482:            private Map _charEncodings;
483:
484:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.