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

@ -1,73 +1,75 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{E2169518-26AB-4DA6-ACB2-6BCFB35D6953}</ProjectGuid> <ProjectGuid>{E2169518-26AB-4DA6-ACB2-6BCFB35D6953}</ProjectGuid>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder> <AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Localization</RootNamespace> <RootNamespace>Localization</RootNamespace>
<AssemblyName>Localization</AssemblyName> <AssemblyName>Localization</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion> <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType> <DebugType>full</DebugType>
<Optimize>false</Optimize> <Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath> <OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants> <DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType> <DebugType>pdbonly</DebugType>
<Optimize>true</Optimize> <Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath> <OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants> <DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="0Harmony, Version=2.0.2.0, Culture=neutral, PublicKeyToken=null"> <Reference Include="0Harmony, Version=2.0.2.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\packages\Lib.Harmony.2.0.2\lib\net48\0Harmony.dll</HintPath> <HintPath>..\packages\Lib.Harmony.2.0.2\lib\net48\0Harmony.dll</HintPath>
<Private>True</Private> <Private>True</Private>
</Reference> </Reference>
<Reference Include="GamecraftModdingAPI, Version=1.6.0.0, Culture=neutral, PublicKeyToken=null"> <Reference Include="GamecraftModdingAPI, Version=1.6.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\GamecraftModdingAPI\GamecraftModdingAPI\bin\Debug\net472\GamecraftModdingAPI.dll</HintPath> <HintPath>..\..\GamecraftModdingAPI\GamecraftModdingAPI\bin\Debug\net472\GamecraftModdingAPI.dll</HintPath>
</Reference> </Reference>
<Reference Include="IllusionPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"> <Reference Include="IllusionPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\GamecraftModdingAPI\GamecraftModdingAPI\bin\Debug\net472\IllusionPlugin.dll</HintPath> <HintPath>..\..\GamecraftModdingAPI\GamecraftModdingAPI\bin\Debug\net472\IllusionPlugin.dll</HintPath>
</Reference> </Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed"> <Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed">
<HintPath>..\..\ref\Gamecraft_Data\Managed\Newtonsoft.Json.dll</HintPath> <HintPath>..\..\ref\Gamecraft_Data\Managed\Newtonsoft.Json.dll</HintPath>
</Reference> </Reference>
<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="System" /> <Reference Include="Facepunch.Steamworks.Win64">
<Reference Include="System.Core" /> <HintPath>..\..\ref\Gamecraft_Data\Managed\Facepunch.Steamworks.Win64.dll</HintPath>
<Reference Include="System.Data" /> </Reference>
<Reference Include="System.Runtime.InteropServices.RuntimeInformation" /> <Reference Include="System" />
<Reference Include="System.Xml" /> <Reference Include="System.Core" />
</ItemGroup> <Reference Include="System.Data" />
<ItemGroup> <Reference Include="System.Runtime.InteropServices.RuntimeInformation" />
<Compile Include="LocalizationMod.cs" /> <Reference Include="System.Xml" />
<Compile Include="Properties\AssemblyInfo.cs" /> </ItemGroup>
</ItemGroup> <ItemGroup>
<ItemGroup> <Compile Include="LocalizationMod.cs" />
<None Include="packages.config" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <ItemGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild"> <Target Name="BeforeBuild">
</Target> </Target>
<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";
} }