001/* 002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Sun Seng David TAN <[email protected]> 018 */ 019package org.nuxeo.ecm.platform.web.common.locale; 020 021import java.util.List; 022import java.util.Locale; 023import java.util.TimeZone; 024 025import org.apache.commons.lang3.LocaleUtils; 026import org.apache.commons.lang3.StringUtils; 027import org.nuxeo.ecm.core.api.CoreSession; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.platform.ui.web.auth.LoginScreenHelper; 030import org.nuxeo.ecm.platform.ui.web.auth.service.LoginScreenConfig; 031 032/** 033 * Provides the default locale and timezone from the server. 034 * 035 * @since 5.6 036 */ 037public class DefaultLocaleProvider implements LocaleProvider { 038 039 @Override 040 public Locale getLocale(CoreSession repo) { 041 return getLocaleWithDefault(repo); 042 } 043 044 @Override 045 public Locale getLocale(DocumentModel userProfileDoc) { 046 return getLocaleWithDefault(userProfileDoc); 047 } 048 049 @Override 050 public TimeZone getTimeZone(CoreSession repo) { 051 return getDefaultTimezone(); 052 } 053 054 @Override 055 public Locale getLocaleWithDefault(CoreSession session) { 056 return getDefaultLocale(); 057 } 058 059 @Override 060 public Locale getLocaleWithDefault(DocumentModel userProfileDoc) { 061 return getDefaultLocale(); 062 } 063 064 @Override 065 public Locale getLocaleWithDefault(String requestedLocale) { 066 Locale res = null; 067 LoginScreenConfig screenConfig = LoginScreenHelper.getConfig(); 068 if (screenConfig != null) { 069 List<String> supported = screenConfig.getSupportedLocales(); 070 if (!StringUtils.isBlank(requestedLocale) && supported.contains(requestedLocale)) { 071 res = LocaleUtils.toLocale(requestedLocale); 072 } else { 073 res = LocaleUtils.toLocale(screenConfig.getDefaultLocale()); 074 } 075 } 076 if (res == null) { 077 return Locale.getDefault(); 078 } 079 return res; 080 } 081 082 protected Locale getDefaultLocale() { 083 return getLocaleWithDefault((String) null); 084 } 085 086 protected TimeZone getDefaultTimezone() { 087 return TimeZone.getDefault(); 088 } 089 090}