Skip to content

Commit

Permalink
Fixes #7: NullReferenceException when using ThreadLanguageDetector an…
Browse files Browse the repository at this point in the history
…d Accept-Language header is not present
  • Loading branch information
Shaddix committed Jul 25, 2019
1 parent 6872a35 commit 4b431c4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions samples/Example.WebApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
Expand All @@ -14,6 +16,9 @@ public class Program
{
public static void Main(string[] args)
{
// This is usually the case for production servers
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
CreateWebHostBuilder(args).Build().Run();
}

Expand Down
2 changes: 1 addition & 1 deletion src/I18Next.Net.Extensions/Builder/I18NextOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class I18NextOptions
public string DefaultLanguage { get; set; } = "en-US";

public string DefaultNamespace { get; set; } = "translation";
public IList<string> FallbackLanguages { get; set; }
public IList<string> FallbackLanguages { get; set; } = new List<string>();

public bool DetectLanguageOnEachTranslation { get; set; }
}
Expand Down
7 changes: 6 additions & 1 deletion src/I18Next.Net/Plugins/ThreadLanguageDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ public ThreadLanguageDetector(string fallbackLanguage)

public string GetLanguage()
{
return Thread.CurrentThread.CurrentCulture.IetfLanguageTag;
var languageTag = Thread.CurrentThread.CurrentCulture.IetfLanguageTag;
if (string.IsNullOrEmpty(languageTag))
{
return FallbackLanguage;
}
return languageTag;
}
}
}

0 comments on commit 4b431c4

Please sign in to comment.