001/* 002 * (C) Copyright 2012-2018 Nuxeo (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.user.center.profile.localeProvider; 020 021import java.util.Locale; 022import java.util.TimeZone; 023 024import org.apache.commons.lang3.LocaleUtils; 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027import org.nuxeo.ecm.core.api.CoreSession; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.platform.web.common.locale.DefaultLocaleProvider; 030import org.nuxeo.ecm.user.center.profile.UserProfileConstants; 031import org.nuxeo.ecm.user.center.profile.UserProfileService; 032import org.nuxeo.runtime.api.Framework; 033 034/** 035 * Provides user local stored in profile doc model 036 * 037 * @since 5.6 038 */ 039public class UserLocaleProvider extends DefaultLocaleProvider { 040 041 public static final Log log = LogFactory.getLog(UserLocaleProvider.class); 042 043 @Override 044 public Locale getLocale(CoreSession repo) { 045 try { 046 UserProfileService userProfileService = Framework.getService(UserProfileService.class); 047 DocumentModel userProfileDoc = userProfileService.getUserProfileDocument(repo); 048 return getLocale(userProfileDoc); 049 } catch (Exception ex) { 050 log.error("Can't get Locale", ex); 051 return null; 052 } 053 } 054 055 @Override 056 public Locale getLocale(DocumentModel userProfileDoc) { 057 if (userProfileDoc == null) { 058 return null; 059 } 060 061 String locale = (String) userProfileDoc.getPropertyValue(UserProfileConstants.USER_PROFILE_LOCALE); 062 if (locale == null || locale.trim().length() == 0) { 063 // undefined if not set 064 return null; 065 } 066 try { 067 return LocaleUtils.toLocale(locale); 068 } catch (IllegalArgumentException e) { 069 log.error("Locale parse exception: \"" + locale + "\"", e); 070 } 071 return null; 072 } 073 074 @Override 075 public TimeZone getTimeZone(CoreSession repo) { 076 // the timezone is not retrieved from the user profile (cookie and Seam 077 // TimezoneSelector) 078 return null; 079 } 080 081 @Override 082 public Locale getLocaleWithDefault(CoreSession session) { 083 Locale locale = getLocale(session); 084 return locale == null ? getDefaultLocale() : locale; 085 } 086 087 @Override 088 public Locale getLocaleWithDefault(DocumentModel userProfileDoc) { 089 Locale locale = getLocale(userProfileDoc); 090 return locale == null ? getDefaultLocale() : locale; 091 } 092 093}