public static CultureInfo[] GetUserCultures(string acceptLanguage) { // Accept-Language: fr-FR , en;q=0.8 , en-us;q=0.5 , de;q=0.3 if (string.IsNullOrWhiteSpace(acceptLanguage)) { return new CultureInfo[] { } } ; var cultures = acceptLanguage .Split(',') .Select(s => WeightedLanguage.Parse(s)) .OrderByDescending(w => w.Weight) .Select(w => GetCultureInfo(w.Language)) .Where(ci => ci != null) .ToArray(); return(cultures); }
public static WeightedLanguage Parse(string weightedLanguageString) { // de // en;q=0.8 var parts = weightedLanguageString.Split(';'); var result = new WeightedLanguage { Language = parts[0].Trim(), Weight = 1.0 }; if (parts.Length > 1) { parts[1] = parts[1].Replace("q=", "").Trim(); double d; if (double.TryParse(parts[1], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out d)) { result.Weight = d; } } return(result); }