Browse Source

Load translation, SetLanguage command

master
NorbiPeti 3 years ago
commit
4d436bbfcf
5 changed files with 215 additions and 0 deletions
  1. +5
    -0
      .gitignore
  2. +16
    -0
      Localization.sln
  3. +73
    -0
      Localization/Localization.csproj
  4. +86
    -0
      Localization/LocalizationMod.cs
  5. +35
    -0
      Localization/Properties/AssemblyInfo.cs

+ 5
- 0
.gitignore View File

@@ -0,0 +1,5 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/

+ 16
- 0
Localization.sln View File

@@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Localization", "Localization\Localization.csproj", "{E2169518-26AB-4DA6-ACB2-6BCFB35D6953}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E2169518-26AB-4DA6-ACB2-6BCFB35D6953}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E2169518-26AB-4DA6-ACB2-6BCFB35D6953}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E2169518-26AB-4DA6-ACB2-6BCFB35D6953}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E2169518-26AB-4DA6-ACB2-6BCFB35D6953}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

+ 73
- 0
Localization/Localization.csproj View File

@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="utf-8"?>
<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')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{E2169518-26AB-4DA6-ACB2-6BCFB35D6953}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Localization</RootNamespace>
<AssemblyName>Localization</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="0Harmony, Version=2.0.2.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\packages\Lib.Harmony.2.0.2\lib\net48\0Harmony.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="GamecraftModdingAPI, Version=1.6.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\GamecraftModdingAPI\GamecraftModdingAPI\bin\Debug\net472\GamecraftModdingAPI.dll</HintPath>
</Reference>
<Reference Include="IllusionPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\GamecraftModdingAPI\GamecraftModdingAPI\bin\Debug\net472\IllusionPlugin.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed">
<HintPath>..\..\ref\Gamecraft_Data\Managed\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="RobocraftX.Common, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\ref\Gamecraft_Data\Managed\RobocraftX.Common.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Runtime.InteropServices.RuntimeInformation" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="LocalizationMod.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<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.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->

</Project>

+ 86
- 0
Localization/LocalizationMod.cs View File

@@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.IO;
using GamecraftModdingAPI;
using GamecraftModdingAPI.Commands;
using GamecraftModdingAPI.Utility;
using HarmonyLib;
using IllusionPlugin;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ServiceLayer;

namespace Localization
{
public class LocalizationMod : IEnhancedPlugin
{
private readonly DirectoryInfo _pluginFolder = new DirectoryInfo(Path.Combine("Plugins", "Localization"));
public override void OnApplicationStart()
{
Main.Init();
if (!_pluginFolder.Exists)
_pluginFolder.Create();
string settingsPath = Path.Combine(_pluginFolder.FullName, "settings.json");
JObject settings;
if (File.Exists(settingsPath))
{
try
{
using (var stream = new JsonTextReader(File.OpenText(settingsPath)))
settings = JObject.Load(stream);
string lang = (string) (settings["lang"] ?? (settings["lang"] = "en-us"));
LoadTranslation(lang);
}
catch (Exception e)
{
Logging.LogError(e);
settings = new JObject();
}
}
else
settings = new JObject();

CommandBuilder.Builder("SetLanguage", "Sets the game's language")
.Action<string>(lang =>
{
settings["lang"] = lang;
using (var stream = new JsonTextWriter(new StreamWriter(File.OpenWrite(settingsPath))))
settings.WriteTo(stream);
LoadTranslation(lang);
}).Build();
}

public override void OnApplicationQuit()
{
Main.Shutdown();
}

private void LoadTranslation(string lang)
{
Logging.CommandLog("Loading translation for " + lang + "...");
string langPath = Path.Combine(_pluginFolder.FullName, lang + ".json");
if (!File.Exists(langPath))
{
Logging.CommandLogError("Could not find json file!");
return;
}

var strings = JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText(langPath));
var origStrings =
(Dictionary<string, string>) AccessTools.Field(typeof(LocalizationService), "LocalizedStrings")
.GetValue(null);
foreach (var kv in strings)
{
if (!origStrings.Remove(kv.Key))
Logging.CommandLogWarning(kv.Key + " wasn't in the original file.");
origStrings.Add(kv.Key, kv.Value);
}

Logging.CommandLog("Updated " + strings.Count + " strings (" + origStrings.Count + " strings in total)");
}

public override string Name { get; } = "LocalizationMod";
public override string Version { get; } = "1.0.0";
}
}

+ 35
- 0
Localization/Properties/AssemblyInfo.cs View File

@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Localization")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Localization")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("E2169518-26AB-4DA6-ACB2-6BCFB35D6953")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

Loading…
Cancel
Save