[Logo] Mentawai Forum - Mentawai Web Framework
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 

Forum Read-Only! Check the new site and forum here!

31) Como coloco "bandeirinhas" para os idiomas do meu site?  XML
Forum Index -> F.A.Q.
Author Message
saoj



Joined: 01/07/2005 09:59:17
Messages: 2846
Location: Rio de Janeiro, RJ
Offline

Na primeira página do seu site, importe o código JSP abaixo utilizando <%@ include file="/jsp/code/lang-cookies.jsp" %>

Code:
 <%@ page import="java.util.*, javax.servlet.*, javax.servlet.http.*" %>
 <%!
     
     public static Locale EN = new Locale("en", "US");
     
     public static Locale PT = new Locale("pt", "BR");
     
     private static final String COOKIE_NAME = "ecupido-loc";
 
     public static void addCookie(HttpServletResponse response, String key, String value) {
         Cookie c = new Cookie(key, value);
         c.setMaxAge(31104000);
         c.setPath("/");
         response.addCookie(c);
     }
     
     public static String getCookie(HttpServletRequest request, String key) {
         Cookie[] cookies = request.getCookies();
 
         if (cookies != null) {
 	        for (int i = 0; i < cookies.length; i++) {
     	        if (cookies[i].getName().equals(key)) {
         	        return cookies[i].getValue();
                 }
             }
         }
         return null;
    	}
     
     public static boolean isSupported(String param) {
         if (param.startsWith("pt") || param.startsWith("en")) return true;
         return false;
     }
 %>
 <%
 String param = request.getParameter("loc");
 if (param != null && isSupported(param)) {
     addCookie(response, COOKIE_NAME, param);
     if (param.startsWith("en")) {
         session.setAttribute(org.mentawai.action.BaseLoginAction.LOCALE_KEY, EN);
     } else {
         session.setAttribute(org.mentawai.action.BaseLoginAction.LOCALE_KEY, PT);
     }
 } else {
 	if (session.getAttribute(org.mentawai.action.BaseLoginAction.LOCALE_KEY) == null) {  
 	    String cookie = getCookie(request, COOKIE_NAME);
 	    if (cookie != null) {
 	        if (cookie.startsWith("en")) {
 	             session.setAttribute(org.mentawai.action.BaseLoginAction.LOCALE_KEY, EN);
 	        } else {
 	            session.setAttribute(org.mentawai.action.BaseLoginAction.LOCALE_KEY, PT);
 	        }
 	    }
 	}
 }
 %>
 


Depois basta vc criar links para o sua primeira página passando os seguintes parametros:

Code:
 
 /index.jsp?loc=en
 
 /index.jsp?loc=pt
 
 

Sergio Oliveira

 
Forum Index -> F.A.Q.
Go to:   
Powered by JForum 2.1.6 © JForum Team