refactor: new code style in Localization.cs

This commit is contained in:
goat 2021-03-31 15:26:16 +02:00
parent a8d2a577b9
commit 876993e620

View file

@ -1,58 +1,95 @@
using System; using System;
using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CheapLoc; using CheapLoc;
using Serilog; using Serilog;
namespace Dalamud namespace Dalamud
{ {
class Localization { /// <summary>
private readonly string workingDirectory; /// Class handling localization.
/// </summary>
public class Localization
{
/// <summary>
/// Array of language codes which have a valid translation in Dalamud.
/// </summary>
public static readonly string[] ApplicableLangCodes = { "de", "ja", "fr", "it", "es", "ko", "no", "ru" };
private const string FallbackLangCode = "en"; private const string FallbackLangCode = "en";
public static readonly string[] ApplicableLangCodes = { "de", "ja", "fr", "it", "es", "ko", "no", "ru" };
public delegate void LocalizationChangedDelegate(string langCode); private readonly string workingDirectory;
public event LocalizationChangedDelegate OnLocalizationChanged;
/// <summary>
public Localization(string workingDirectory) { /// 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)
{
this.workingDirectory = workingDirectory; this.workingDirectory = workingDirectory;
} }
public void SetupWithUiCulture() { /// <summary>
/// Delegate for the <see cref="Localization.OnLocalizationChanged"/> event that occurs when the language is changed.
/// </summary>
/// <param name="langCode">The language code of the new language.</param>
public delegate void LocalizationChangedDelegate(string langCode);
/// <summary>
/// Event that occurs when the language is changed.
/// </summary>
public event LocalizationChangedDelegate OnLocalizationChanged;
/// <summary>
/// Set up the UI language with the users' local UI culture.
/// </summary>
public void SetupWithUiCulture()
{
try try
{ {
var currentUiLang = CultureInfo.CurrentUICulture; var currentUiLang = CultureInfo.CurrentUICulture;
Log.Information("Trying to set up Loc for culture {0}", currentUiLang.TwoLetterISOLanguageName); Log.Information("Trying to set up Loc for culture {0}", currentUiLang.TwoLetterISOLanguageName);
if (ApplicableLangCodes.Any(x => currentUiLang.TwoLetterISOLanguageName == x)) { if (ApplicableLangCodes.Any(x => currentUiLang.TwoLetterISOLanguageName == x))
SetupWithLangCode(currentUiLang.TwoLetterISOLanguageName); {
} else { this.SetupWithLangCode(currentUiLang.TwoLetterISOLanguageName);
SetupWithFallbacks(); }
else
{
this.SetupWithFallbacks();
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Log.Error(ex, "Could not get language information. Setting up fallbacks."); Log.Error(ex, "Could not get language information. Setting up fallbacks.");
SetupWithFallbacks(); this.SetupWithFallbacks();
} }
} }
public void SetupWithFallbacks() { /// <summary>
OnLocalizationChanged?.Invoke(FallbackLangCode); /// Set up the UI language with "fallbacks"(original English text).
/// </summary>
public void SetupWithFallbacks()
{
this.OnLocalizationChanged?.Invoke(FallbackLangCode);
Loc.SetupWithFallbacks(); Loc.SetupWithFallbacks();
} }
public void SetupWithLangCode(string langCode) { /// <summary>
if (langCode.ToLower() == FallbackLangCode) { /// Set up the UI language with the provided language code.
SetupWithFallbacks(); /// </summary>
/// <param name="langCode">The language code to set up the UI language with.</param>
public void SetupWithLangCode(string langCode)
{
if (langCode.ToLower() == FallbackLangCode)
{
this.SetupWithFallbacks();
return; return;
} }
OnLocalizationChanged?.Invoke(langCode); this.OnLocalizationChanged?.Invoke(langCode);
Loc.Setup(File.ReadAllText(Path.Combine(this.workingDirectory, "UIRes", "loc", "dalamud", $"dalamud_{langCode}.json"))); Loc.Setup(File.ReadAllText(Path.Combine(this.workingDirectory, "UIRes", "loc", "dalamud", $"dalamud_{langCode}.json")));
} }
} }