User options to change UI language as well as content filter language.

Some small rationalizing of common code for above.


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@4754 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2007-01-08 17:27:53 +00:00
parent f9e9d0b808
commit b3a588171a
7 changed files with 167 additions and 97 deletions

View File

@@ -21,7 +21,6 @@ import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import javax.faces.context.FacesContext;
import javax.portlet.PortletContext;
@@ -33,6 +32,7 @@ import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.alfresco.config.ConfigService;
import org.alfresco.i18n.I18NUtil;
import org.alfresco.repo.importer.ImporterBootstrap;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.web.app.servlet.AuthenticationHelper;
@@ -495,7 +495,7 @@ public class Application
@SuppressWarnings("unchecked")
public static void setLanguage(FacesContext context, String code)
{
Locale locale = parseLocale(code);
Locale locale = I18NUtil.parseLocale(code);
// set locale for JSF framework usage
context.getViewRoot().setLocale(locale);
@@ -515,38 +515,12 @@ public class Application
*/
public static void setLanguage(HttpSession session, String code)
{
Locale locale = parseLocale(code);
Locale locale = I18NUtil.parseLocale(code);
session.setAttribute(LOCALE, locale);
session.removeAttribute(MESSAGE_BUNDLE);
}
/**
* @param code Locale code (java format with underscores) to parse
* @return Locale object or default if unable to parse
*/
private static Locale parseLocale(String code)
{
Locale locale = Locale.getDefault();
StringTokenizer t = new StringTokenizer(code, "_");
int tokens = t.countTokens();
if (tokens == 1)
{
locale = new Locale(code);
}
else if (tokens == 2)
{
locale = new Locale(t.nextToken(), t.nextToken());
}
else if (tokens == 3)
{
locale = new Locale(t.nextToken(), t.nextToken(), t.nextToken());
}
return locale;
}
/**
* Return the language Locale for the current user context
*

View File

@@ -18,18 +18,14 @@ package org.alfresco.web.bean;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.faces.validator.ValidatorException;
import javax.servlet.http.HttpServletRequest;
import org.alfresco.config.Config;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.security.authentication.AuthenticationException;
import org.alfresco.service.cmr.repository.InvalidNodeRefException;
@@ -42,7 +38,6 @@ import org.alfresco.web.app.servlet.AuthenticationHelper;
import org.alfresco.web.app.servlet.FacesHelper;
import org.alfresco.web.bean.repository.Repository;
import org.alfresco.web.bean.repository.User;
import org.alfresco.web.config.LanguagesConfigElement;
import org.alfresco.web.ui.common.Utils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -166,62 +161,6 @@ public class LoginBean
return this.password;
}
/**
* @return the available languages
*/
public SelectItem[] getLanguages()
{
Config config = Application.getConfigService(FacesContext.getCurrentInstance()).getConfig("Languages");
LanguagesConfigElement langConfig = (LanguagesConfigElement)config.getConfigElement(
LanguagesConfigElement.CONFIG_ELEMENT_ID);
List<String> languages = langConfig.getLanguages();
SelectItem[] items = new SelectItem[languages.size()];
int count = 0;
for (String locale : languages)
{
// get label associated to the locale
String label = langConfig.getLabelForLanguage(locale);
// set default selection
if (count == 0 && this.language == null)
{
// first try to get the language that the current user is using
Locale lastLocale = Application.getLanguage(FacesContext.getCurrentInstance());
if (lastLocale != null)
{
this.language = lastLocale.toString();
}
// else we default to the first item in the list
else
{
this.language = locale;
}
}
items[count++] = new SelectItem(locale, label);
}
return items;
}
/**
* @return Returns the language selection.
*/
public String getLanguage()
{
return this.language;
}
/**
* @param language The language selection to set.
*/
public void setLanguage(String language)
{
this.language = language;
Application.setLanguage(FacesContext.getCurrentInstance(), this.language);
}
// ------------------------------------------------------------------------------
// Validator methods
@@ -419,9 +358,10 @@ public class LoginBean
session.put(AuthenticationHelper.SESSION_INVALIDATED, true);
// set language to last used
if (this.language != null && this.language.length() != 0)
String language = preferences.getLanguage();
if (language != null && language.length() != 0)
{
Application.setLanguage(context, this.language);
Application.setLanguage(context, language);
}
return externalAuth ? "logout" : "relogin";
@@ -451,9 +391,6 @@ public class LoginBean
/** password */
private String password = null;
/** language locale selection */
private String language = null;
/** PersonService bean reference */
protected PersonService personService;

View File

@@ -16,13 +16,20 @@
*/
package org.alfresco.web.bean;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.alfresco.config.Config;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.i18n.I18NUtil;
import org.alfresco.web.app.Application;
import org.alfresco.web.bean.repository.PreferencesService;
import org.alfresco.web.config.LanguagesConfigElement;
/**
* Simple bean backing the user preferences settings.
@@ -37,6 +44,131 @@ public class UserPreferencesBean
private static final String MSG_COMPANYHOME = "company_home";
private static final String MSG_GUESTHOME = "guest_home";
private static final String PREF_CONTENTFILTERLANGUAGE = "content-filter-language";
private static final String MSG_CONTENTALLLANGUAGES = "content_all_languages";
/** language locale selection */
private String language = null;
/** content language locale selection */
private String contentFilterLanguage = null;
private SelectItem[] getLanguageItems(boolean includeAllLanguages)
{
Config config = Application.getConfigService(FacesContext.getCurrentInstance()).getConfig("Languages");
LanguagesConfigElement langConfig = (LanguagesConfigElement)config.getConfigElement(
LanguagesConfigElement.CONFIG_ELEMENT_ID);
List<String> languages = langConfig.getLanguages();
List<SelectItem> items = new ArrayList<SelectItem>(20);
if (includeAllLanguages)
{
ResourceBundle msg = Application.getBundle(FacesContext.getCurrentInstance());
String allLanguagesStr = msg.getString(MSG_CONTENTALLLANGUAGES);
items.add(new SelectItem(MSG_CONTENTALLLANGUAGES, allLanguagesStr));
}
for (String locale : languages)
{
// get label associated to the locale
String label = langConfig.getLabelForLanguage(locale);
items.add(new SelectItem(locale, label));
}
SelectItem[] result = new SelectItem[items.size()];
return items.toArray(result);
}
/**
* @return the available languages
*/
public SelectItem[] getLanguages()
{
// Get the item selection list
SelectItem[] items = getLanguageItems(false);
// Change the current language
if (this.language == null)
{
// first try to get the language that the current user is using
Locale lastLocale = Application.getLanguage(FacesContext.getCurrentInstance());
if (lastLocale != null)
{
this.language = lastLocale.toString();
}
// else we default to the first item in the list
else if (items.length > 0)
{
this.language = (String) items[0].getValue();
}
else
{
throw new AlfrescoRuntimeException("The language list is empty");
}
}
return items;
}
/**
* @return Returns the language selection.
*/
public String getLanguage()
{
return this.language;
}
/**
* @param language The language selection to set.
*/
public void setLanguage(String language)
{
this.language = language;
Application.setLanguage(FacesContext.getCurrentInstance(), this.language);
}
public String getContentFilterLanguage()
{
if (this.contentFilterLanguage == null)
{
Locale locale = (Locale) PreferencesService.getPreferences().getValue(PREF_CONTENTFILTERLANGUAGE);
// Null means All Languages
if (locale == null)
{
this.contentFilterLanguage = MSG_CONTENTALLLANGUAGES;
}
else
{
this.contentFilterLanguage = locale.toString();
}
}
return contentFilterLanguage;
}
/**
* @param languageStr A valid locale string or {@link #MSG_CONTENTALLLANGUAGES}
*/
public void setContentFilterLanguage(String languageStr)
{
this.contentFilterLanguage = languageStr;
Locale language = null;
if (languageStr.equals(MSG_CONTENTALLLANGUAGES))
{
// The generic "All Languages" was selected - persist this as a null
}
else
{
// It should be a proper locale string
language = I18NUtil.parseLocale(languageStr);
}
PreferencesService.getPreferences().setValue(PREF_CONTENTFILTERLANGUAGE, language);
}
public SelectItem[] getContentFilterLanguages()
{
// Get the item selection list
SelectItem[] items = getLanguageItems(true);
return items;
}
public String getStartLocation()
{