mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2024-11-19 09:54:30 +01:00
Do not crash on bad language file
This commit is contained in:
parent
5fd620556e
commit
b02542f3be
@ -7,6 +7,7 @@ using BTCPayServer.Client.Models;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
@ -27,19 +28,29 @@ namespace BTCPayServer.Services
|
||||
|
||||
public class LanguageService
|
||||
{
|
||||
private readonly ILogger<LanguageService> _logger;
|
||||
private readonly Language[] _languages;
|
||||
|
||||
public LanguageService(IWebHostEnvironment environment)
|
||||
public LanguageService(IWebHostEnvironment environment, ILogger<LanguageService> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
var path = environment.WebRootPath;
|
||||
path = Path.Combine(path, "locales");
|
||||
var files = Directory.GetFiles(path, "*.json");
|
||||
var result = new List<Language>();
|
||||
foreach (var file in files)
|
||||
{
|
||||
using var stream = new StreamReader(file);
|
||||
var json = stream.ReadToEnd();
|
||||
result.Add(JObject.Parse(json).ToObject<Language>());
|
||||
try
|
||||
{
|
||||
|
||||
using var stream = new StreamReader(file);
|
||||
var json = stream.ReadToEnd();
|
||||
result.Add(JObject.Parse(json).ToObject<Language>());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, $"Could not parse language file {file}");
|
||||
}
|
||||
}
|
||||
|
||||
_languages = result.ToArray();
|
||||
|
Loading…
Reference in New Issue
Block a user