Skip to content

Commit

Permalink
Added TranslationHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Feb 3, 2021
1 parent dbcefd3 commit 2dade99
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/Home.md
Expand Up @@ -95,6 +95,7 @@

## [KKAPI.Utilities](KKAPI.Utilities.md)

- [`ConfigurationManagerAttributes`](KKAPI.Utilities.md#configurationmanagerattributes)
- [`CoroutineUtils`](KKAPI.Utilities.md#coroutineutils)
- [`Extensions`](KKAPI.Utilities.md#extensions)
- [`HSceneUtils`](KKAPI.Utilities.md#hsceneutils)
Expand All @@ -106,5 +107,6 @@
- [`ResourceUtils`](KKAPI.Utilities.md#resourceutils)
- [`TextureUtils`](KKAPI.Utilities.md#textureutils)
- [`TextUtils`](KKAPI.Utilities.md#textutils)
- [`TranslationHelper`](KKAPI.Utilities.md#translationhelper)
- [`WindowsStringComparer`](KKAPI.Utilities.md#windowsstringcomparer)

51 changes: 51 additions & 0 deletions doc/KKAPI.Utilities.md
@@ -1,3 +1,31 @@
## `ConfigurationManagerAttributes`

Class that specifies how a setting should be displayed inside the ConfigurationManager settings window. Usage: You can use this copy of the class instead of including it in your own plugin. Make a new instance, assign any fields that you want to override, and pass it as a tag for your setting. If a field is null (default), it will be ignored and won't change how the setting is displayed. If a field is non-null (you assigned a value to it), it will override default behavior.
```csharp
public class KKAPI.Utilities.ConfigurationManagerAttributes

```

Fields

| Type | Name | Summary |
| --- | --- | --- |
| `Nullable<Boolean>` | Browsable | Show this setting in the settings screen at all? If false, don't show. |
| `String` | Category | Category the setting is under. Null to be directly under the plugin. |
| `Action<ConfigEntryBase>` | CustomDrawer | Custom setting editor (OnGUI code that replaces the default editor provided by ConfigurationManager). See below for a deeper explanation. Using a custom drawer will cause many of the other fields to do nothing. |
| `Object` | DefaultValue | If set, a "Default" button will be shown next to the setting to allow resetting to default. |
| `String` | Description | Optional description shown when hovering over the setting. Not recommended, provide the description when creating the setting instead. |
| `String` | DispName | Name of the setting. |
| `Nullable<Boolean>` | HideDefaultButton | Force the "Reset" button to not be displayed, even if a valid DefaultValue is available. |
| `Nullable<Boolean>` | HideSettingName | Force the setting name to not be displayed. Should only be used with a `KKAPI.Utilities.ConfigurationManagerAttributes.CustomDrawer` to get more space. Can be used together with `KKAPI.Utilities.ConfigurationManagerAttributes.HideDefaultButton` to gain even more space. |
| `Nullable<Boolean>` | IsAdvanced | If true, don't show the setting by default. User has to turn on showing advanced settings or search for it. |
| `Func<Object, String>` | ObjToStr | Custom converter from setting type to string for the built-in editor textboxes. |
| `Nullable<Int32>` | Order | Order of the setting on the settings list relative to other settings in a category. 0 by default, higher number is higher on the list. |
| `Nullable<Boolean>` | ReadOnly | Only show the value, don't allow editing it. |
| `Nullable<Boolean>` | ShowRangeAsPercent | Should the setting be shown as a percentage (only use with value range settings). |
| `Func<String, Object>` | StrToObj | Custom converter from string to setting type for the built-in editor textboxes. |


## `CoroutineUtils`

Utility methods for working with coroutines.
Expand Down Expand Up @@ -226,6 +254,29 @@ Static Methods
| `String` | PascalCaseToSentenceCase(this `String` str) | Convert PascalCase to Sentence case. |


## `TranslationHelper`

Class that abstracts away AutoTranslator. It lets you translate text to current language.
```csharp
public static class KKAPI.Utilities.TranslationHelper

```

Static Properties

| Type | Name | Summary |
| --- | --- | --- |
| `Boolean` | AutoTranslatorInstalled | True if a reasonably recent version of AutoTranslator is installed. It might return false for some very old versions that don't have the necessary APIs to make this class work. |


Static Methods

| Type | Name | Summary |
| --- | --- | --- |
| `void` | TranslateAsync(`String` untranslatedText, `Action<String>` onCompleted) | Queries AutoTranslator to provide a translated text for the untranslated text. If the translation cannot be found in the cache, it will make a request to the translator selected by the user. If AutoTranslator is not installed, this will do nothing. |
| `Boolean` | TryTranslate(`String` untranslatedText, `String&` translatedText) | Queries the plugin to provide a translated text for the untranslated text. If the translation cannot be found in the cache, the method returns false and returns null as the untranslated text. |


## `WindowsStringComparer`

String comparer that is equivalent to the one used by Windows Explorer to sort files (e.g. 2 will go before 10, unlike normal compare).
Expand Down
4 changes: 4 additions & 0 deletions src/AIAPI/AIAPI.csproj
Expand Up @@ -97,6 +97,10 @@
<HintPath>..\packages\IllusionLibs.AIGirl.UnityEngine.UIModule.2018.2.21.3\lib\net46\UnityEngine.UIModule.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="XUnity.AutoTranslator.Plugin.Core, Version=4.13.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\IllusionLibs.XUnity.AutoTranslator.Plugin.Core.4.13.0\lib\net46\XUnity.AutoTranslator.Plugin.Core.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="KoikatuAPI.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/AIAPI/packages.config
Expand Up @@ -13,4 +13,5 @@
<package id="IllusionLibs.AIGirl.UnityEngine.UIModule" version="2018.2.21.3" targetFramework="net46" developmentDependency="true" />
<package id="IllusionLibs.BepInEx" version="5.4.4" targetFramework="net46" developmentDependency="true" />
<package id="IllusionLibs.BepInEx.Harmony" version="2.2.0.1" targetFramework="net46" developmentDependency="true" />
<package id="IllusionLibs.XUnity.AutoTranslator.Plugin.Core" version="4.13.0" targetFramework="net46" developmentDependency="true" />
</packages>
4 changes: 4 additions & 0 deletions src/ECAPI/ECAPI.csproj
Expand Up @@ -109,6 +109,10 @@
<HintPath>..\packages\IllusionLibs.EmotionCreators.UnityEngine.UIModule.2017.4.24.3\lib\net46\UnityEngine.UIModule.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="XUnity.AutoTranslator.Plugin.Core, Version=4.13.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\IllusionLibs.XUnity.AutoTranslator.Plugin.Core.4.13.0\lib\net46\XUnity.AutoTranslator.Plugin.Core.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
1 change: 1 addition & 0 deletions src/ECAPI/packages.config
Expand Up @@ -14,4 +14,5 @@
<package id="IllusionLibs.EmotionCreators.UnityEngine.TextRenderingModule" version="2017.4.24.3" targetFramework="net46" developmentDependency="true" />
<package id="IllusionLibs.EmotionCreators.UnityEngine.UI" version="2017.4.24.3" targetFramework="net46" developmentDependency="true" />
<package id="IllusionLibs.EmotionCreators.UnityEngine.UIModule" version="2017.4.24.3" targetFramework="net46" developmentDependency="true" />
<package id="IllusionLibs.XUnity.AutoTranslator.Plugin.Core" version="4.13.0" targetFramework="net46" developmentDependency="true" />
</packages>
4 changes: 4 additions & 0 deletions src/HS2API/HS2API.csproj
Expand Up @@ -113,6 +113,10 @@
<HintPath>..\packages\IllusionLibs.HoneySelect2.UnityEngine.UIModule.2018.4.11.2\lib\net46\UnityEngine.UIModule.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="XUnity.AutoTranslator.Plugin.Core, Version=4.13.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\IllusionLibs.XUnity.AutoTranslator.Plugin.Core.4.13.0\lib\net46\XUnity.AutoTranslator.Plugin.Core.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="KoikatuAPI.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/HS2API/packages.config
Expand Up @@ -15,4 +15,5 @@
<package id="IllusionLibs.HoneySelect2.UnityEngine.TextRenderingModule" version="2018.4.11.2" targetFramework="net46" developmentDependency="true" />
<package id="IllusionLibs.HoneySelect2.UnityEngine.UI" version="2018.4.11.2" targetFramework="net46" developmentDependency="true" />
<package id="IllusionLibs.HoneySelect2.UnityEngine.UIModule" version="2018.4.11.2" targetFramework="net46" developmentDependency="true" />
<package id="IllusionLibs.XUnity.AutoTranslator.Plugin.Core" version="4.13.0" targetFramework="net46" developmentDependency="true" />
</packages>
4 changes: 4 additions & 0 deletions src/KKAPI/KKAPI.csproj
Expand Up @@ -75,6 +75,10 @@
<HintPath>..\packages\IllusionLibs.Koikatu.UnityEngine.UI.5.6.2.2\lib\net35\UnityEngine.UI.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="XUnity.AutoTranslator.Plugin.Core, Version=4.13.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\IllusionLibs.XUnity.AutoTranslator.Plugin.Core.4.13.0\lib\net35\XUnity.AutoTranslator.Plugin.Core.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Maker\AccessoryCopyEventArgs.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/KKAPI/packages.config
Expand Up @@ -8,4 +8,5 @@
<package id="IllusionLibs.Koikatu.TextMeshPro" version="2019.4.27.2" targetFramework="net35" developmentDependency="true" />
<package id="IllusionLibs.Koikatu.UnityEngine" version="5.6.2.2" targetFramework="net35" developmentDependency="true" />
<package id="IllusionLibs.Koikatu.UnityEngine.UI" version="5.6.2.2" targetFramework="net35" developmentDependency="true" />
<package id="IllusionLibs.XUnity.AutoTranslator.Plugin.Core" version="4.13.0" targetFramework="net35" developmentDependency="true" />
</packages>
4 changes: 4 additions & 0 deletions src/PHAPI/PHAPI.csproj
Expand Up @@ -73,6 +73,10 @@
<HintPath>..\packages\IllusionLibs.PlayHome.UnityEngine.UI.5.5.5.2\lib\net35\UnityEngine.UI.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="XUnity.AutoTranslator.Plugin.Core, Version=4.13.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\IllusionLibs.XUnity.AutoTranslator.Plugin.Core.4.13.0\lib\net35\XUnity.AutoTranslator.Plugin.Core.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Chara\CharacterApi.ControllerRegistration.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/PHAPI/packages.config
Expand Up @@ -7,4 +7,5 @@
<package id="IllusionLibs.PlayHome.Assembly-CSharp-firstpass" version="2018.3.11.2" targetFramework="net35" developmentDependency="true" />
<package id="IllusionLibs.PlayHome.UnityEngine" version="5.5.5.2" targetFramework="net35" developmentDependency="true" />
<package id="IllusionLibs.PlayHome.UnityEngine.UI" version="5.5.5.2" targetFramework="net35" developmentDependency="true" />
<package id="IllusionLibs.XUnity.AutoTranslator.Plugin.Core" version="4.13.0" targetFramework="net35" developmentDependency="true" />
</packages>
2 changes: 2 additions & 0 deletions src/Shared.Core/Shared.Core.projitems
Expand Up @@ -27,6 +27,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Maker\UI\BaseEditableGuiEntry.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Maker\UI\BaseGuiEntry.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Maker\UI\Sidebar\ISidebarControl.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\ConfigurationManagerAttributes.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\CoroutineUtils.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\HSceneUtils.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\IMGUIUtils.cs" />
Expand All @@ -38,6 +39,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Utilities\OpenFileDialog.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\RecycleBinUtil.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\Extensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\TranslationHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\WindowsStringComparer.cs" />
</ItemGroup>
</Project>
71 changes: 71 additions & 0 deletions src/Shared.Core/Utilities/TranslationHelper.cs
@@ -0,0 +1,71 @@
using System;
using System.Linq;
using XUnity.AutoTranslator.Plugin.Core;

namespace KKAPI.Utilities
{
/// <summary>
/// Class that abstracts away AutoTranslator. It lets you translate text to current language.
/// </summary>
public static class TranslationHelper
{
private static readonly Action<string, Action<string>> _translatorCallback;
private static readonly Func<string, string> _tryTranslateCallback;

/// <summary>
/// True if a reasonably recent version of AutoTranslator is installed.
/// It might return false for some very old versions that don't have the necessary APIs to make this class work.
/// </summary>
public static bool AutoTranslatorInstalled { get; }

static TranslationHelper()
{
var xua = Type.GetType("XUnity.AutoTranslator.Plugin.Core.ITranslator, XUnity.AutoTranslator.Plugin.Core", false);
if (xua != null && xua.GetMethods().Any(x => x.Name == "TranslateAsync"))
{
// The lambdas don't get their types resolved until called so this doesn't crash here if the type doesn't exist
_translatorCallback = (s, action) => AutoTranslator.Default.TranslateAsync(s, result => { if (result.Succeeded) action(result.TranslatedText); });
_tryTranslateCallback = s => AutoTranslator.Default.TryTranslate(s, out s) ? s : null;
AutoTranslatorInstalled = true;
}
else
{
KoikatuAPI.Logger.LogWarning("Could not find method AutoTranslator.Default.TranslateAsync, translations will be limited or unavailable");
_translatorCallback = null;
}
}

/// <summary>
/// Queries AutoTranslator to provide a translated text for the untranslated text.
/// If the translation cannot be found in the cache, it will make a request to the translator selected by the user.
/// If AutoTranslator is not installed, this will do nothing.
/// </summary>
/// <param name="untranslatedText">The untranslated text to provide a translation for.</param>
/// <param name="onCompleted">Callback with the completed translation. It can return immediately or at some later point.</param>
public static void TranslateAsync(string untranslatedText, Action<string> onCompleted)
{
if (onCompleted == null) throw new ArgumentNullException(nameof(onCompleted));
if (string.IsNullOrEmpty(untranslatedText)) return;
_translatorCallback?.Invoke(untranslatedText, onCompleted);
}

/// <summary>
/// Queries the plugin to provide a translated text for the untranslated text.
/// If the translation cannot be found in the cache, the method returns false
/// and returns null as the untranslated text.
/// </summary>
/// <param name="untranslatedText">The untranslated text to provide a translation for.</param>
/// <param name="translatedText">The translated text.</param>
public static bool TryTranslate(string untranslatedText, out string translatedText)
{
if (string.IsNullOrEmpty(untranslatedText) || _tryTranslateCallback == null)
{
translatedText = null;
return false;
}

translatedText = _tryTranslateCallback(untranslatedText);
return translatedText != null;
}
}
}

0 comments on commit 2dade99

Please sign in to comment.