Programm_Wirl/AA-Klassen/TranslationService.cs

118 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
using System.IO;
using System.Text.Json;
using ClosedXML.Excel;
namespace Deckungsbeitrag.AA_Klassen
{
public class TranslationService
{
private static Dictionary<string, Dictionary<string, string>> _data = new Dictionary<string, Dictionary<string, string>>();
public static string CurrentLanguage { get; set; } = "de";
public static string FallbackLanguage { get; set; } = "de";
public static IReadOnlyList<string> AvailableLanguages =>
_data.Values
.SelectMany(d => d.Keys)
.Distinct(StringComparer.OrdinalIgnoreCase)
.OrderBy(x => x)
.ToList();
public static void Load(string filePath = @"N:\TECHNIK\Software\Wirl-Verwaltung\Test\translations.json")
{
var json = File.ReadAllText(filePath);
_data = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(json)
?? new Dictionary<string, Dictionary<string, string>>();
}
public static void SetLanguage(string languageCode)
{
//CultureInfo.CurrentUICulture = new CultureInfo(languageCode);
//CultureInfo.CurrentCulture = new CultureInfo(languageCode);
CurrentLanguage = languageCode.Substring(0,2);
}
/// <summary>
/// Verwenden um die Strings zu erhalten.
/// </summary>
/// <param name="key"></param>
/// <param name="args"></param>
/// <returns>Einen String in der gewünschten Sprache</returns>
public static string T(string key, params object[] args)
{
if (_data.TryGetValue(key, out var translations))
{
if (translations.TryGetValue(CurrentLanguage, out var text))
return Format(text, args);
if (!string.IsNullOrWhiteSpace(FallbackLanguage) &&
translations.TryGetValue(FallbackLanguage, out text))
return Format(text, args);
}
int i = key.IndexOf('.');
return $"{key.Substring(i + 1)}";
}
private static string Format(string text, object[] args)
=> args != null && args.Length > 0 ? string.Format(text, args) : text;
public static void Convert(string excelFile, string jsonFile, string sheetName = "Translation01")
{
var result = new Dictionary<string, Dictionary<string, string>>();
var workbook = new XLWorkbook(excelFile);
var ws = workbook.Worksheet(sheetName);
var usedRange = ws.RangeUsed();
if (usedRange == null)
throw new InvalidOperationException("Excel sheet is empty.");
var headerRow = usedRange.FirstRow();
var headers = headerRow.Cells().Select(c => c.GetString().Trim()).ToList();
int keyCol = headers.FindIndex(h => h.Equals("Key", StringComparison.OrdinalIgnoreCase)) + 1;
if (keyCol <= 0)
throw new InvalidOperationException("No 'Key' column found.");
for (int row = 2; row <= usedRange.RowCount(); row++)
{
var excelRow = usedRange.Row(row);
var key = excelRow.Cell(keyCol).GetString().Trim();
if (string.IsNullOrWhiteSpace(key))
continue;
var translations = new Dictionary<string, string>();
for (int col = 1; col <= headers.Count; col++)
{
if (col == keyCol)
continue;
var lang = headers[col - 1];
if (string.IsNullOrWhiteSpace(lang))
continue;
var value = excelRow.Cell(col).GetString();
if (!string.IsNullOrWhiteSpace(value))
translations[lang] = value;
}
result[key] = translations;
}
var json = JsonSerializer.Serialize(result, new JsonSerializerOptions
{
WriteIndented = true
});
File.WriteAllText(jsonFile, json, new UTF8Encoding(false));
}
}
}