Skip to content

Commit

Permalink
Add localization .json file (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
drojf committed Sep 21, 2022
1 parent 83285b0 commit 55ced8a
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions Assembly-CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@
<Compile Include="MOD.Scripts.Core.State\MODStateDisableInput.cs" />
<Compile Include="MOD.Scripts.Core.State\StateMovie.cs" />
<Compile Include="MOD.Scripts.Core.TextWindow\MODTextController.cs" />
<Compile Include="MOD.Scripts.Core\MODLocalization.cs" />
<Compile Include="MOD.Scripts.Core\MODLogger.cs" />
<Compile Include="MOD.Scripts.Core\MODSystem.cs" />
<Compile Include="MOD.Scripts.Core\MODUtility.cs" />
Expand Down
2 changes: 2 additions & 0 deletions Assets.Scripts.Core/GameSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Assets.Scripts.UI.Choice;
using Assets.Scripts.UI.Config;
using Assets.Scripts.UI.Prompt;
using MOD.Scripts.Core;
using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -239,6 +240,7 @@ public GameState GameState
private void Initialize()
{
Logger.Log($"GameSystem: Starting GameSystem - DLL Version: {MODUtility.InformationalVersion()}");
MODLocalization.LoadFromJSON();
IsInitialized = true;
AssetManager = new AssetManager();
AudioController = new AudioController();
Expand Down
124 changes: 124 additions & 0 deletions MOD.Scripts.Core/MODLocalization.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using Assets.Scripts.Core;
using MOD.Scripts.UI;

namespace MOD.Scripts.Core
{
public class LocalizationEntry
{
public string comment;
public string text;
public string textJP;
}

public class LocalizationInfo
{
public Dictionary<string, LocalizationEntry> allChapters;

public LocalizationInfo()
{
allChapters = new Dictionary<string, LocalizationEntry>();
}
}

public class MODLocalization
{
private static bool initializeAttempted;
private static readonly LocalizationEntry notInitializedEntry;
private static readonly LocalizationEntry defaultEntry;
private static readonly LocalizationInfo fallbackInfo;
private static LocalizationInfo info;

static MODLocalization()
{
// This entry is shown if you try to access this class too early (before GameSystem has inititalized).
notInitializedEntry = new LocalizationEntry()
{
comment = "This shows if LoadFromJSON() has not been called yet",
text = "#NOT INITIALIZED#",
textJP = "#NOT INITIALIZED#",
};

defaultEntry = new LocalizationEntry()
{
comment = "This localization entry is missing!",
text = "#TEXT MISSING#",
textJP = "#TEXT MISSING#",
};

info = new LocalizationInfo();
fallbackInfo = new LocalizationInfo();

void addFallbackEntry(string name, string comment, string text, string textJP)
{
fallbackInfo.allChapters[name] = new LocalizationEntry()
{
comment = comment,
text = text,
textJP = textJP,
};
}

addFallbackEntry(
name: "no-tips-available",
comment: "This text appears at the top of the tips menu when there are no tips available",
text: "No new tips available",
textJP: "入手TIPSはありません"
);
}

private static LocalizationEntry GetEntry(string name)
{
if(!initializeAttempted)
{
return notInitializedEntry;
}

if (info.allChapters.TryGetValue(name, out LocalizationEntry entry))
{
return entry;
}

if (fallbackInfo.allChapters.TryGetValue(name, out LocalizationEntry fallbackEntry))
{
return fallbackEntry;
}

return defaultEntry;
}

public static string Get(string name)
{
LocalizationEntry entry = GetEntry(name);
return GameSystem.Instance.UseEnglishText ? entry.text : entry.textJP;
}

public static void LoadFromJSON()
{
initializeAttempted = true;
string localizationPath = Path.Combine(MODSystem.BaseDirectory, "localization.json");

if (!File.Exists(localizationPath))
{
Debug.Log($"MODLocalizationController(): No localization file at [{localizationPath}] - will use hardcoded localization");
return;
}

try
{
using (var reader = new JsonTextReader(new StreamReader(localizationPath)))
{
info = JsonSerializer.Create(new JsonSerializerSettings()).Deserialize<LocalizationInfo>(reader);
}
}
catch (System.Exception e)
{
Debug.LogError($"MODLocalizationController(): Failed to read localization file at [{localizationPath}]: {e.Message}");
MODToaster.Show("localization.json fail - check logs");
}
}
}
}

0 comments on commit 55ced8a

Please sign in to comment.