Skip to content

Commit

Permalink
provide user session's locale
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Sep 11, 2017
1 parent 437236d commit 9202cda
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/main/java/de/metas/ui/web/session/UserSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ public Locale getLocale()
{
return data.getLocale();
}

public UserSessionLocale getUserSessionLocale()
{
return UserSessionLocale.get(getAD_Language());
}


public boolean isUseHttpAcceptLanguage()
{
Expand Down Expand Up @@ -409,7 +415,7 @@ public int getHttpCacheMaxAge()
{
return data.getHttpCacheMaxAge();
}

/**
* Event fired when the user language was changed.
* Usually it is user triggered.
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/de/metas/ui/web/session/UserSessionLocale.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package de.metas.ui.web.session;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

import org.compiere.util.CCache;
import org.compiere.util.DisplayType;

import de.metas.i18n.Language;
import lombok.NonNull;
import lombok.Value;

/*
* #%L
* metasfresh-webui-api
* %%
* Copyright (C) 2017 metas GmbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/

@Value
public class UserSessionLocale
{
public static final UserSessionLocale get(@NonNull final String adLanguage)
{
return cache.getOrLoad(adLanguage, () -> new UserSessionLocale(adLanguage));
}

private static final CCache<String, UserSessionLocale> cache = CCache.newCache(UserSessionLocale.class.getName(), 10, 0);

private final String adLanguage;
private final char numberDecimalSeparator;
private final char numberGroupingSeparator;

private UserSessionLocale(final String adLanguage)
{
final Language language = Language.getLanguage(adLanguage);
if (language == null)
{
throw new IllegalArgumentException("No language found for " + adLanguage);
}
this.adLanguage = language.getAD_Language();

final DecimalFormat decimalFormat = DisplayType.getNumberFormat(DisplayType.Amount, language);
final DecimalFormatSymbols decimalFormatSymbols = decimalFormat.getDecimalFormatSymbols();
numberDecimalSeparator = decimalFormatSymbols.getDecimalSeparator();
numberGroupingSeparator = decimalFormatSymbols.getGroupingSeparator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public static final JSONUserSession of(final UserSession userSession)

@JsonProperty("language")
private final JSONLookupValue language;
@JsonProperty("locale")
private final JSONUserSessionLocale locale;

@JsonProperty("timeZone")
private final String timeZone;
Expand Down Expand Up @@ -116,6 +118,7 @@ private JSONUserSession(final UserSession userSession)

final Language language = userSession.getLanguage();
this.language = JSONLookupValue.of(language.getAD_Language(), language.getName());
this.locale = JSONUserSessionLocale.of(userSession.getUserSessionLocale());

timeZone = JSONDate.getCurrentTimeZoneAsJson();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package de.metas.ui.web.session.json;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;

import de.metas.ui.web.session.UserSessionLocale;
import lombok.Value;

/*
* #%L
* metasfresh-webui-api
* %%
* Copyright (C) 2017 metas GmbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/

@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
@Value
public class JSONUserSessionLocale
{
public static JSONUserSessionLocale of(final UserSessionLocale locale)
{
return new JSONUserSessionLocale(locale);
}

private final char numberDecimalSeparator;
private final char numberGroupingSeparator;

private JSONUserSessionLocale(final UserSessionLocale locale)
{
numberDecimalSeparator = locale.getNumberDecimalSeparator();
numberGroupingSeparator = locale.getNumberGroupingSeparator();
}
}

0 comments on commit 9202cda

Please sign in to comment.