Add ability to fetch translations for a given language

This commit is contained in:
jvyden 2022-04-13 18:58:07 -04:00
parent 25731dd84b
commit a340a77d74
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
18 changed files with 86 additions and 35 deletions

View file

@ -0,0 +1,36 @@
using System.Diagnostics;
using System.Reflection;
using System.Resources;
namespace LBPUnion.ProjectLighthouse.Localization;
public static class LocalizationManager
{
private static readonly string namespaceStr = typeof(LocalizationManager).Namespace ?? "";
public static string GetLocalizedString(TranslationAreas translationArea, string language, string key)
{
#if DEBUG
Console.WriteLine($"Attempting to load '{key}' for '{language}' ");
#endif
string resourceBasename = $"{namespaceStr}.{translationArea.ToString()}.{language}";
ResourceManager resourceManager = new(resourceBasename, Assembly.GetExecutingAssembly());
string? localizedString = resourceManager.GetString(key);
if (localizedString == null)
{
#if DEBUG
if (Debugger.IsAttached) Debugger.Break();
#endif
return $"{translationArea.ToString()}.{language}.{key}";
}
return localizedString;
}
public static IEnumerable<string> GetAvailableLanguages(TranslationAreas translationArea)
{
return Assembly.GetExecutingAssembly().GetManifestResourceNames().Where(r => r.StartsWith($"{namespaceStr}.{translationArea.ToString()}"));
}
}