[Using Sakai] anonymous visitor language

Miguel Carro Pellicer farreri.sakai at gmail.com
Mon Oct 29 06:19:19 PDT 2012


An HTML attachment was scrubbed...
URL: http://collab.sakaiproject.org/pipermail/sakai-user/attachments/20121029/5fa9bc87/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.jpg
Type: image/jpeg
Size: 3022 bytes
Desc: not available
Url : http://collab.sakaiproject.org/pipermail/sakai-user/attachments/20121029/5fa9bc87/attachment-0001.jpg 
-------------- next part --------------
Index: portal/portal-impl/impl/src/java/org/sakaiproject/portal/charon/handlers/SiteHandler.java
===================================================================
--- portal/portal-impl/impl/src/java/org/sakaiproject/portal/charon/handlers/SiteHandler.java	(revision 84074)
+++ portal/portal-impl/impl/src/java/org/sakaiproject/portal/charon/handlers/SiteHandler.java	(working copy)
@@ -27,6 +27,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.Locale;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -38,6 +39,7 @@
 import org.sakaiproject.authz.cover.SecurityService;
 import org.sakaiproject.component.cover.ServerConfigurationService;
 import org.sakaiproject.entity.api.ResourceProperties;
+import org.sakaiproject.entity.api.ResourcePropertiesEdit;
 import org.sakaiproject.entity.cover.EntityManager;
 import org.sakaiproject.exception.IdUnusedException;
 import org.sakaiproject.exception.PermissionException;
@@ -57,6 +59,7 @@
 import org.sakaiproject.user.cover.PreferencesService;
 import org.sakaiproject.user.cover.UserDirectoryService;
 import org.sakaiproject.util.Web;
+import org.sakaiproject.util.ResourceLoader;
 
 /**
  * @author ieb
@@ -79,7 +82,8 @@
 	private int configuredTabsToDisplay = 5;
 
 	private boolean useDHTMLMore = false;
-
+	
+	
 	public SiteHandler()
 	{
 		setUrlFragment(SiteHandler.URL_FRAGMENT);
@@ -131,8 +135,8 @@
 	public void doSite(HttpServletRequest req, HttpServletResponse res, Session session,
 			String siteId, String pageId, String toolContextPath) throws ToolException,
 			IOException
-	{
-
+	{		
+				
 		boolean doFrameTop = "true".equals(req.getParameter("sakai.frame.top"));
 		boolean doFrameSuppress = "true".equals(req.getParameter("sakai.frame.suppress"));
 
@@ -201,7 +205,7 @@
 			}
 			return;
 		}
-
+		
 		// Lookup the page in the site - enforcing access control
 		// business rules
 		SitePage page = portal.getSiteHelper().lookupSitePage(pageId, site);
@@ -225,6 +229,10 @@
 		
 		// should we consider a frameset ?
 		boolean doFrameSet = includeFrameset(rcontext, res, req, session, page);
+				
+				
+		setSiteLanguage(site);				
+				
 		
 		includeSiteNav(rcontext, req, session, siteId);
 
@@ -278,6 +286,8 @@
 			// This request is the destination of the request
 			portalService.setStoredState(null);
 		}
+		
+		
 	}
 
 	/**
@@ -405,6 +415,7 @@
 			{
 				skin = ServerConfigurationService.getString("skin.default");
 			}
+			
 			String skinRepo = ServerConfigurationService.getString("skin.repo");
 			rcontext.put("logoSkin", skin);
 			rcontext.put("logoSkinRepo", skinRepo);
@@ -731,5 +742,48 @@
 		}
 		return framesetRequested;
 	}
+	
+	/**
+	 * *
+	 * 
+	 * @return Locale based on its string representation (language_region)
+	 */
+	private Locale getLocaleFromString(String localeString)
+	{
+		String[] locValues = localeString.trim().split("_");
+		if (locValues.length >= 3)
+			return new Locale(locValues[0], locValues[1], locValues[2]); // language, country, variant
+		else if (locValues.length == 2)
+			return new Locale(locValues[0], locValues[1]); // language, country
+		else if (locValues.length == 1)
+			return new Locale(locValues[0]); // language
+		else
+			return Locale.getDefault();
+	}
+	
+		
+	void setSiteLanguage(Site site)
+	{
+		ResourceLoader rl = new ResourceLoader();
+				
+		ResourcePropertiesEdit props = site.getPropertiesEdit();
+				
+		String locale_string = props.getProperty("locale_string");
+						
+		Locale loc;
+				
+		// if no language was specified when creating the site, set default language to session
+		if(locale_string == null || locale_string == "")
+		{					
+			loc = rl.setContextLocale(null);
+		}
+		
+		// if you have indicated a language when creating the site, set selected language to session
+		else
+		{				
+			Locale locale = getLocaleFromString(locale_string);			
+			loc = rl.setContextLocale(locale);			
+		}
+	}
 
 }
Index: site-manage/site-manage-tool/tool/src/bundle/sitesetupgeneric.properties
===================================================================
--- site-manage/site-manage-tool/tool/src/bundle/sitesetupgeneric.properties	(revision 84077)
+++ site-manage/site-manage-tool/tool/src/bundle/sitesetupgeneric.properties	(working copy)
@@ -436,6 +436,8 @@
 newsc.reqsite = Request Site
 newsc.crsite = Create Site
 
+newsc.language.none = No Language provided
+
 #NewSiteCourse
 nscourse.creating = Course/Section Information
 ### (moot) nscourse.step = Step 1 of
@@ -541,6 +543,9 @@
 sinfo.means = means required information.
 sinfo.stitle = Site Title
 sinfo.url_alias = Site URL Alias
+
+sinfo.language = Site Language
+
 sinfo.descr = Description
 sinfo.display = (displayed on the site's homepage)
 sinfo.short = Short Description
@@ -582,6 +587,8 @@
 sitediconf.siticonema = Site Contact Email
 sitediconf.sittit     = Site Title
 
+sitediconf.language = Site Language
+
 sitediinf.alert     = Alert:
 sitediinf.app       = Appearance (Icon)
 ### (moot) sitediinf.app.none  = *default*
Index: site-manage/site-manage-tool/tool/src/java/org/sakaiproject/site/tool/LocaleComparator.java
===================================================================
--- site-manage/site-manage-tool/tool/src/java/org/sakaiproject/site/tool/LocaleComparator.java	(revision 0)
+++ site-manage/site-manage-tool/tool/src/java/org/sakaiproject/site/tool/LocaleComparator.java	(revision 0)
@@ -0,0 +1,73 @@
+/**********************************************************************************
+ * $URL: https://source.sakaiproject.org/svn/trunk/sakai/admin-tools/su/src/java/org/sakaiproject/tool/su/SuTool.java $
+ * $Id: SuTool.java 6970 2006-03-23 23:25:04Z zach.thomas at txstate.edu $
+ ***********************************************************************************
+ *
+ * Copyright (c) 2005, 2006, 2008 The Sakai Foundation
+ *
+ * Licensed under the Educational Community License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *       http://www.osedu.org/licenses/ECL-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ **********************************************************************************/
+
+package org.sakaiproject.site.tool;
+
+import java.util.Comparator;
+import java.util.Locale;
+
+/**
+ * Comparator for sorting locale by DisplayName
+ */
+public final class LocaleComparator implements Comparator
+{
+	/**
+	 * * Compares Locale objects by comparing the DisplayName * *
+	 * 
+	 * @param obj1
+	 *        1st Locale Object for comparison *
+	 * @param obj2
+	 *        2nd Locale Object for comparison *
+	 * @return negative, zero, or positive integer * (obj1 charge is less than, equal to, or greater than the obj2 charge)
+	 */
+	public int compare(Object obj1, Object obj2)
+	{
+		if (obj1 instanceof Locale && obj2 instanceof Locale)
+		{
+			Locale localeOne = (Locale) obj1;
+			Locale localeTwo = (Locale) obj2;
+
+			String displayNameOne = localeOne.getDisplayName();
+			String displayNameTwo = localeTwo.getDisplayName();
+
+			return displayNameOne.compareTo(displayNameTwo);
+		}
+		else
+		{
+			throw new ClassCastException("Inappropriate object class for LocaleComparator");
+		}
+	}
+
+	/**
+	 * * Override of equals method * *
+	 * 
+	 * @param obj
+	 *        LocaleComparator object *
+	 * @return true if equal, false if not equal
+	 */
+	public boolean equals(Object obj)
+	{
+		if (obj instanceof LocaleComparator)
+			return super.equals(obj);
+		else
+			return false;
+	}
+}
Index: site-manage/site-manage-tool/tool/src/java/org/sakaiproject/site/tool/SiteAction.java
===================================================================
--- site-manage/site-manage-tool/tool/src/java/org/sakaiproject/site/tool/SiteAction.java	(revision 84077)
+++ site-manage/site-manage-tool/tool/src/java/org/sakaiproject/site/tool/SiteAction.java	(working copy)
@@ -156,6 +156,7 @@
 import org.sakaiproject.util.Validator;
 import org.sakaiproject.util.Web;
 
+
 /**
  * <p>
  * SiteAction controls the interface for worksite setup.
@@ -299,6 +300,9 @@
 
 	private final static String PROP_SITE_TERM_EID = "term_eid";
 
+	
+	private final static String PROP_SITE_LANGUAGE = "locale_string";
+	
 	/**
 	 * Name of the state attribute holding the site list column list is sorted
 	 * by
@@ -659,6 +663,12 @@
 	/** the course set definition from CourseManagementService **/
 	private final static String STATE_COURSE_SET = "state_course_set";
 	
+	
+	private List prefLocales = new ArrayList();
+	private String SAKAI_LOCALES = "locales";
+	private String SAKAI_LOCALES_MORE = "locales.more";
+	private LocaleComparator localeComparator = new LocaleComparator();	
+	
 	/**
 	 * what are the tool ids within Home page?
 	 * If this is for a newly added Home tool, get the tool ids from template site or system set default
@@ -1629,6 +1639,19 @@
 			context.put("short_description", siteInfo.short_description);
 			context.put("siteContactName", siteInfo.site_contact_name);
 			context.put("siteContactEmail", siteInfo.site_contact_email);
+			
+			/// site language information
+							
+			String locale_string_selected = (String) state.getAttribute("locale_string");
+			if(locale_string_selected == ""  || locale_string_selected == null)							
+				context.put("locale_string_selected", "");			
+			else
+			{
+				Locale locale_selected = getLocaleFromString(locale_string_selected);
+				context.put("locale_string_selected", locale_selected);
+			}
+			
+			
 			siteType = (String) state.getAttribute(STATE_SITE_TYPE);
 			toolRegistrationSelectedList = (List) state
 					.getAttribute(STATE_TOOL_REGISTRATION_SELECTED_LIST);
@@ -2016,6 +2039,11 @@
 				// revising a existing site's tool
 				context.put("existingSite", Boolean.TRUE);
 				context.put("continue", "14");
+				
+				ResourcePropertiesEdit props = site.getPropertiesEdit();
+						
+				String locale_string = props.getProperty(PROP_SITE_LANGUAGE);
+				context.put("locale_string",locale_string);
 			} else {
 				// new site
 				context.put("existingSite", Boolean.FALSE);
@@ -2144,6 +2172,15 @@
 			context.put("form_site_contact_name", siteInfo.site_contact_name);
 			context.put("form_site_contact_email", siteInfo.site_contact_email);
 
+			// available languages in sakai.properties
+			List locales = getPrefLocales();
+						
+			// insert blank item in the first position to give the option not to select any language 
+			if(locales.get(0) != "")
+				locales.add(0,"");
+				
+			context.put("locales",locales);			
+						
 			return (String) getContext(data).get("template") + TEMPLATE[13];
 		case 14:
 			/*
@@ -2163,7 +2200,29 @@
 			}
 			context.put("oTitle", site.getTitle());
 			context.put("title", siteInfo.title);
-
+			
+			// get updated language
+			String new_locale_string = (String) state.getAttribute("locale_string");			
+			if(new_locale_string == ""  || new_locale_string == null)							
+				context.put("new_locale", "");			
+			else
+			{
+				Locale new_locale = getLocaleFromString(new_locale_string);
+				context.put("new_locale", new_locale);
+			}
+						
+			// get site language saved
+			ResourcePropertiesEdit props = site.getPropertiesEdit();					
+			String oLocale_string = props.getProperty(PROP_SITE_LANGUAGE);			
+			if(oLocale_string == "" || oLocale_string == null)				
+				context.put("oLocale", "");			
+			else
+			{
+				Locale oLocale = getLocaleFromString(oLocale_string);
+				context.put("oLocale", oLocale);
+			}
+						
+			
 			context.put("description", siteInfo.description);
 			context.put("oDescription", site.getDescription());
 			context.put("short_description", siteInfo.short_description);
@@ -5432,6 +5491,7 @@
 							EmailService.send(from, to, message_subject, content,
 									headerTo, replyTo, null);
 							// revert back the local setting to default
+							
 							rb.setContextLocale(Locale.getDefault());
 						}
 						catch (Exception e)
@@ -5444,7 +5504,7 @@
 
 			// To Support
 			from = cUser.getEmail();
-			// set locale to system default
+			// set locale to system default			
 			rb.setContextLocale(Locale.getDefault());
 			to = requestEmail;
 			headerTo = requestEmail;
@@ -5518,7 +5578,7 @@
 			// To the Instructor
 			from = requestEmail;
 			to = cUser.getEmail();
-			// set the locale to individual receipient's setting
+			// set the locale to individual receipient's setting			
 			rb.setContextLocale(rb.getLocale(cUser.getId()));
 			headerTo = to;
 			replyTo = to;
@@ -5531,13 +5591,13 @@
 			content = buf.toString();
 			EmailService.send(from, to, message_subject, content, headerTo,
 					replyTo, null);
-			// revert the locale to system default
+			// revert the locale to system default			
 			rb.setContextLocale(Locale.getDefault());
 			state.setAttribute(REQUEST_SENT, Boolean.valueOf(true));
 
 		} // if
 		
-		// reset locale to user default
+		// reset locale to user default		
 		rb.setContextLocale(null);
 
 	} // sendSiteRequest
@@ -5607,7 +5667,7 @@
 			}
 
 			// To Support
-			//set local to default
+			//set local to default			
 			rb.setContextLocale(Locale.getDefault());
 			from = UserDirectoryService.getCurrentUser().getEmail();
 			to = requestEmail;
@@ -5643,6 +5703,7 @@
 		} // if
 
 		// reset locale to user default
+		
 		rb.setContextLocale(null);
 		
 	} // sendSiteNotification
@@ -6310,7 +6371,14 @@
 		if (contactEmail != null) {
 			siteProperties.addProperty(PROP_SITE_CONTACT_EMAIL, contactEmail);
 		}
-
+	
+		/// site language information
+				
+		String locale_string = (String) state.getAttribute("locale_string");							
+				
+		siteProperties.removeProperty(PROP_SITE_LANGUAGE);		
+		siteProperties.addProperty(PROP_SITE_LANGUAGE, locale_string);
+				
 		if (state.getAttribute(STATE_MESSAGE) == null) {
 			try {
 				SiteService.save(Site);
@@ -6722,9 +6790,10 @@
 		ParameterParser params = data.getParameters();
 		String publishUnpublish = params.getString("publishunpublish");
 		String include = params.getString("include");
-
+				
 		if (sEdit != null) {
 			// editing existing site
+																
 			// publish site or not
 			if (publishUnpublish != null
 					&& publishUnpublish.equalsIgnoreCase("publish")) {
@@ -6772,10 +6841,11 @@
 			}
 		} else {
 			// adding new site
+						
 			if (state.getAttribute(STATE_SITE_INFO) != null) {
 				SiteInfo siteInfo = (SiteInfo) state
 						.getAttribute(STATE_SITE_INFO);
-
+						
 				if (publishUnpublish != null
 						&& publishUnpublish.equalsIgnoreCase("publish")) {
 					siteInfo.published = true;
@@ -6828,6 +6898,7 @@
 				state.setAttribute(STATE_TEMPLATE_INDEX, "10");
 			}
 		}
+			
 
 	} // doUpdate_site_access
 
@@ -8073,6 +8144,7 @@
 				addAlert(state, rb.getString("site_group_title_length_limit_1") + SiteConstants.SITE_GROUP_TITLE_LIMIT + " " + rb.getString("site_group_title_length_limit_2")); 	 
 			}
         }
+				
 		if (params.getString("description") != null) {
 			StringBuilder alertMsg = new StringBuilder();
 			String description = params.getString("description");
@@ -9066,6 +9138,13 @@
 				}
 
 				ResourcePropertiesEdit rp = site.getPropertiesEdit();
+				
+				/// site language information
+							
+				String locale_string = (String) state.getAttribute("locale_string");							
+								
+				rp.addProperty(PROP_SITE_LANGUAGE, locale_string);
+															
 				site.setShortDescription(siteInfo.short_description);
 				site.setPubView(siteInfo.include);
 				site.setJoinable(siteInfo.joinable);
@@ -10157,6 +10236,7 @@
 		public String site_contact_email = NULL_STRING; // site contact email
 		
 		public String term = NULL_STRING; // academic term
+				
 
 		public String getSiteId() {
 			return site_id;
@@ -10224,7 +10304,7 @@
 		
 		public void setTerm(String term) {
 			this.term = term;
-		}
+		}		
 
 	} // SiteInfo
 
@@ -11841,7 +11921,11 @@
 
 		SessionState state = ((JetspeedRunData) data).getPortletSessionState(((JetspeedRunData) data).getJs_peid());
 		ParameterParser params = data.getParameters();
-
+					
+		String locale_string = params.getString("locales"); 
+										
+		state.setAttribute("locale_string",locale_string);
+			
 		String option = params.getString("option");
 		if ("removeSection".equals(option))
 		{
@@ -11859,6 +11943,70 @@
 			doCancel(data);
 		}
 	}
+		
+	/**
+	 * *
+	 * 
+	 * @return Locale based on its string representation (language_region)
+	 */
+	private Locale getLocaleFromString(String localeString)
+	{
+		String[] locValues = localeString.trim().split("_");
+		if (locValues.length >= 3)
+			return new Locale(locValues[0], locValues[1], locValues[2]); // language, country, variant
+		else if (locValues.length == 2)
+			return new Locale(locValues[0], locValues[1]); // language, country
+		else if (locValues.length == 1)
+			return new Locale(locValues[0]); // language
+		else
+			return Locale.getDefault();
+	}
 	
+	/**
+	 * @return Returns the prefLocales
+	 */
+	 
+	public List getPrefLocales()
+	{
+		// Initialize list of supported locales, if necessary
+		if (prefLocales.size() == 0)
+		{
+			Locale[] localeArray = null;
+			String localeString = ServerConfigurationService.getString(SAKAI_LOCALES);
+			String localeStringMore = ServerConfigurationService.getString(SAKAI_LOCALES_MORE);
+			
+			if ( localeString == null )
+				localeString = "";
+			if ( localeStringMore != null && !localeStringMore.equals("") )
+				localeString += ","+localeStringMore;
+
+			if ( !localeString.equals("") )
+			{
+				String[] sakai_locales = localeString.split(",");
+				localeArray = new Locale[sakai_locales.length + 1];
+				for (int i = 0; i < sakai_locales.length; i++)
+					localeArray[i] = getLocaleFromString(sakai_locales[i]);
+				localeArray[localeArray.length - 1] = Locale.getDefault();
+			}
+			else
+				// if no locales specified, get default list
+			{
+				localeArray = new Locale[] { Locale.getDefault() };
+			}
+
+			// Sort locales and add to prefLocales (removing duplicates)
+			Arrays.sort(localeArray, localeComparator);
+			for (int i = 0; i < localeArray.length; i++)
+			{
+				if (i == 0 || !localeArray[i].equals(localeArray[i - 1]))
+				{					
+					prefLocales.add(localeArray[i]);
+				}
+			}
+		}
+
+		return prefLocales;
+	}
+	
 }
 
Index: site-manage/site-manage-tool/tool/src/webapp/vm/sitesetup/chef_site-newSiteConfirm.vm
===================================================================
--- site-manage/site-manage-tool/tool/src/webapp/vm/sitesetup/chef_site-newSiteConfirm.vm	(revision 84077)
+++ site-manage/site-manage-tool/tool/src/webapp/vm/sitesetup/chef_site-newSiteConfirm.vm	(working copy)
@@ -217,7 +217,23 @@
  				</td>
  			</tr>
  			<tr>
+			
+			
 			<tr>
+				<th>
+					$tlang.getString("sitediconf.language")
+				</th>
+				<td>					
+					#if ($locale_string_selected == "")
+						<span class="instruction">$tlang.getString("newsc.language.none")</span>
+					#else
+						$validator.escapeHtml($locale_string_selected.getDisplayName())
+					#end						
+				</td>
+			</tr>
+			
+			
+			<tr>
 					<th colspan="2">
 						<h4>
 							$tlang.getString("newsc.descr")
Index: site-manage/site-manage-tool/tool/src/webapp/vm/sitesetup/chef_site-siteInfo-editInfo.vm
===================================================================
--- site-manage/site-manage-tool/tool/src/webapp/vm/sitesetup/chef_site-siteInfo-editInfo.vm	(revision 84077)
+++ site-manage/site-manage-tool/tool/src/webapp/vm/sitesetup/chef_site-siteInfo-editInfo.vm	(working copy)
@@ -184,6 +184,18 @@
 				$!selectedTerm
 			</p>	
 		#end
+
+		<div class="shorttext">
+			<label for="language">
+ 				$tlang.getString("sinfo.language")
+ 			</label> 
+			<select name="locales" class="indnt2" id="locales" size="5">			
+				#foreach($locale in $locales)			
+					<option value="$locale.toString()" #if ($locale.toString() == $locale_string)selected="true" #elseif($locale.toString() == "")selected="true" #end>$validator.escapeHtml($locale.getDisplayName())</option>	
+				#end			
+			</select>
+		</div>
+				
 		<div class="longtext">
 			<label for="description" class="block">
 				$tlang.getString("sitediinf.des")
Index: site-manage/site-manage-tool/tool/src/webapp/vm/sitesetup/chef_site-siteInfo-editInfoConfirm.vm
===================================================================
--- site-manage/site-manage-tool/tool/src/webapp/vm/sitesetup/chef_site-siteInfo-editInfoConfirm.vm	(revision 84077)
+++ site-manage/site-manage-tool/tool/src/webapp/vm/sitesetup/chef_site-siteInfo-editInfoConfirm.vm	(working copy)
@@ -42,6 +42,28 @@
 					</td>					
 				</tr>
 			#end
+			
+			<tr>
+				<th>
+					$tlang.getString("sitediconf.language")
+				</th>
+				<td>					
+					#if (!$new_locale.equals("") && !$oLocale.equals(""))
+						#if (!$new_locale.toString().equals($oLocale.toString()))
+							<span class="highlight">$validator.escapeHtml($new_locale.getDisplayName())</span>
+						#else
+							$validator.escapeHtml($new_locale.getDisplayName())
+						#end
+					#elseif (!$new_locale.equals(""))
+						<span class="highlight">$validator.escapeHtml($new_locale.getDisplayName())</span>
+					#elseif ($oLocale.equals(""))
+						<span class="instruction">$tlang.getString("newsc.language.none")</span>
+					#else
+						<span class="highlight">$tlang.getString("newsc.language.none")</span>
+					#end
+				</td>
+			</tr>
+			
 			#if($!description)
 				<tr>
 					<th>


More information about the sakai-user mailing list