Skip to content

Instantly share code, notes, and snippets.

@svaponi
Created March 1, 2017 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svaponi/77cc33fecc710964e26f8ff6794ef06c to your computer and use it in GitHub Desktop.
Save svaponi/77cc33fecc710964e26f8ff6794ef06c to your computer and use it in GitHub Desktop.
package it.miriade.commons.web.utils;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class AcceptLanguageParser {
/**
* Ritorna una lista di Array Object contenent in Object[0] l'oggetto Locale e in Object[1] il valore "q".
*
* @param header
* @return
*/
public List<Object[]> parseLocale(String header) {
List<Object[]> list = new ArrayList<Object[]>();
for (String str : header.split(",")) {
String[] arr = str.trim().replace("-", "_").split(";");
// Parse the locale
Locale locale = null;
String[] l = arr[0].split("_");
switch (l.length) {
case 2:
locale = new Locale(l[0], l[1]);
break;
case 3:
locale = new Locale(l[0], l[1], l[2]);
break;
default:
locale = new Locale(l[0]);
break;
}
// Parse the q-value
Double q = 1.0D;
for (String s : arr) {
s = s.trim();
if (s.startsWith("q=")) {
q = Double.parseDouble(s.substring(2).trim());
break;
}
}
// Print the Locale and associated q-value
// System.out.println(q + " - " + arr[0] + "\t " + locale.getDisplayLanguage());
list.add(new Object[] { locale, q });
}
return list;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment