using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using Gtk;
public class Language
{
private const string EngCssPath = "lang_eng.css";
private const string JpnCssPath = "lang_jpn.css";
private const string EngCsvPath = "info_eng.csv";
private const string JpnCsvPath = "info_jpn.csv";
private const string TouchCssPath = "touch_friendly.css"; // NEW
private const string NamesCsvPath = "names_eng.csv"; // Japanese case/category name -> English
private readonly Dictionary<string, string> _engText;
private readonly Dictionary<string, string> _jpnText;
private Dictionary<string, string> _text;
private readonly Dictionary<string, string> _names;
private static readonly System.Text.RegularExpressions.Regex EnJpRx =
new System.Text.RegularExpressions.Regex(@"^(.*?)\((.*)\)\s*$");
// widgets whose label is a plain CSV lookup
private readonly Dictionary<string, Widget> _widgets = new Dictionary<string, Widget>();
public readonly CssProvider _languageCssProvider = new CssProvider();
private readonly CssProvider _touchCssProvider = new CssProvider(); // NEW
public bool IsEnglish { get; private set; }
/// <summary>Raised after every language change, so callers can rebuild state-dependent text.</summary>
public event EventHandler Changed;
public Language(bool isEnglish)
{
// Touch-friendly sizing loads once, at a lower priority than the
// language CSS (600) so per-language #id rules can still override
// fonts/colors if needed, while touch sizing stays constant.
if (File.Exists(TouchCssPath))
{
_touchCssProvider.LoadFromPath(TouchCssPath);
Console.WriteLine("Touch CSS loaded from: " + System.IO.Path.GetFullPath(TouchCssPath));
}
else
{
Console.WriteLine("Warning: CSS file not found: " + TouchCssPath);
}
StyleContext.AddProviderForScreen(Gdk.Screen.Default, _touchCssProvider, 1000);
StyleContext.AddProviderForScreen(Gdk.Screen.Default, _languageCssProvider, 600);
_engText = LoadText(EngCsvPath);
_jpnText = LoadText(JpnCsvPath);
_names = LoadText(NamesCsvPath);
// set before any widget is built, so ctor-time lookups are already correct
IsEnglish = isEnglish;
_text = isEnglish ? _engText : _jpnText;
}
// ---------- registration ----------
/// <summary>Label comes straight from the CSV key. Applied automatically on every change.</summary>
public void Register(string key, Widget widget)
{
if (widget == null) return;
widget.Name = key; // enables #Key selectors in the lang CSS
_widgets[key] = widget;
// Set the current text now, so widgets registered after the first
// Apply() (e.g. SoundWindow, built later) don't wait for a toggle.
string value;
if (_text.TryGetValue(key, out value)) SetText(widget, value);
}
/// <summary>Widget whose text the owner sets itself (state-dependent). Name only, no auto-apply.</summary>
public void RegisterDynamic(string key, Widget widget)
{
if (widget != null) widget.Name = key;
}
// ---------- lookup ----------
public string this[string key] { get { return Get(key); } }
public string Get(string key)
{
string value;
if (_text.TryGetValue(key, out value)) return value;
Console.WriteLine("Warning: missing text key '" + key + "'");
return key;
}
/// <summary>
/// Display text for a case-tree name. The Japanese CSV value stays the lookup key;
/// this only changes what is shown. "fine crackle(捻髪音)" -> "fine crackle" in English.
/// </summary>
public string Name(string jp)
{
if (string.IsNullOrEmpty(jp) || !IsEnglish) return jp;
string en;
if (_names.TryGetValue(jp.Trim(), out en)) return en;
var m = EnJpRx.Match(jp);
if (m.Success && m.Groups[1].Value.Trim().Length > 0) return m.Groups[1].Value.Trim();
return jp;
}
/// <summary>Adds Japanese -> English display names (e.g. from cases_old_eng.csv).</summary>
public void AddNames(Dictionary<string, string> map)
{
foreach (var kv in map) _names[kv.Key] = kv.Value;
}
/// <summary>Display text for a whole path, e.g. "Breath sounds : Wheeze : Right lung".</summary>
public string Path(string[] parts, string sep)
{
var shown = new string[parts.Length];
for (int i = 0; i < parts.Length; i++) shown[i] = Name(parts[i]);
return string.Join(sep, shown);
}
public string Format(string key, params object[] args)
{
string template = Get(key);
try
{
return string.Format(CultureInfo.InvariantCulture, template, args);
}
catch (FormatException)
{
Console.WriteLine("Warning: bad placeholder in text key '" + key + "'");
return template;
}
}
// ---------- apply ----------
public void Apply(bool isEnglish)
{
IsEnglish = isEnglish;
_text = isEnglish ? _engText : _jpnText;
foreach (var kvp in _widgets)
{
string value;
if (_text.TryGetValue(kvp.Key, out value))
SetText(kvp.Value, value);
else
Console.WriteLine("Warning: missing text key '" + kvp.Key + "'");
}
string cssPath = isEnglish ? EngCssPath : JpnCssPath;
if (File.Exists(cssPath))
_languageCssProvider.LoadFromPath(cssPath);
else
Console.WriteLine("Warning: CSS file not found: " + cssPath);
EventHandler handler = Changed;
if (handler != null) handler(this, EventArgs.Empty);
}
private static void SetText(Widget widget, string value)
{
Button button = widget as Button; // also covers ToggleButton
if (button != null) { button.Label = value; return; }
Label label = widget as Label;
if (label != null) { label.Text = value; return; }
Console.WriteLine("Warning: don't know how to set text on " + widget.GetType().Name);
}
// ---------- csv ----------
private Dictionary<string, string> LoadText(string path)
{
var result = new Dictionary<string, string>();
// Prefer the copy next to the exe (same rule as cases*.csv), so the
// app finds its text no matter which folder it was launched from.
string besideExe = SoundWindow.AppFile(path);
if (File.Exists(besideExe)) path = besideExe;
if (!File.Exists(path))
{
Console.WriteLine("Warning: text file not found: " + path);
return result;
}
string[] lines = File.ReadAllLines(path);
for (int i = 1; i < lines.Length; i++) // row 0 is the header
{
string line = lines[i].Trim();
if (string.IsNullOrEmpty(line) || line.StartsWith("#")) continue;
string[] parts = line.Split(new[] { ',' }, 2);
if (parts.Length < 2) continue;
// Value keeps any commas. "\n" in the CSV becomes a real line break,
// so a translator controls where a message wraps instead of leaving
// it to Pango, which breaks Japanese at arbitrary characters.
result[parts[0].Trim()] = parts[1].Replace("\\n", "\n");
}
return result;
}
}