Skip to content

Commit

Permalink
[SHIBUI-814]
Browse files Browse the repository at this point in the history
Added an endpoint that retrieves a list of supported locales.
  • Loading branch information
Bill Smith committed Sep 19, 2018
1 parent 162568d commit 45ff2f1
Showing 1 changed file with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collections;
import java.util.HashSet;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author Bill Smith (wsmith@unicon.net)
Expand All @@ -22,4 +28,33 @@ public class InternationalizationMessagesController {
public ResponseEntity<?> getAll(Locale locale) {
return ResponseEntity.ok(messageSource.getMessagesMap(locale));
}

@GetMapping(value = "/available")
public ResponseEntity<?> getAvailableLocales() {
Set<ResourceBundle> supportedLocaleResourceBundles = getResourceBundles();
Set<Locale> supportedLocales = supportedLocaleResourceBundles
.stream()
.map(ResourceBundle::getLocale)
.collect(Collectors.toSet());
return ResponseEntity.ok(supportedLocales);
}

/**
* Get all available resource bundles in i18n/messages that matches a locale supported by this JRE.
*
* @return a set of resource bundles for supported locales for this system
*/
private Set<ResourceBundle> getResourceBundles() {
Set<ResourceBundle> resourceBundles = new HashSet<>();

for (Locale locale : Locale.getAvailableLocales()) {
try {
resourceBundles.add(ResourceBundle.getBundle("i18n/messages", locale));
} catch (MissingResourceException e) {
// do nothing
}
}

return Collections.unmodifiableSet(resourceBundles);
}
}

0 comments on commit 45ff2f1

Please sign in to comment.