The new Ripple frontend.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
670B

  1. package locale
  2. import (
  3. "sort"
  4. "strconv"
  5. "strings"
  6. )
  7. // ParseHeader parses an Accept-Language header, and sorts the values.
  8. func ParseHeader(header string) []string {
  9. if header == "" {
  10. return nil
  11. }
  12. parts := strings.Split(header, ",")
  13. sort.Slice(parts, func(i, j int) bool {
  14. return getQuality(parts[i]) > getQuality(parts[j])
  15. })
  16. for idx, val := range parts {
  17. parts[idx] = strings.Replace(strings.SplitN(val, ";q=", 2)[0], "-", "_", 1)
  18. }
  19. return parts
  20. }
  21. func getQuality(s string) float32 {
  22. idx := strings.Index(s, ";q=")
  23. if idx == -1 {
  24. return 1
  25. }
  26. f, err := strconv.ParseFloat(s[idx+3:], 32)
  27. if err != nil {
  28. return 1
  29. }
  30. return float32(f)
  31. }