feat: update loc to allow use by plugins

This commit is contained in:
kalilistic 2021-04-10 13:45:28 -04:00
parent 2a585e8d5d
commit 13eaa4450d
4 changed files with 57 additions and 9 deletions

View file

@ -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;
}

View file

@ -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

View file

@ -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;
/// <summary>
/// Initializes a new instance of the <see cref="Localization"/> class.
/// </summary>
/// <param name="workingDirectory">The working directory to load language files from.</param>
public Localization(string workingDirectory)
/// <param name="locResourceDirectory">The working directory to load language files from.</param>
/// <param name="locResourcePrefix">The prefix on the loc resource file name (e.g. dalamud_).</param>
public Localization(string locResourceDirectory, string locResourcePrefix = "")
{
this.workingDirectory = workingDirectory;
this.locResourceDirectory = locResourceDirectory;
this.locResourcePrefix = locResourcePrefix;
this.assembly = Assembly.GetCallingAssembly();
}
/// <summary>
@ -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);
}
/// <summary>
@ -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();
}
}
/// <summary>
/// Saves localizable JSON data in the current working directory for the provided assembly.
/// </summary>
public void ExportLocalizable()
{
Loc.ExportLocalizableForAssembly(this.assembly);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="key">The string key to be returned.</param>
/// <param name="fallBack">The fallback string, usually your source language.</param>
/// <returns>The localized string, fallback or string key if not found.</returns>
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"));
}
}
}

View file

@ -189,6 +189,12 @@ namespace Dalamud.Plugin
/// <returns>directory with path of AppData/XIVLauncher/pluginConfig/PluginInternalName.</returns>
public string GetPluginConfigDirectory() => this.configs.GetDirectory(this.pluginName);
/// <summary>
/// Get the loc directory.
/// </summary>
/// <returns>directory with path of AppData/XIVLauncher/pluginConfig/PluginInternalName/loc.</returns>
public string GetPluginLocDirectory() => this.configs.GetDirectory(Path.Combine(this.pluginName, "loc"));
#region Chat Links
/// <summary>