Skip to content

Commit 12734ed

Browse files
committed
API.Utility.GetUserAcceptLanguage() fixed when no Languages are detected
1 parent 5c8abd5 commit 12734ed

File tree

5 files changed

+33
-25
lines changed

5 files changed

+33
-25
lines changed

rls/API.Library.dll

0 Bytes
Binary file not shown.

rls/API.Library.pdb

0 Bytes
Binary file not shown.

src/API.Library/Entities/Utility.cs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -355,38 +355,46 @@ public static string GZipDecompress(string inputBase64)
355355
/// <returns></returns>
356356
public static string GetUserAcceptLanguage()
357357
{
358-
List<string> acceptLanguages = HttpContext.Current.Request.UserLanguages.ToList<string>();
359-
List<dynamic> outLanguages = new List<dynamic>();
360-
361-
if (acceptLanguages.Count() == 0)
362-
{
363-
acceptLanguages.Add(CultureInfo.CurrentCulture.Name);
364-
}
365-
acceptLanguages.Add(CultureInfo.CurrentCulture.Name);
366-
foreach (string al in acceptLanguages)
358+
try
367359
{
360+
List<string> acceptLanguages = HttpContext.Current.Request.UserLanguages == null ? new List<string>() { CultureInfo.CurrentCulture.Name } : HttpContext.Current.Request.UserLanguages.ToList<string>();
361+
List<dynamic> outLanguages = new List<dynamic>();
368362

369-
string[] bits = al.Split(';');
370-
string[] ietf = bits[0].Split('-');
371-
bool hasScript = ietf.Length == 3;
372-
string q = "1.0";
373-
374-
if (bits.Count() > 1)
363+
if (acceptLanguages.Count() == 0)
375364
{
376-
string[] innerBits = bits[1].Split('=');
377-
q = innerBits.Count() > 1 ? innerBits[1] : "1.0";
365+
return null;
378366
}
379367

380-
outLanguages.Add(new
368+
foreach (string al in acceptLanguages)
381369
{
382-
code = ietf[0],
383-
script = hasScript && ietf.Count() > 1 ? ietf[1] : null,
384-
region = hasScript && ietf.Count() > 2 ? ietf[2] : (ietf.Count() > 1 ? ietf[1] : null),
385-
quality = Convert.ToDouble(q)
386-
});
387-
}
388370

389-
return outLanguages.OrderByDescending(x => x.quality).FirstOrDefault().code;
371+
string[] bits = al.Split(';');
372+
string[] ietf = bits[0].Split('-');
373+
bool hasScript = ietf.Length == 3;
374+
string q = "1.0";
375+
376+
if (bits.Count() > 1)
377+
{
378+
string[] innerBits = bits[1].Split('=');
379+
q = innerBits.Count() > 1 ? innerBits[1] : "1.0";
380+
}
381+
382+
outLanguages.Add(new
383+
{
384+
code = ietf[0],
385+
script = hasScript && ietf.Count() > 1 ? ietf[1] : null,
386+
region = hasScript && ietf.Count() > 2 ? ietf[2] : (ietf.Count() > 1 ? ietf[1] : null),
387+
quality = Convert.ToDouble(q)
388+
});
389+
}
390+
391+
return outLanguages.OrderByDescending(x => x.quality).FirstOrDefault().code;
392+
}
393+
catch (Exception)
394+
{
395+
//Do not trow nor log. Instead, return null if language cannot be detected
396+
return null;
397+
}
390398
}
391399
#endregion
392400
}
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)