-
Notifications
You must be signed in to change notification settings - Fork 2
/
LocalizationService.cs
82 lines (68 loc) · 2.82 KB
/
LocalizationService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using Newtonsoft.Json;
namespace FmgLib.Localization;
public static class LocalizationService
{
private static Dictionary<string, Dictionary<string, string>> _languagePacks;
public static string Language { get; set; }
public static void ReadLocalizationFile(params string[] filePathes)
{
_languagePacks = new Dictionary<string, Dictionary<string, string>>();
try
{
List<string> pathesList = filePathes.ToList();
if (pathesList.Count == 0)
pathesList.Add("Localization.json");
foreach (var filePath in pathesList)
{
var json = File.ReadAllText(filePath);
var pack = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, string>>>(json);
foreach (var lang in pack)
{
if (!_languagePacks.ContainsKey(lang.Key))
{
_languagePacks[lang.Key] = new Dictionary<string, string>();
}
if (lang.Value != null)
{
foreach (var kvp in lang.Value)
{
_languagePacks[lang.Key][kvp.Key] = kvp.Value;
}
}
}
}
}
catch (Exception ex)
{
throw new FileLoadException($"There is an error in your language file.\n{ex.Message} \n\n\n\n The proper json format is:\n \"{{\\n \\\"wordKey\\\": {{\\n \\\"langKey1\\\": \\\"1st language translation.\\\",\\n \\\"langKey2\\\": \\\"2nd language translation.\\\"\\n }},\\n \\\"wordKey2\\\": {{\\n \\\"langKey1\\\": \\\"1st language translation.\\\",\\n \\\"langKey2\\\": \\\"2nd language translation.\\\"\\n }}\\n}}\"");
}
}
public static string Translate(string key)
{
if (string.IsNullOrEmpty(Language))
Language = Thread.CurrentThread.CurrentUICulture.Name;
var lang = _languagePacks.FirstOrDefault(x => x.Key == key).Value?.FirstOrDefault(y => y.Key == Language);
var resultKey = lang?.Key;
var resultValue = lang?.Value;
if (resultKey == Language)
{
return resultValue ?? key;
}
if (_languagePacks.ContainsKey("en") && _languagePacks["en"].ContainsKey(key))
{
return _languagePacks["en"][key];
}
return key;
}
public static string Translate(string key, string langKey)
{
var lang = _languagePacks.FirstOrDefault(x => x.Key == key).Value?.FirstOrDefault(y => y.Key == langKey);
var resultKey = lang?.Key;
var resultValue = lang?.Value;
if (resultKey == langKey)
{
return resultValue ?? key;
}
return key;
}
}