Add support for embedded resources and add strings
This commit is contained in:
parent
53fc61da14
commit
ae8fd9f7d5
4 changed files with 1215 additions and 22 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -2,4 +2,6 @@ bin/
|
||||||
obj/
|
obj/
|
||||||
/packages/
|
/packages/
|
||||||
riderModule.iml
|
riderModule.iml
|
||||||
/_ReSharper.Caches/
|
/_ReSharper.Caches/
|
||||||
|
.idea/
|
||||||
|
/Localization.sln.DotSettings.user
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
using GamecraftModdingAPI;
|
using GamecraftModdingAPI;
|
||||||
using GamecraftModdingAPI.App;
|
using GamecraftModdingAPI.App;
|
||||||
using GamecraftModdingAPI.Commands;
|
using GamecraftModdingAPI.Commands;
|
||||||
|
@ -18,6 +19,7 @@ namespace Localization
|
||||||
{
|
{
|
||||||
private readonly DirectoryInfo _pluginFolder = new DirectoryInfo(Path.Combine("Plugins", "Localization"));
|
private readonly DirectoryInfo _pluginFolder = new DirectoryInfo(Path.Combine("Plugins", "Localization"));
|
||||||
private Dictionary<string, string> _origStrings;
|
private Dictionary<string, string> _origStrings;
|
||||||
|
private readonly string[] _embeddedLanguages = Assembly.GetExecutingAssembly().GetManifestResourceNames();
|
||||||
|
|
||||||
public override void OnApplicationStart()
|
public override void OnApplicationStart()
|
||||||
{
|
{
|
||||||
|
@ -60,30 +62,29 @@ namespace Localization
|
||||||
.Action(() =>
|
.Action(() =>
|
||||||
{
|
{
|
||||||
Logging.CommandLog(new[] {"en"}.Concat(_pluginFolder.EnumerateFiles()
|
Logging.CommandLog(new[] {"en"}.Concat(_pluginFolder.EnumerateFiles()
|
||||||
.Where(file => file.Extension == ".json" && file.Name.Length < 8)
|
.Where(file => file.Extension == ".json"
|
||||||
.Select(file => file.Name.Replace(file.Extension, ""))).Distinct()
|
&& (file.Name.Length < 8 || file.Name.Contains("-")))
|
||||||
.Aggregate((a, b) => a + ", " + b));
|
.Select(file => file.Name.Replace(file.Extension, "")))
|
||||||
|
.Concat(_embeddedLanguages)
|
||||||
|
.Select(lang => lang.Replace("Localization.", "").Replace(".json", ""))
|
||||||
|
.Distinct().Aggregate((a, b) => a + ", " + b));
|
||||||
}).Build();
|
}).Build();
|
||||||
CommandBuilder.Builder("DumpLangStrings", "Dumps the current strings. Set language to en-us first.")
|
CommandBuilder.Builder("DumpLangStrings", "Dumps the current strings. Set language to en first.")
|
||||||
.Action(() =>
|
.Action(() =>
|
||||||
{
|
{
|
||||||
File.WriteAllText(Path.Combine(_pluginFolder.FullName, "dumped.json"),
|
File.WriteAllText(Path.Combine(_pluginFolder.FullName, "dumped.json"),
|
||||||
JsonConvert.SerializeObject(_origStrings, Formatting.Indented));
|
JsonConvert.SerializeObject(_origStrings, Formatting.Indented));
|
||||||
}).Build();
|
}).Build();
|
||||||
|
|
||||||
if (!settings.ContainsKey("autoload") || (bool) settings["autoload"])
|
if (!settings.ContainsKey("autoload"))
|
||||||
{
|
{
|
||||||
settings["autoload"] = true;
|
settings["autoload"] = true;
|
||||||
using (var stream = new JsonTextWriter(new StreamWriter(File.OpenWrite(settingsPath))))
|
using (var stream = new JsonTextWriter(new StreamWriter(File.OpenWrite(settingsPath))))
|
||||||
settings.WriteTo(stream);
|
settings.WriteTo(stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((bool) settings["autoload"])
|
||||||
Client.EnterMenu += TryLoadSteamLanguageMenuEnterEvent;
|
Client.EnterMenu += TryLoadSteamLanguageMenuEnterEvent;
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
settings["autoload"] = false;
|
|
||||||
using (var stream = new JsonTextWriter(new StreamWriter(File.OpenWrite(settingsPath))))
|
|
||||||
settings.WriteTo(stream);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void OnApplicationQuit()
|
public override void OnApplicationQuit()
|
||||||
|
@ -98,21 +99,42 @@ namespace Localization
|
||||||
string langPath = Path.Combine(_pluginFolder.FullName, lang + ".json");
|
string langPath = Path.Combine(_pluginFolder.FullName, lang + ".json");
|
||||||
if (!File.Exists(langPath))
|
if (!File.Exists(langPath))
|
||||||
{
|
{
|
||||||
if (lang == "en")
|
if (_embeddedLanguages.Contains("Localization." + lang + ".json"))
|
||||||
|
langPath = null;
|
||||||
|
else
|
||||||
{
|
{
|
||||||
AccessTools.Method(typeof(LocalizationService), "Init").Invoke(null, new object[0]);
|
if (lang == "en")
|
||||||
_origStrings = null; //Reset as the object changed
|
{
|
||||||
LoadOriginalStrings();
|
AccessTools.Method(typeof(LocalizationService), "Init").Invoke(null, new object[0]);
|
||||||
Logging.CommandLog("Strings reset to default. (" + _origStrings.Count + " strings in total)");
|
_origStrings = null; //Reset as the object changed
|
||||||
|
LoadOriginalStrings();
|
||||||
|
Logging.CommandLog("Strings reset to default. (" + _origStrings.Count + " strings in total)");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Logging.CommandLogError("Could not find json file!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Logging.CommandLogError("Could not find json file!");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var strings = JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText(langPath));
|
string jsonText = langPath != null ? File.ReadAllText(langPath) : null;
|
||||||
foreach (var kv in strings)
|
if (jsonText == null)
|
||||||
{
|
{
|
||||||
|
var stream = Assembly.GetExecutingAssembly()
|
||||||
|
.GetManifestResourceStream("Localization." + lang + ".json");
|
||||||
|
if (stream != null)
|
||||||
|
using (var st = new StreamReader(stream))
|
||||||
|
jsonText = st.ReadToEnd();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Logging.CommandLogError("Failed to load embedded translation for " + lang);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var strings = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonText);
|
||||||
|
foreach (var kv in strings)
|
||||||
|
{
|
||||||
if (!_origStrings.Remove(kv.Key))
|
if (!_origStrings.Remove(kv.Key))
|
||||||
Logging.CommandLogWarning(kv.Key + " wasn't in the original file.");
|
Logging.CommandLogWarning(kv.Key + " wasn't in the original file.");
|
||||||
_origStrings.Add(kv.Key, kv.Value);
|
_origStrings.Add(kv.Key, kv.Value);
|
||||||
|
|
4
Localization/packages.config
Normal file
4
Localization/packages.config
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<packages>
|
||||||
|
<package id="Lib.Harmony" version="2.0.2" targetFramework="net48" />
|
||||||
|
</packages>
|
1165
dumped.json
Normal file
1165
dumped.json
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue