-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Thomas Richner <[email protected]>
- Loading branch information
1 parent
81f22d3
commit f73950c
Showing
24 changed files
with
863 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
ehealthid-rp/src/main/java/com/oviva/ehealthid/relyingparty/svc/LocalizedException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.oviva.ehealthid.relyingparty.svc; | ||
|
||
public interface LocalizedException { | ||
|
||
Message localizedMessage(); | ||
|
||
record Message(String messageKey, String... args) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
ehealthid-rp/src/main/java/com/oviva/ehealthid/relyingparty/util/LocaleUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.oviva.ehealthid.relyingparty.util; | ||
|
||
import com.oviva.ehealthid.relyingparty.svc.LocalizedException.Message; | ||
import com.oviva.ehealthid.relyingparty.svc.ValidationException; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.MissingResourceException; | ||
import java.util.ResourceBundle; | ||
import java.util.Set; | ||
|
||
public class LocaleUtils { | ||
|
||
public static final String BUNDLE = "i18n"; | ||
public static final Locale DEFAULT_LOCALE = Locale.GERMANY; | ||
protected static Set<Locale> supportedLocales = loadSupportedLocales(); | ||
|
||
private LocaleUtils() {} | ||
|
||
public static Locale getNegotiatedLocale(String acceptLanguageHeaderValue) { | ||
|
||
var acceptableLanguages = negotiatePreferredLocales(acceptLanguageHeaderValue); | ||
|
||
return acceptableLanguages.stream().findFirst().orElse(DEFAULT_LOCALE); | ||
} | ||
|
||
static List<Locale> negotiatePreferredLocales(String headerValue) { | ||
|
||
if (headerValue == null || headerValue.isBlank()) { | ||
headerValue = DEFAULT_LOCALE.toLanguageTag(); | ||
} | ||
|
||
try { | ||
var languageRanges = Locale.LanguageRange.parse(headerValue); | ||
return Locale.filter(languageRanges, supportedLocales); | ||
} catch (IllegalArgumentException e) { | ||
throw new ValidationException(new Message("error.unparsableHeader")); | ||
} | ||
} | ||
|
||
public static String formatLocalizedErrorMessage(Message localizedErrorMessage, Locale locale) { | ||
var bundle = ResourceBundle.getBundle(BUNDLE, locale); | ||
var localizedMessage = bundle.getString(localizedErrorMessage.messageKey()); | ||
|
||
var key = localizedErrorMessage.messageKey(); | ||
if (!key.isBlank()) { | ||
localizedMessage = localizedMessage.formatted((Object[]) localizedErrorMessage.args()); | ||
} | ||
return localizedMessage; | ||
} | ||
|
||
public static Set<Locale> loadSupportedLocales() { | ||
var locales = new HashSet<Locale>(); | ||
var control = ResourceBundle.Control.getControl(ResourceBundle.Control.FORMAT_PROPERTIES); | ||
for (Locale locale : Locale.getAvailableLocales()) { | ||
try { | ||
var bundle = ResourceBundle.getBundle(BUNDLE, locale, control); | ||
if (bundle.getLocale().equals(locale)) { | ||
locales.add(locale); | ||
} | ||
} catch (MissingResourceException e) { | ||
// left empty on purpose | ||
// Skip adding this locale | ||
} | ||
} | ||
return locales; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.