/** * Returns the current locale associated to this request if any has been selected, otherwise it * will return the default browser locale. * * @return the current locale */ public Locale getLocale() { String languageParam = getHttpServletRequest().getParameter(WebApplicationConfig.LANGUAGE); if (StringUtils.hasText(languageParam)) { return LocaleUtils.toLocale(languageParam); } Cookie cookie = getCookie(WebApplicationConfig.CURRENT_LOCALE_COOKIE); return (cookie != null) ? LocaleUtils.toLocale(cookie.getValue()) : getHttpServletRequest().getLocale(); }
private String generateWorkingHours(final String locale) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 8); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); long minHours = calendar.getTimeInMillis(); calendar.set(Calendar.HOUR_OF_DAY, 20); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); long maxHours = calendar.getTimeInMillis(); long workBeginHours = (long) (RANDOM.nextDouble() * (maxHours / 2 - minHours) + minHours); long workEndHours = (long) (RANDOM.nextDouble() * (maxHours - workBeginHours) + workBeginHours); Date workBeginDate = new Date(workBeginHours); Date workEndDate = new Date(workEndHours); StringBuilder workingHours = new StringBuilder(); SimpleDateFormat hourFormat = new SimpleDateFormat("HH:mm", LocaleUtils.toLocale(locale)); workingHours .append(hourFormat.format(workBeginDate)) .append("-") .append(hourFormat.format(workEndDate)); return workingHours.toString(); }
@Override public Attachment map(int i, ResultSet resultSet, StatementContext statementContext) throws SQLException { Attachment attachment = new Attachment(); attachment.setId((UUID) resultSet.getObject("id")); attachment.setTitle(resultSet.getString("title")); attachment.setDescription(resultSet.getString("description")); attachment.setSlug(resultSet.getString("slug")); attachment.setData(resultSet.getBinaryStream("data")); attachment.setExtension(resultSet.getString("extension")); attachment.setParentId((UUID) resultSet.getObject("parent_id")); if (MapperUtils.hasColumn("localization_data", resultSet) && !Strings.isNullOrEmpty(resultSet.getString("localization_data"))) { ObjectMapper mapper = new ObjectMapper(); try { Map<Locale, Map<String, Object>> localizedVersions = Maps.newHashMap(); Map[] data = mapper.readValue(resultSet.getString("localization_data"), Map[].class); for (Map map : data) { localizedVersions.put( LocaleUtils.toLocale((String) map.get("locale")), (Map) map.get("entity")); } attachment.setLocalizedVersions(localizedVersions); } catch (IOException e) { throw new SQLException("Failed to de-serialize localization JSON data", e); } } return attachment; }
private Locale toLocale(String str) { Locale locale; try { locale = LocaleUtils.toLocale(str); } catch (Exception e) { locale = Locale.ROOT; } return locale; }
@Override public Pair<DocumentReference, String> next() { SolrDocument result = getResults().get(index++); String wiki = (String) result.get(FieldUtils.WIKI); String space = (String) result.get(FieldUtils.SPACE); String name = (String) result.get(FieldUtils.NAME); String locale = (String) result.get(FieldUtils.DOCUMENT_LOCALE); String version = (String) result.get(FieldUtils.VERSION); DocumentReference documentReference = new DocumentReference(wiki, space, name); if (!locale.isEmpty()) { documentReference = new DocumentReference(documentReference, LocaleUtils.toLocale(locale)); } return new ImmutablePair<DocumentReference, String>(documentReference, version); }
/** * Determine the effective locale for this request. * * <p>Priority 1: URL parameter "locale" Priority 2: Browser locale * * @param request * @return */ private Locale getEffectiveLocale(final HttpServletRequest request) { // Overwrite locale if requested by URL parameter String requestedLocaleString = request.getParameter(LOCALE_KEY); Locale locale = request.getLocale(); if (StringUtils.isNotBlank(requestedLocaleString)) { try { locale = LocaleUtils.toLocale(requestedLocaleString); } catch (IllegalArgumentException e) { locale = Locale.forLanguageTag(requestedLocaleString); } } return locale; }
@Override public void sessionInit(SessionInitEvent event) throws ServiceException { // event.getSession().setErrorHandler(new UIErrorHandler()); Cookie langCookie = null; if (event.getRequest() != null) { for (Cookie cookie : event.getRequest().getCookies()) if (cookie.getName().equals(VWebCommonConstants.USER_LANGUAGE_APPCOOKIE)) { langCookie = cookie; break; } } if (langCookie == null) langCookie = createLanguageCookie(event.getRequest()); event.getSession().setLocale(LocaleUtils.toLocale(langCookie.getValue())); VaadinService.getCurrentResponse().addCookie(langCookie); }
/** * Extract {@link LocalDocumentReference} from a XAR document XML stream. * * @param documentStream the stream to parse * @return the reference extracted from the stream * @throws XarException when failing to parse the document stream * @since 5.4M1 */ public static LocalDocumentReference getReference(InputStream documentStream) throws XarException { XMLStreamReader xmlReader; try { xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(documentStream); } catch (XMLStreamException e) { throw new XarException("Failed to create a XML read", e); } String space = null; String page = null; Locale locale = null; try { // <xwikidoc> xmlReader.nextTag(); xmlReader.require(XMLStreamReader.START_ELEMENT, null, XARDocumentModel.ELEMENT_DOCUMENT); for (xmlReader.nextTag(); xmlReader.isStartElement(); xmlReader.nextTag()) { String elementName = xmlReader.getLocalName(); if (XARDocumentModel.ELEMENT_NAME.equals(elementName)) { page = xmlReader.getElementText(); if (space != null && locale != null) { break; } } else if (XARDocumentModel.ELEMENT_SPACE.equals(elementName)) { space = xmlReader.getElementText(); if (page != null && locale != null) { break; } } else if (XARDocumentModel.ELEMENT_LOCALE.equals(elementName)) { String value = xmlReader.getElementText(); if (value.length() == 0) { locale = Locale.ROOT; } else { locale = LocaleUtils.toLocale(value); } if (space != null && page != null) { break; } } else { StAXUtils.skipElement(xmlReader); } } } catch (XMLStreamException e) { throw new XarException("Failed to parse document", e); } finally { try { xmlReader.close(); } catch (XMLStreamException e) { throw new XarException("Failed to close XML reader", e); } } if (space == null) { throw new XarException("Missing space element"); } if (page == null) { throw new XarException("Missing page element"); } if (locale == null) { throw new XarException("Missing locale element"); } return new LocalDocumentReference(space, page, locale); }
Locale getLocale(String locale) { return StringUtils.isNotBlank(locale) ? LocaleUtils.toLocale(locale) : null; }
class LocaleUtils$SyncAvoid { private static List AVAILABLE_LOCALE_LIST = Collections.unmodifiableList(new ArrayList(Arrays.asList(Locale.getAvailableLocales()))); private static Set AVAILABLE_LOCALE_SET = Collections.unmodifiableSet(new HashSet(LocaleUtils.availableLocaleList())); }
public void setLocale(String string) { Locale locale = LocaleUtils.toLocale(string); SessionState sessionState = getSessionState(); sessionState.setLocale(locale); }