btcpayserver/BTCPayServer/Services/LanguageService.cs

69 lines
2.0 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.IO;
2018-03-23 09:27:48 +01:00
using System.Linq;
using Microsoft.AspNetCore.Hosting.Internal;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
2018-03-23 09:27:48 +01:00
namespace BTCPayServer.Services
{
public class Language
{
public Language(string code, string displayName)
{
DisplayName = displayName;
Code = code;
}
[JsonProperty("code")]
2018-03-23 09:27:48 +01:00
public string Code { get; set; }
[JsonProperty("currentLanguage")]
2018-03-23 09:27:48 +01:00
public string DisplayName { get; set; }
}
2018-03-23 09:27:48 +01:00
public class LanguageService
{
private readonly Language[] _languages;
public LanguageService(IHostingEnvironment environment)
2018-03-23 09:27:48 +01:00
{
var path = (environment as HostingEnvironment)?.WebRootPath;
if (string.IsNullOrEmpty(path))
2018-03-23 09:27:48 +01:00
{
//test environment
path = Path.Combine(TryGetSolutionDirectoryInfo().FullName,"BTCPayServer", "wwwroot");
}
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>());
}
}
_languages = result.ToArray();
}
public static DirectoryInfo TryGetSolutionDirectoryInfo(string currentPath = null)
{
var directory = new DirectoryInfo(
currentPath ?? Directory.GetCurrentDirectory());
while (directory != null && !directory.GetFiles("*.sln").Any())
{
directory = directory.Parent;
}
return directory;
}
public Language[] GetLanguages()
{
return _languages;
2018-03-23 09:27:48 +01:00
}
}
}