forked from potatosalad775/MWC_Localization_Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagazineTextHandler.cs
More file actions
134 lines (116 loc) · 4.87 KB
/
MagazineTextHandler.cs
File metadata and controls
134 lines (116 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// 'Classified Magazine' Text Handler
using MSCLoader;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
namespace MWC_Localization_Core
{
/// <summary>
/// Handles Yellow Pages magazine text translation
/// Manages comma-separated word lists and price/phone line formatting
/// </summary>
public class MagazineTextHandler
{
private Dictionary<string, string> magazineTranslations = new Dictionary<string, string>();
/// <summary>
/// Load magazine-specific translations from separate file
/// </summary>
public void LoadMagazineTranslations(string translationPath)
{
magazineTranslations = TranslationFileParser.ParseKeyValueFile(translationPath, normalizeKeys: true);
CoreConsole.Print($"[MagazineTextHandler] {magazineTranslations.Count} traduções de revistas carregadas");
}
/// <summary>
/// Get total count of loaded magazine translations
/// </summary>
public int GetTranslationCount()
{
return magazineTranslations.Count;
}
/// <summary>
/// Check if path is a magazine text element
/// </summary>
public bool IsMagazineText(string path)
{
return path.Contains("Sheets/YellowPagesMagazine/Page") && path.EndsWith("/Lines/YellowLine");
}
/// <summary>
/// Handle magazine text translation
/// </summary>
public bool HandleMagazineText(TextMesh textMesh)
{
if (textMesh == null || string.IsNullOrEmpty(textMesh.text))
return false;
// Skip placeholder text immediately - return false to prevent monitoring registration
// Placeholder text isn't ready yet, will be filled with real content later
if (textMesh.text == "88888888888888888888888888888")
return false;
// Handling price and phone number line (format: "h.149,- puh.123456")
if (textMesh.text.StartsWith("h.") && textMesh.text.Contains(",- puh."))
{
TranslatePricePhoneLine(textMesh);
return true; // Handled
}
// Try updating rest of the magazine lines in standard way
string key = MLCUtils.FormatUpperKey(textMesh.text);
if (magazineTranslations.TryGetValue(key, out string translation))
{
textMesh.text = translation;
return true; // Handled
}
// No translation found - mark as handled to prevent infinite retries
//CoreConsole.Warning($"[MagazineTextHandler] No translation found for: '{textMesh.text}' (key: '{key}')");
return true; // Handled (even though not translated)
}
/// <summary>
/// Translate price and phone number line (e.g., "h.149,- puh.123456" -> "149 MK, PHONE - 123456")
/// Uses PHONE key from translate_magazine.txt for the phone label
/// </summary>
private void TranslatePricePhoneLine(TextMesh textMesh)
{
try
{
// Remove "h." prefix and split by ",- puh."
string withoutPrefix = textMesh.text.Substring(2);
string[] parts = withoutPrefix.Split(new string[] { ",- puh." }, System.StringSplitOptions.None);
if (parts.Length == 2)
{
string pricePart = parts[0].Trim();
string phonePart = parts[1].Trim();
// Get phone label from translations (default to "PHONE" if not found)
string phoneLabel = magazineTranslations.TryGetValue("PHONE", out string translation)
? translation
: "PHONE";
textMesh.text = $"{pricePart} MK, {phoneLabel} - {phonePart}";
}
}
catch (System.Exception ex)
{
CoreConsole.Warning($"[MagazineTextHandler] Falha ao analisar linha de preço/telefone da revista: {textMesh.text} - {ex.Message}");
}
}
/// <summary>
/// Clear all magazine translations
/// </summary>
public void ClearTranslations()
{
magazineTranslations.Clear();
}
/// <summary>
/// Get translation for a magazine text (case-insensitive lookup)
/// Returns null if no translation found
/// </summary>
public string GetTranslation(string original)
{
if (string.IsNullOrEmpty(original))
return null;
string normalizedKey = MLCUtils.FormatUpperKey(original);
if (magazineTranslations.TryGetValue(normalizedKey, out string translation))
{
return translation;
}
return null;
}
}
}