Add Steam language auto-detection

This commit is contained in:
NGnius (Graham) 2020-10-05 14:16:03 -04:00
parent 2642f80b8a
commit 53fc61da14
2 changed files with 119 additions and 66 deletions

View file

@ -48,6 +48,9 @@
<Reference Include="RobocraftX.Common, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"> <Reference Include="RobocraftX.Common, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\ref\Gamecraft_Data\Managed\RobocraftX.Common.dll</HintPath> <HintPath>..\..\ref\Gamecraft_Data\Managed\RobocraftX.Common.dll</HintPath>
</Reference> </Reference>
<Reference Include="Facepunch.Steamworks.Win64">
<HintPath>..\..\ref\Gamecraft_Data\Managed\Facepunch.Steamworks.Win64.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
@ -69,5 +72,4 @@
<Target Name="AfterBuild"> <Target Name="AfterBuild">
</Target> </Target>
--> -->
</Project> </Project>

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using GamecraftModdingAPI; using GamecraftModdingAPI;
using GamecraftModdingAPI.App;
using GamecraftModdingAPI.Commands; using GamecraftModdingAPI.Commands;
using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Utility;
using HarmonyLib; using HarmonyLib;
@ -67,8 +68,22 @@ namespace Localization
.Action(() => .Action(() =>
{ {
File.WriteAllText(Path.Combine(_pluginFolder.FullName, "dumped.json"), File.WriteAllText(Path.Combine(_pluginFolder.FullName, "dumped.json"),
JsonConvert.SerializeObject(_origStrings)); JsonConvert.SerializeObject(_origStrings, Formatting.Indented));
}).Build(); }).Build();
if (!settings.ContainsKey("autoload") || (bool) settings["autoload"])
{
settings["autoload"] = true;
using (var stream = new JsonTextWriter(new StreamWriter(File.OpenWrite(settingsPath))))
settings.WriteTo(stream);
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()
@ -114,6 +129,42 @@ namespace Localization
.GetValue(null); .GetValue(null);
} }
private void TryLoadSteamLanguageMenuEnterEvent(object sender, MenuEventArgs args)
{
TryLoadSteamLanguage();
Client.EnterMenu -= TryLoadSteamLanguageMenuEnterEvent;
}
private void TryLoadSteamLanguage()
{
string lang = Steamworks.SteamUtils.SteamUILanguage;
string first2 = lang.Substring(0, 2);
// try some possibly valid language codes
if (File.Exists(Path.Combine(_pluginFolder.FullName, lang + ".json")))
{
Logging.MetaLog($"Automatically detected Steam UI language {lang}, loading translations...");
LoadTranslation(lang);
}
else if (File.Exists(Path.Combine(_pluginFolder.FullName, first2 + ".json")))
{
lang = first2;
Logging.MetaLog($"Automatically detected Steam UI language {lang}, loading translations...");
LoadTranslation(lang);
}
else if (File.Exists(Path.Combine(_pluginFolder.FullName, first2 + "-" + first2 + ".json")))
{
// a lot of standard languages codes are like fr-fr (French from France), but this won't work for English (nor fr-CA French from Canada)
lang = first2 + "-" + first2;
Logging.MetaLog($"Automatically detected Steam UI language {lang}, loading translations...");
LoadTranslation(lang);
}
else
{
Logging.MetaLog($"Translations for Steam UI language {lang} are not available, skipping...");
}
}
public override string Name { get; } = "LocalizationMod"; public override string Name { get; } = "LocalizationMod";
public override string Version { get; } = "1.0.0"; public override string Version { get; } = "1.0.0";
} }