Skip to content

Instantly share code, notes, and snippets.

@tamboer
Created December 18, 2017 15:04
Show Gist options
  • Select an option

  • Save tamboer/7bf665bdf14fab9a05f80672ee5a7d3b to your computer and use it in GitHub Desktop.

Select an option

Save tamboer/7bf665bdf14fab9a05f80672ee5a7d3b to your computer and use it in GitHub Desktop.
Supported Locales java
import org.springframework.context.i18n.LocaleContextHolder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public class SupportedLocales {
private static final Map<String,Locale> LOCALE_LIST = new HashMap<>();
public static final Locale ENGLISH = Locale.ENGLISH;
public static final Locale DUTCH = new Locale("nl");
static {
LOCALE_LIST.put("en", ENGLISH);
LOCALE_LIST.put("nl", DUTCH);
}
private SupportedLocales() {
throw new UnsupportedOperationException("Constructor for this class is not supported.");
}
public static Locale getLocale() {
Locale localeFromContext = LocaleContextHolder.getLocale();
return LOCALE_LIST.containsKey(localeFromContext.getLanguage()) ? localeFromContext : ENGLISH;
}
public static Locale getDefault() {
return ENGLISH;
}
public static List<Locale> getSupportedLocales() {
return new ArrayList<>(LOCALE_LIST.values());
}
public static Locale get(final String languageCode){
final Locale locale = LOCALE_LIST.get(languageCode);
if(locale == null){
throw new UnsupportedLocaleException(languageCode);
}
return locale;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment