mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-06-17 13:11:28 +00:00
Add ability to fetch translations for a given language
This commit is contained in:
parent
25731dd84b
commit
a340a77d74
18 changed files with 86 additions and 35 deletions
36
ProjectLighthouse.Localization/LocalizationManager.cs
Normal file
36
ProjectLighthouse.Localization/LocalizationManager.cs
Normal 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()}"));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue