W3C home > Mailing lists > Public > www-validator-cvs@w3.org > March 2010

2006/unicorn/src/org/w3c/unicorn/action LanguageAction.java,1.22,1.23

From: Thomas Gambet via cvs-syncmail <cvsmail@w3.org>
Date: Sat, 20 Mar 2010 17:06:07 +0000
To: www-validator-cvs@w3.org
Message-Id: <E1Nt27j-00082q-20@lionel-hutz.w3.org>
Update of /sources/public/2006/unicorn/src/org/w3c/unicorn/action
In directory hutz:/tmp/cvs-serv30900/src/org/w3c/unicorn/action

Modified Files:
	LanguageAction.java 
Log Message:
updated with the new Language.class functions
+ cleanup

Index: LanguageAction.java
===================================================================
RCS file: /sources/public/2006/unicorn/src/org/w3c/unicorn/action/LanguageAction.java,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -d -r1.22 -r1.23
--- LanguageAction.java	5 Mar 2010 15:13:18 -0000	1.22
+++ LanguageAction.java	20 Mar 2010 17:06:04 -0000	1.23
@@ -11,10 +11,7 @@
 import java.util.ArrayList;
 import java.util.Hashtable;
 import java.util.List;
-import java.util.Locale;
 import java.util.Map;
-import java.util.Properties;
-import java.util.TreeMap;
 
 import javax.mail.internet.AddressException;
 import javax.mail.internet.InternetAddress;
@@ -22,7 +19,6 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.apache.commons.lang.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.velocity.VelocityContext;
@@ -39,28 +35,18 @@
 import org.w3c.unicorn.util.Mail;
 import org.w3c.unicorn.util.UCNProperties;
 
-/**
- * Servlet implementation class LanguageServlet
- */
+import com.ibm.icu.util.ULocale;
+
 public class LanguageAction extends Action {
 	
 	private static final long serialVersionUID = 1L;
 	
 	private static Log logger = LogFactory.getLog(LanguageAction.class);
 	
-	private static TreeMap<String, Properties> languageProperties;
-	
-	private static TreeMap<String, Properties> metadataProperties;
-	
-	private static TreeMap<String, String> defaultProperties = new TreeMap<String, String>();
-	
-	private static TreeMap<String, String> defaultMetadatas = new TreeMap<String, String>();
+	private static Hashtable<ULocale, UCNProperties> languageProperties = new Hashtable<ULocale, UCNProperties>();
 	
-	private static TreeMap<String, String> availableLocales;
+	private static Hashtable<ULocale, UCNProperties> metadataProperties = new Hashtable<ULocale, UCNProperties>();
 
-	/**
-	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
-	 */
 	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 		
 		if (!Framework.isUcnInitialized) {
@@ -74,28 +60,30 @@
 		req.setCharacterEncoding("UTF-8");
 		resp.setContentType("text/html; charset=UTF-8");
 		
-		String defaultLang = Property.get("DEFAULT_LANGUAGE");
 		MessageList messages;
-		
 		if (req.getAttribute("messages") != null && req.getAttribute("messages") instanceof MessageList)
 			messages = (MessageList) req.getAttribute("messages");
 		else
 			messages = new MessageList();
 		
-		VelocityContext velocityContext = new VelocityContext(Language.getContext(defaultLang));
+		VelocityContext velocityContext = new VelocityContext(Language.getContext(Language.getDefaultLocale()));
 		velocityContext.put("queryString", "./");
 		velocityContext.put("messages", messages);
 		velocityContext.put("baseUri", "./");
-		velocityContext.put("availableLocales", availableLocales);
-		velocityContext.put("native_lang", req.getLocale().getLanguage());
+		velocityContext.put("availableLocales", Language.getAvailableLocales());
+		velocityContext.put("native_lang", Language.getAvailableLocale(req.getHeader("Accept-Language")).getName());
 		velocityContext.put("translator_name", req.getAttribute("translator_name"));
 		velocityContext.put("translator_mail", req.getAttribute("translator_mail"));
 		velocityContext.put("translator_comments", req.getAttribute("translator_comments"));
+		velocityContext.put("uiLocales", Language.getUiLocales());
 		
-		Hashtable<String, String> languages = new Hashtable<String, String>();
-		languages.put(defaultLang, defaultProperties.get("language"));
+		ArrayList<ULocale> languages = new ArrayList<ULocale>();
+		languages.add(Language.getDefaultLocale());
 		velocityContext.put("languages", languages);
 		
+		UCNProperties defaultProperties = languageProperties.get(Language.getDefaultLocale());
+		UCNProperties defaultMetadatas = metadataProperties.get(Language.getDefaultLocale());
+		
 		velocityContext.put("languageProps", languageProperties);
 		velocityContext.put("defaultProps", defaultProperties);
 		
@@ -104,58 +92,52 @@
 		
 		PrintWriter writer = resp.getWriter();
 		String langParameter = req.getParameter(Property.get("UNICORN_PARAMETER_PREFIX") + "lang");
+		
 		if (langParameter == null || req.getAttribute("submitted") != null)
 			Templates.write("language.vm", velocityContext, writer);
 		else {
-			if (Framework.getLanguageProperties().containsKey(langParameter)) {
-				if (langParameter.equals(Property.get("DEFAULT_LANGUAGE"))) {
-					messages.add(new Message(Message.INFO, "You cannot edit the default language"));
+			ULocale locale = Language.getAvailableLocale(langParameter);
+			ULocale requestedLocale = Language.getLocale(langParameter);			
+			if (requestedLocale != locale) {
+				messages.add(new Message(Message.INFO, "The requested language \"" + requestedLocale.getDisplayName(requestedLocale) + "\" has been changed to the fallback language \"" +
+														locale.getDisplayName(locale) + "\" instead. If you think this is not right please make a request on <a href=\"mailto:public-qa-dev@w3.org\">our public mailing-list</a>."));
+			}
+			if (Language.getUiLocales().contains(locale)) {
+				if (locale.equals(Language.getDefaultLocale())) {
+					messages.add(new Message(Message.ERROR, "The language you are trying to edit is either the default language or is unknown. " +
+															"If it is the latter and you think we are wrong please make a request on <a href=\"mailto:public-qa-dev@w3.org\">our public mailing-list</a>."));
 					Templates.write("language.vm", velocityContext, writer);
 					writer.close(); return;
 				} else {
-					int missings = defaultProperties.size() - languageProperties.get(langParameter).size() + defaultMetadatas.size() - metadataProperties.get(langParameter).size();
-					
+					int missings = defaultProperties.size() + defaultMetadatas.size() - languageProperties.get(locale).size() - metadataProperties.get(locale).size();
 					if (missings > 0)
-						messages.add(new Message(Message.INFO, "This translation lacks " + missings + " properties. Help us to improve it."));
+						messages.add(new Message(Message.INFO, "This translation lacks " + missings + " properties on " + (defaultProperties.size() + defaultMetadatas.size()) + ". Help us to improve it."));
 					else 
 						messages.add(new Message(Message.INFO, "This translation is complete but you can help us to improve it if needed."));
 				}
 				if (req.getAttribute("submitted_props") != null) {
-					Properties submittedProps = (Properties) req.getAttribute("submitted_props");
+					UCNProperties submittedProps = (UCNProperties) req.getAttribute("submitted_props");
 					velocityContext.put("prop", submittedProps);
 				} else
-					velocityContext.put("prop", languageProperties.get(langParameter));
+					velocityContext.put("prop", languageProperties.get(locale));
 				if (req.getAttribute("submitted_metas") != null) {
-					Properties submittedMetas = (Properties) req.getAttribute("submitted_metas");
+					UCNProperties submittedMetas = (UCNProperties) req.getAttribute("submitted_metas");
 					velocityContext.put("metadatas", submittedMetas);
 				} else
-					velocityContext.put("metadatas", metadataProperties.get(langParameter));
-			} else if (Language.isISOLanguageCode(langParameter)) {
-				Locale locale = Language.getLocale(langParameter);
-				if (locale == null) {
-					messages.add(new Message(Message.WARNING, "The language code you requested is valid but the coresponding locale seems not to be installed on our system. Please make a request on public-qa-dev@w3.org."));
-					Templates.write("language.vm", velocityContext, writer);
-					writer.close(); return;
-				} else {
-					messages.add(new Message(Message.INFO, "Thank you for translating Unicorn in " 
-							+ Language.getLocale(langParameter).getDisplayLanguage(Locale.ENGLISH) 
-							+ ". You can submit a full or a partial translation."));
-					velocityContext.put("prop", createProperties(langParameter));
-					velocityContext.put("metadatas", createProperties(langParameter));
-				}
+					velocityContext.put("metadatas", metadataProperties.get(locale));
 			} else {
-				messages.add(new Message(Message.ERROR, "$message_invalid_requested_language", null, langParameter));
-				Templates.write("language.vm", velocityContext, writer);
-				writer.close(); return;
+				messages.add(new Message(Message.INFO, "Thank you for translating Unicorn in " 
+						+ locale.getDisplayName(Language.getDefaultLocale()) 
+						+ ". You can submit a full or a partial translation."));
+				velocityContext.put("prop", new UCNProperties());
+				velocityContext.put("metadatas", new UCNProperties());
 			}
+			velocityContext.put("transLocale", locale);
 			Templates.write("language.form.vm", velocityContext, writer);
 			writer.close();
 		}
 	}
 
-	/**
-	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
-	 */
 	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 		
 		if (!Framework.isUcnInitialized) {
@@ -180,12 +162,12 @@
 			UCNProperties metaProps;
 			if (languageProperties.get(languageParameter) == null || metadataProperties.get(languageParameter) == null) {
 				if (languageProperties.get(languageParameter) == null) {
-					langProps = createProperties(languageParameter);
+					langProps = new UCNProperties();
 					contextObjects.put("new_interface_translation", true);
 				} else
 					langProps = (UCNProperties) languageProperties.get(languageParameter).clone();
 				if (metadataProperties.get(languageParameter) == null) {
-					metaProps = createProperties(languageParameter);
+					metaProps = new UCNProperties();
 					contextObjects.put("new_tasklist_translation", true);
 				} else
 					metaProps = (UCNProperties) metadataProperties.get(languageParameter).clone();
@@ -286,17 +268,12 @@
 			contextObjects.put("translator_name", req.getParameter("translator_name"));
 			contextObjects.put("translator_mail", req.getParameter("translator_mail"));
 			contextObjects.put("translator_comments", req.getParameter("translator_comments"));
-			contextObjects.put("language", Language.getLocale(languageParameter).getDisplayLanguage(Locale.ENGLISH));
+			contextObjects.put("language", Language.getLocale(languageParameter).getDisplayLanguage(Language.getDefaultLocale()));
 			contextObjects.put("interfaceChangeLog", interfaceChangeLog);
 			contextObjects.put("tasklistChangeLog", tasklistChangeLog);
 			contextObjects.put("interfaceChanged", interfaceChanged);
 			contextObjects.put("tasklistChanged", tasklistChanged);
 			
-			langProps.remove("lang");
-			langProps.remove("language");
-			metaProps.remove("lang");
-			metaProps.remove("language");
-			
 			if (interfaceChanged) {
 				ByteArrayOutputStream baos = new ByteArrayOutputStream();
 				OutputStreamWriter osw = new OutputStreamWriter(baos, "UTF-8");
@@ -350,77 +327,28 @@
 		}
 	}
 
-	private UCNProperties createProperties(String langParameter) {
-		UCNProperties props = new UCNProperties();
-		Locale locale = Language.getLocale(langParameter);
-		if (locale == null)
-			return null;
-		props.put("lang", locale.getLanguage());
-		props.put("language", StringUtils.capitalize(locale.getDisplayLanguage(locale)));
-		return props;
-	}
-
-	public static void addLanguageProperties(Properties props) {
-		if (!props.getProperty("lang").equals(Property.get("DEFAULT_LANGUAGE")))
-			languageProperties.put(props.getProperty("lang"), (Properties) props.clone());
+	public static void addLanguageProperties(ULocale locale, UCNProperties props) {
+		languageProperties.put(locale, (UCNProperties) props.clone());
 	}
 
-	public static TreeMap<String, Properties> getLanguageProperties() {
+	public static Hashtable<ULocale, UCNProperties> getLanguageProperties() {
 		return languageProperties;
 	}
 
-	public static void setLanguageProperties(TreeMap<String, Properties> languageProperties) {
+	public static void setLanguageProperties(Hashtable<ULocale, UCNProperties> languageProperties) {
 		LanguageAction.languageProperties = languageProperties;
 	}
-
-	public static TreeMap<String, String> getDefaultProperties() {
-		return defaultProperties;
-	}
-
-	public static void setDefaultProperties(TreeMap<String, String> defaultProperties) {
-		LanguageAction.defaultProperties = defaultProperties;
-	}
 	
-	public static void setDefaultProperties(Properties defaultProperties) {
-		for (Object obj : defaultProperties.keySet()) {
-			String key = (String) obj;
-			LanguageAction.defaultProperties.put(key, defaultProperties.getProperty(key));
-		}
-	}
-
-	public static TreeMap<String, String> getAvailableLocales() {
-		return availableLocales;
-	}
-
-	public static void setAvailableLocales(TreeMap<String, String> availableLocales) {
-		LanguageAction.availableLocales = availableLocales;
-	}
-
-	public static TreeMap<String, Properties> getMetadataProperties() {
+	public static Hashtable<ULocale, UCNProperties> getMetadataProperties() {
 		return metadataProperties;
 	}
 
-	public static void setMetadataProperties(
-			TreeMap<String, Properties> metadataProperties) {
+	public static void setMetadataProperties(Hashtable<ULocale, UCNProperties> metadataProperties) {
 		LanguageAction.metadataProperties = metadataProperties;
 	}
-
-	public static TreeMap<String, String> getDefaultMetadatas() {
-		return defaultMetadatas;
-	}
-
-	public static void setDefaultMetadatas(TreeMap<String, String> defaultMetadatas) {
-		LanguageAction.defaultMetadatas = defaultMetadatas;
-	}
-
-	public static void setDefaultMetadatas(Properties defaultProperties) {
-		for (Object obj : defaultProperties.keySet()) {
-			String key = (String) obj;
-			LanguageAction.defaultMetadatas.put(key, defaultProperties.getProperty(key));
-		}
-	}
 	
-	public static void addMetadatasProperties(String lang, Properties props) {
-		metadataProperties.put(lang, (Properties) props.clone());
+	public static void addMetadatasProperties(ULocale locale, UCNProperties props) {
+		metadataProperties.put(locale, (UCNProperties) props.clone());
 	}
+	
 }
Received on Saturday, 20 March 2010 17:06:08 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Thursday, 26 April 2012 12:55:19 GMT