From 13eaa4450d64efa72c019a3ed4595cbfd29687c0 Mon Sep 17 00:00:00 2001
From: kalilistic <35899782+kalilistic@users.noreply.github.com>
Date: Sat, 10 Apr 2021 13:45:28 -0400
Subject: [PATCH] feat: update loc to allow use by plugins
---
Dalamud.Test/LocalizationTests.cs | 2 +-
Dalamud/Dalamud.cs | 2 +-
Dalamud/Localization.cs | 56 +++++++++++++++++++++---
Dalamud/Plugin/DalamudPluginInterface.cs | 6 +++
4 files changed, 57 insertions(+), 9 deletions(-)
diff --git a/Dalamud.Test/LocalizationTests.cs b/Dalamud.Test/LocalizationTests.cs
index 5b6bf1e1f..67977f805 100644
--- a/Dalamud.Test/LocalizationTests.cs
+++ b/Dalamud.Test/LocalizationTests.cs
@@ -9,7 +9,7 @@ namespace Dalamud.Test {
public LocalizationTests() {
var workingDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
- this.localization = new Localization(workingDir);
+ this.localization = new Localization(workingDir, "dalamud_");
this.localization.OnLocalizationChanged += code => this.currentLangCode = code;
}
diff --git a/Dalamud/Dalamud.cs b/Dalamud/Dalamud.cs
index 8855b7e85..8027d9329 100644
--- a/Dalamud/Dalamud.cs
+++ b/Dalamud/Dalamud.cs
@@ -218,7 +218,7 @@ namespace Dalamud
Log.Verbose("[START] CS OK!");
- this.LocalizationManager = new Localization(this.AssetDirectory.FullName);
+ this.LocalizationManager = new Localization(Path.Combine(this.AssetDirectory.FullName, "UIRes", "loc", "dalamud"), "dalamud_");
if (!string.IsNullOrEmpty(this.Configuration.LanguageOverride))
this.LocalizationManager.SetupWithLangCode(this.Configuration.LanguageOverride);
else
diff --git a/Dalamud/Localization.cs b/Dalamud/Localization.cs
index ad2e06dd8..fe13c6bff 100644
--- a/Dalamud/Localization.cs
+++ b/Dalamud/Localization.cs
@@ -2,6 +2,7 @@ using System;
using System.Globalization;
using System.IO;
using System.Linq;
+using System.Reflection;
using CheapLoc;
using Serilog;
@@ -20,15 +21,20 @@ namespace Dalamud
private const string FallbackLangCode = "en";
- private readonly string workingDirectory;
+ private readonly string locResourceDirectory;
+ private readonly string locResourcePrefix;
+ private readonly Assembly assembly;
///
/// Initializes a new instance of the class.
///
- /// The working directory to load language files from.
- public Localization(string workingDirectory)
+ /// The working directory to load language files from.
+ /// The prefix on the loc resource file name (e.g. dalamud_).
+ public Localization(string locResourceDirectory, string locResourcePrefix = "")
{
- this.workingDirectory = workingDirectory;
+ this.locResourceDirectory = locResourceDirectory;
+ this.locResourcePrefix = locResourcePrefix;
+ this.assembly = Assembly.GetCallingAssembly();
}
///
@@ -52,7 +58,7 @@ namespace Dalamud
var currentUiLang = CultureInfo.CurrentUICulture;
Log.Information("Trying to set up Loc for culture {0}", currentUiLang.TwoLetterISOLanguageName);
- if (ApplicableLangCodes.Any(x => currentUiLang.TwoLetterISOLanguageName == x))
+ if (ApplicableLangCodes.Any(langCode => currentUiLang.TwoLetterISOLanguageName == langCode))
{
this.SetupWithLangCode(currentUiLang.TwoLetterISOLanguageName);
}
@@ -74,7 +80,7 @@ namespace Dalamud
public void SetupWithFallbacks()
{
this.OnLocalizationChanged?.Invoke(FallbackLangCode);
- Loc.SetupWithFallbacks();
+ Loc.SetupWithFallbacks(this.assembly);
}
///
@@ -90,7 +96,43 @@ namespace Dalamud
}
this.OnLocalizationChanged?.Invoke(langCode);
- Loc.Setup(File.ReadAllText(Path.Combine(this.workingDirectory, "UIRes", "loc", "dalamud", $"dalamud_{langCode}.json")));
+ try
+ {
+ Loc.Setup(this.ReadLocData(langCode), this.assembly);
+ }
+ catch (Exception ex)
+ {
+ Log.Error(ex, "Could not load loc {0}. Setting up fallbacks.", langCode);
+ this.SetupWithFallbacks();
+ }
+ }
+
+ ///
+ /// Saves localizable JSON data in the current working directory for the provided assembly.
+ ///
+ public void ExportLocalizable()
+ {
+ Loc.ExportLocalizableForAssembly(this.assembly);
+ }
+
+ ///
+ /// Search the set-up localization data for the provided assembly for the given string key and return it.
+ /// If the key is not present, the fallback is shown.
+ /// The fallback is also required to create the string files to be localized.
+ ///
+ /// The string key to be returned.
+ /// The fallback string, usually your source language.
+ /// The localized string, fallback or string key if not found.
+ public string Localize(string key, string fallBack)
+ {
+ return Loc.Localize(key, fallBack, this.assembly);
+ }
+
+ private string ReadLocData(string langCode)
+ {
+ return File.ReadAllText(Path.Combine(
+ this.locResourceDirectory,
+ $"{this.locResourcePrefix}{langCode}.json"));
}
}
}
diff --git a/Dalamud/Plugin/DalamudPluginInterface.cs b/Dalamud/Plugin/DalamudPluginInterface.cs
index 05097b832..650ee6313 100644
--- a/Dalamud/Plugin/DalamudPluginInterface.cs
+++ b/Dalamud/Plugin/DalamudPluginInterface.cs
@@ -189,6 +189,12 @@ namespace Dalamud.Plugin
/// directory with path of AppData/XIVLauncher/pluginConfig/PluginInternalName.
public string GetPluginConfigDirectory() => this.configs.GetDirectory(this.pluginName);
+ ///
+ /// Get the loc directory.
+ ///
+ /// directory with path of AppData/XIVLauncher/pluginConfig/PluginInternalName/loc.
+ public string GetPluginLocDirectory() => this.configs.GetDirectory(Path.Combine(this.pluginName, "loc"));
+
#region Chat Links
///