2018-11-09 08:48:38 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2018-03-23 09:27:48 +01:00
|
|
|
|
using System.Linq;
|
2018-11-09 08:48:38 +01:00
|
|
|
|
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;
|
|
|
|
|
}
|
2018-11-09 08:48:38 +01:00
|
|
|
|
|
|
|
|
|
[JsonProperty("code")]
|
2018-03-23 09:27:48 +01:00
|
|
|
|
public string Code { get; set; }
|
2018-11-09 08:48:38 +01:00
|
|
|
|
[JsonProperty("currentLanguage")]
|
2018-03-23 09:27:48 +01:00
|
|
|
|
public string DisplayName { get; set; }
|
|
|
|
|
}
|
2018-11-09 08:48:38 +01:00
|
|
|
|
|
2018-03-23 09:27:48 +01:00
|
|
|
|
public class LanguageService
|
|
|
|
|
{
|
2018-11-09 08:48:38 +01:00
|
|
|
|
private readonly Language[] _languages;
|
|
|
|
|
|
|
|
|
|
public LanguageService(IHostingEnvironment environment)
|
2018-03-23 09:27:48 +01:00
|
|
|
|
{
|
2018-11-09 08:48:38 +01:00
|
|
|
|
var path = (environment as HostingEnvironment)?.WebRootPath;
|
|
|
|
|
if (string.IsNullOrEmpty(path))
|
2018-03-23 09:27:48 +01:00
|
|
|
|
{
|
2018-11-09 08:48:38 +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();
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-14 08:48:25 +01:00
|
|
|
|
public static DirectoryInfo TryGetSolutionDirectoryInfo(string currentPath = null)
|
2018-11-09 08:48:38 +01:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|