src/main/kotlin/de/uapcore/lightpit/AbstractServlet.kt

changeset 208
785820da6485
parent 184
e8eecee6aadf
child 247
e71ae69c68c0
equal deleted inserted replaced
207:479dd7993ef9 208:785820da6485
102 if (session.getAttribute(Constants.SESSION_ATTR_LANGUAGE) == null) { 102 if (session.getAttribute(Constants.SESSION_ATTR_LANGUAGE) == null) {
103 val availableLanguages = availableLanguages() 103 val availableLanguages = availableLanguages()
104 val reqLocale = req.locale 104 val reqLocale = req.locale
105 val sessionLocale = if (availableLanguages.contains(reqLocale)) reqLocale else availableLanguages.first() 105 val sessionLocale = if (availableLanguages.contains(reqLocale)) reqLocale else availableLanguages.first()
106 session.setAttribute(Constants.SESSION_ATTR_LANGUAGE, sessionLocale) 106 session.setAttribute(Constants.SESSION_ATTR_LANGUAGE, sessionLocale)
107 resp.locale = sessionLocale
107 logger().debug( 108 logger().debug(
108 "Setting language for new session {}: {}", session.id, sessionLocale.displayLanguage 109 "Setting language for new session {}: {}", session.id, sessionLocale.displayLanguage
109 ) 110 )
110 } else { 111 } else {
111 val sessionLocale = session.getAttribute(Constants.SESSION_ATTR_LANGUAGE) as Locale 112 val sessionLocale = session.getAttribute(Constants.SESSION_ATTR_LANGUAGE) as Locale
174 } 175 }
175 176
176 protected fun availableLanguages(): List<Locale> { 177 protected fun availableLanguages(): List<Locale> {
177 val langTags = servletContext.getInitParameter(Constants.CTX_ATTR_LANGUAGES)?.split(",")?.map(String::trim) ?: emptyList() 178 val langTags = servletContext.getInitParameter(Constants.CTX_ATTR_LANGUAGES)?.split(",")?.map(String::trim) ?: emptyList()
178 val locales = langTags.map(Locale::forLanguageTag).filter { it.language.isNotEmpty() } 179 val locales = langTags.map(Locale::forLanguageTag).filter { it.language.isNotEmpty() }
179 return if (locales.isEmpty()) listOf(Locale.ENGLISH) else locales 180 return locales.ifEmpty { listOf(Locale.ENGLISH) }
180 } 181 }
181 182
182 } 183 }

mercurial