Newer
Older
EARS-LINUX / program / Language.cs
using System;
using System.Collections.Generic;
using System.IO;
using Gtk;

public class Language
{
    public static bool LanguageSwitchValue = false;

    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 readonly Dictionary<string, string> _engText;
    private readonly Dictionary<string, string> _jpnText;
    private readonly Dictionary<string, Button> _buttonsById;
    public readonly CssProvider _languageCssProvider = new CssProvider();
    private readonly Label _languageLabel;

     public Language(Dictionary<string, Button> buttonsById, Label languageLabel)
    {
        _buttonsById = buttonsById;
        _languageLabel = languageLabel;
        StyleContext.AddProviderForScreen(Gdk.Screen.Default, _languageCssProvider, 600);


        _engText = LoadButtonText(EngCsvPath);
        _jpnText = LoadButtonText(JpnCsvPath);
    }

    private Dictionary<string, string> LoadButtonText(string path)
    {
        var result = new Dictionary<string, string>();
        if (!File.Exists(path))
        {
            Console.WriteLine($"Warning: text file not found: {path}");
            return result;
        }

        var lines = File.ReadAllLines(path);
        for (int i = 1; i < lines.Length; i++)
        {
            var line = lines[i].Trim();
            if (string.IsNullOrEmpty(line)) continue;
            var parts = line.Split(new[] { ',' }, 2);
            if (parts.Length < 2) continue;
            result[parts[0]] = parts[1];
        }
        return result;
    }

    public void ApplyLanguage(bool isEnglish)
    {
        var text = isEnglish ? _engText : _jpnText;
        string cssPath = isEnglish ? EngCssPath : JpnCssPath;

        foreach (var kvp in _buttonsById)
            if (text.TryGetValue(kvp.Key, out var buttonText))
                kvp.Value.Label = buttonText;

        if (File.Exists(cssPath))
            _languageCssProvider.LoadFromPath(cssPath);
        else
            Console.WriteLine($"Warning: CSS file not found: {cssPath}");

        _languageLabel.Text = isEnglish ? "あ" : "A";
    }

    //for others
     public Language(Dictionary<string, Button> buttonsById)
    {
        _buttonsById = buttonsById;
        StyleContext.AddProviderForScreen(Gdk.Screen.Default, _languageCssProvider, 600);


        _engText = LoadButtonText(EngCsvPath);
        _jpnText = LoadButtonText(JpnCsvPath);
    }

    
    public void ApplyLanguages(bool isEnglish)
    {
        var text = isEnglish ? _engText : _jpnText;
        string cssPath = isEnglish ? EngCssPath : JpnCssPath;

        foreach (var kvp in _buttonsById)
            if (text.TryGetValue(kvp.Key, out var buttonText))
                kvp.Value.Label = buttonText;

        if (File.Exists(cssPath))
            _languageCssProvider.LoadFromPath(cssPath);
        else
            Console.WriteLine($"Warning: CSS file not found: {cssPath}");

    }
}