-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
84 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,44 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Newtonsoft.Json; | ||
using Language; | ||
|
||
namespace GodSeekerPlus.Util { | ||
internal static class LocalizationUtil { | ||
private static Dictionary<string, Dictionary<string, string>> ReadLangs() { | ||
string path = Assembly.GetExecutingAssembly().Location; | ||
path = Path.GetFullPath(path); | ||
path = Path.GetDirectoryName(path); | ||
path = Path.Combine(path, "lang"); | ||
private const string resPrefix = "GodSeekerPlus.Lang."; | ||
private const string resPostfix = ".json"; | ||
private static readonly List<string> langs = Language.Language | ||
.GetLanguages() | ||
.Map(str => str.ToLower()) | ||
.ToList(); | ||
|
||
return Directory.GetFiles(path) | ||
.Filter(path => Path.GetExtension(path) == ".json") | ||
.Map(path => (Path.GetFileNameWithoutExtension(path), new StreamReader(path).ReadToEnd())) | ||
.Map(tuple => (tuple.Item1, | ||
(Dictionary<string, string>) JsonConvert.DeserializeObject(tuple.Item2, typeof(Dictionary<string, string>)) | ||
)) | ||
.Reduce( | ||
(dict, tuple) => { | ||
dict[tuple.Item1] = tuple.Item2; | ||
return dict; | ||
}, | ||
new Dictionary<string, Dictionary<string, string>>() | ||
); | ||
} | ||
private static string ToIdentifier(this LanguageCode code) => | ||
code.ToString().ToLower().Replace('_', '-'); | ||
|
||
private static string CurrentLang => | ||
Language.Language.CurrentLanguage().ToIdentifier(); | ||
|
||
private static Dictionary<string, Dictionary<string, string>> ReadLangs() => Assembly | ||
.GetExecutingAssembly() | ||
.GetManifestResourceNames() | ||
.Filter(name => name.EnclosedWith(resPrefix, resPostfix)) | ||
.Map(name => (lang: name.StripStart(resPrefix).StripEnd(resPostfix), path: name)) | ||
.Filter(tuple => langs.Contains(tuple.lang)) | ||
.Map(tuple => (tuple.lang, stream: Assembly.GetExecutingAssembly().GetManifestResourceStream(tuple.path))) | ||
.Map(tuple => (tuple.lang, json: tuple.stream.ReadToString())) | ||
.Map(tuple => (tuple.lang, table: MiscUtil.DeserializeJson<Dictionary<string, string>>(tuple.json))) | ||
.Reduce( | ||
(dict, tuple) => { | ||
Logger.LogDebug($"Loaded localization for lang: {tuple.lang}"); | ||
dict[tuple.lang] = tuple.table; | ||
return dict; | ||
}, | ||
new Dictionary<string, Dictionary<string, string>>() | ||
); | ||
|
||
private static Dictionary<string, Dictionary<string, string>> Dict { get; set; } = ReadLangs(); | ||
|
||
internal static string TryLocalize(string key) { | ||
try { | ||
return Dict[Language.Language.CurrentLanguage().ToString().ToLower().Replace('_', '-')][key]; | ||
} catch { | ||
return key; | ||
} | ||
} | ||
internal static string TryLocalize(string key) => | ||
MiscUtil.Try(() => Dict[CurrentLang][key], key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.IO; | ||
using Newtonsoft.Json; | ||
|
||
namespace GodSeekerPlus.Util { | ||
internal static class MiscUtil { | ||
internal static bool EnclosedWith(this string self, string start, string end) => | ||
self.StartsWith(start) && self.EndsWith(end); | ||
|
||
internal static string StripStart(this string self, string val) => | ||
self.StartsWith(val) ? self.Substring(val.Length) : self; | ||
|
||
internal static string StripEnd(this string self, string val) => | ||
self.EndsWith(val) ? self.Substring(0, self.Length - val.Length) : self; | ||
|
||
|
||
internal static string ReadToString(this Stream self) => | ||
new StreamReader(self).ReadToEnd(); | ||
|
||
|
||
internal static T DeserializeJson<T>(string json) => | ||
(T) JsonConvert.DeserializeObject(json, typeof(T)); | ||
|
||
|
||
internal static T Try<T>(Func<T> f, T @default) { | ||
try { | ||
return f(); | ||
} catch { | ||
return @default; | ||
} | ||
} | ||
} | ||
} |