Gladiator Idle (MonoGame) – Spiel mit 3D-Gladiatoren
Nur was zum Bauen und Starten nötig ist: Mono/ (Spiel), Game/, Core/, Languages/, Assets/ (Bilder, Clips, 3D-Figuren, Ton), build-release.bat. Bauen: build-release.bat → Spiel\GladiatorIdle.exe Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
165
Core/LocalizationManager.cs
Normal file
165
Core/LocalizationManager.cs
Normal file
@@ -0,0 +1,165 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace GladiatorIdle
|
||||
{
|
||||
/// <summary>
|
||||
/// JSON-basierte Lokalisierung.
|
||||
/// Sprachdateien liegen in Languages/{lang}.json neben der EXE.
|
||||
/// Fallback: Englisch → danach der rohe Key.
|
||||
///
|
||||
/// Neue Sprache hinzufügen: JSON-Datei in Languages/ ablegen (z.B. de.json).
|
||||
/// Neuer Feature-String: immer zuerst in en.json eintragen, dann in allen anderen Sprachen.
|
||||
/// </summary>
|
||||
public static class L
|
||||
{
|
||||
public enum Lang { En, De, Es }
|
||||
|
||||
public static Lang Current { get; private set; } = Lang.En;
|
||||
|
||||
static Dictionary<string, string> _strings = new();
|
||||
|
||||
static string SettingsPath => Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
|
||||
"GladiatorIdle", "lang.txt");
|
||||
|
||||
static string LangFolder =>
|
||||
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Languages");
|
||||
|
||||
/// <summary>Wurde schon einmal eine Sprache gewählt? (sonst Sprachauswahl vor dem Intro)</summary>
|
||||
public static bool HasSavedLanguage => File.Exists(SettingsPath);
|
||||
|
||||
/// <summary>Einmalig beim App-Start aufrufen — lädt gespeicherte Sprachpräferenz.</summary>
|
||||
public static void Load()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (File.Exists(SettingsPath) &&
|
||||
Enum.TryParse<Lang>(File.ReadAllText(SettingsPath).Trim(), out var saved))
|
||||
Current = saved;
|
||||
}
|
||||
catch { }
|
||||
LoadFile();
|
||||
}
|
||||
|
||||
/// <summary>Sprache wechseln, Präferenz speichern, Strings neu laden.</summary>
|
||||
public static void Set(Lang lang)
|
||||
{
|
||||
Current = lang;
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(SettingsPath)!);
|
||||
File.WriteAllText(SettingsPath, lang.ToString());
|
||||
}
|
||||
catch { }
|
||||
LoadFile();
|
||||
}
|
||||
|
||||
/// <summary>Gespeicherte Sprache (kann im laufenden Spiel von <see cref="Current"/> abweichen).</summary>
|
||||
public static Lang Saved
|
||||
{
|
||||
get
|
||||
{
|
||||
try
|
||||
{
|
||||
if (File.Exists(SettingsPath) && Enum.TryParse<Lang>(File.ReadAllText(SettingsPath).Trim(), out var s))
|
||||
return s;
|
||||
}
|
||||
catch { }
|
||||
return Current;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Sprache nur speichern, gilt ab dem nächsten Start der Startseite (mitten im
|
||||
/// Leben würden sonst alte und neue Texte gemischt).</summary>
|
||||
public static void SaveForNextStart(Lang lang)
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(SettingsPath)!);
|
||||
File.WriteAllText(SettingsPath, lang.ToString());
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
/// <summary>Sprache nur für diese Sitzung (Testläufe), nichts wird gespeichert.</summary>
|
||||
public static void UseForSession(Lang lang)
|
||||
{
|
||||
Current = lang;
|
||||
LoadFile();
|
||||
}
|
||||
|
||||
/// <summary>Gespeicherte, aber noch nicht aktive Sprache jetzt laden (z. B. beim Neustart).</summary>
|
||||
public static void ApplySaved()
|
||||
{
|
||||
if (DevShot.Lang != null) return; // Testlauf mit --lang
|
||||
var s = Saved;
|
||||
if (s != Current) { Current = s; LoadFile(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lokalisierten String zurückgeben.
|
||||
/// Fallback: Englisch → Key selbst (fehlende Keys werden im Debug geloggt).
|
||||
/// </summary>
|
||||
public static string t(string key)
|
||||
{
|
||||
if (_strings.TryGetValue(key, out var val)) return val;
|
||||
|
||||
#if DEBUG
|
||||
try { File.AppendAllText("missing_translations.txt", $"[{Current}] MISSING: {key}\n"); }
|
||||
catch { }
|
||||
#endif
|
||||
// Fallback: roher Key (sichtbar als Warnung im Spiel)
|
||||
return $"[{key}]";
|
||||
}
|
||||
|
||||
static void LoadFile()
|
||||
{
|
||||
_strings = LoadDictionary("en");
|
||||
string langName = Current.ToString().ToLower();
|
||||
|
||||
if (langName == "en") return;
|
||||
|
||||
foreach (var kv in LoadDictionary(langName))
|
||||
_strings[kv.Key] = kv.Value;
|
||||
}
|
||||
|
||||
static Dictionary<string, string> LoadDictionary(string langName)
|
||||
{
|
||||
string? json = LoadEmbedded(langName);
|
||||
|
||||
if (json == null)
|
||||
{
|
||||
string file = Path.Combine(LangFolder, langName + ".json");
|
||||
if (File.Exists(file))
|
||||
try { json = File.ReadAllText(file); } catch { }
|
||||
}
|
||||
|
||||
if (json == null) return new Dictionary<string, string>();
|
||||
|
||||
try
|
||||
{
|
||||
return JsonSerializer.Deserialize<Dictionary<string, string>>(json)
|
||||
?? new Dictionary<string, string>();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new Dictionary<string, string>();
|
||||
}
|
||||
}
|
||||
|
||||
static string? LoadEmbedded(string langName)
|
||||
{
|
||||
// Embedded Resource Name: GladiatorIdle.Languages.{lang}.json
|
||||
string resName = $"GladiatorIdle.Languages.{langName}.json";
|
||||
var asm = Assembly.GetExecutingAssembly();
|
||||
using var stream = asm.GetManifestResourceStream(resName);
|
||||
if (stream == null) return null;
|
||||
using var reader = new StreamReader(stream);
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user