Friday 15 April 2011

I18n portlets

Supporting multilingual content is very important in my organization. Unfortunately not everything is so easy as it might seem. Today I solved a little problem which was bugging probationer who is under my guidance. As it turned out, there were two major problems with this tutorial:

  1. He was using wrong fmt.tld file. The best thing is to define taglib using this line (notice the difference from line in tutorial):
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
  2. Ssecond pitfall was that uPortal implementation which we use, didn't set locale automagically. If your portal implementation isn't setting this attribute (with key 'javax.servlet.jsp.jstl.fmt.locale'), then use these lines (the first one is needed in order to define renderRequest value):
    <portlet:defineObjects />
    <fmt:setLocale value="${renderRequest.locale}"/>

Ok, now putting all together if you need to translate content in JSP:

  1. Check that your portlet supports locales of your choice
    <portlet>
    ...
    <supported-locale>en</supported-locale>
    <supported-locale>lv</supported-locale>
    ...
    </portlet>
  2. Create files named messages.properties and messages_lv.properties in package com.example with key-value pairs of your choice. For example:
    messages.properties:
    welcome=Welcome to my page!

    messages_lv.properties:
    welcome=Esiet sveicināti manā mājas lapā!
  3. Add standard library to classpath of your web application (i.e. in WEB-INF/lib) and define use of formatting taglib in JSP page, specify locale, and translate:
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    <%@ taglib prefix="portlet" uri="http://java.sun.com/portlet" %>
    ...
    <portlet:defineObjects />
    <fmt:setLocale value="${renderRequest.locale}"/>
    <fmt:setBundle basename="com.example.messages"/>
    <h1><fmt:message key="welcome"/></h1>

Hope this helps

Thursday 7 April 2011

Trusting self signed certificate in java

Simple version of how to trust self-signed certificates.
$ wget https://raw.githubusercontent.com/escline/InstallCert/master/InstallCert.java
$ javac InstallCert.java 
$ java InstallCert host:port
$ cp jssecacerts /usr/java/jdk1.6.0_21/jre/lib/security/

restart JVM and you're done. Of course, you can always choose longer path and follow JSSE documentation