Jak mam udelany preklad klasickych Windows aplikaci

Zakladem jsou 5 trid – RL, Translation, AppLangConverter, AppLang a AppLangHelper a vyctu Langs. Podle kodu poznate, ze jazyku program muze mit maximalne 10(jedna se o jedine cislo, tedy 0-9). Takze nyni vam tu naserviruji vsechny tyto tridy, vycty a jak se to pouziva v praxi:
RL:

using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
/// 
/// Staticka trida, do ktere se ulozi pod jednotlive klice cesky text i anglicky preklad
/// Ma metodu pro vraceni textu na zaklade daneho jazyku.
/// 
public static class RL
{
    public static Langs l = Langs.en;
    static Dictionary en = new Dictionary();
    static Dictionary cs = new Dictionary();
    public  static void Initialize(Dictionary trans)
    {
        foreach (var item in trans)
        {
            en.Add(item.Key, item.Value.En);
            cs.Add(item.Key, item.Value.Cs);
        }
    }
    public static string GetString(string k)
    {
        if (l == Langs.cs)
        {
            return cs[k];
        }
        return en[k];
    }
}

Translation:

/// 
/// Datova trida, obsahujici pouze cesky text a jeho odpovidajici anglicky preklad
/// 
public class Translation
{
    public string En = null;
    public string Cs = null;
    public Translation(string en, string cs)
    {
        En = en;
        Cs = cs;
    }
}

AppLangConverter:

using swf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
    /// 
    /// Staticka  prevodni trida, prevadejici vsechny data z tridy AppLang do retezce a naopak
    /// 
    public static class AppLangConverter
    {
        public static AppLang ConvertTo(string b)
        {
            byte type = 0;
            if (!byte.TryParse(b[0].ToString(), out type))
            {
                return new AppLang(1, 0);
            }
            byte lang = 0;
            if (!byte.TryParse(b[1].ToString(), out lang))
            {
                return new AppLang(1, 0);
            }
            return new AppLang(type, lang);
        }
        public static string ConvertFrom(AppLang t)
        {
            return t.Type.ToString() + t.Language.ToString();
        }
    }

AppLang:

/// 
/// AppLang je pouze datova trida, ktera obsahuje dvou ciferne cislo. Pokud je prvni cislo 0, druhe cislo oznacuje ID jazyku, ktery bude fixne nastaveny. Pokud je 1. cislo 1, bude se jazyk nastavovat podle systemu.  
/// 
public class AppLang
{
    byte language = 0;
    byte type = 0;
    /// 
    /// Pokud se jedna o systemovy jazyk, z hodnoty 0 se jazyk bere z CurrentUICulture, kdezto u hodnoty 1 z CurrentCulture
    /// 
    public byte Language
    {
        get
        {
            return language;
        }
    }
    /// 
    /// Typ jazyka zda fixni nebo systemovy
    ///
    /// 
    public byte Type
    {
        get
        {
            return type;
        }
    }
    /// 
    /// Je zde vykon na 1. miste, proto tato trida nema zadnou metodu Parse a zadny bezparametrovy konstruktor.
    /// 
    /// 
    /// 
    public AppLang(byte type, byte language)
    {
        this.type = type;
        this.language = language;
    }
    public override string ToString()
    {
        return AppLangHelper.ToString(this);
    }
}

AppLangHelper:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Controls;
public static class AppLangHelper
{
    public static CultureInfo currentCulture = null;
    public static CultureInfo currentUICulture = null;
    /// 
    /// Jazyky ktere si muze zvolit sam uzivatel
    /// V klici je zkratka jazyku, v hodnote pak jeji plny nazev
    /// 
    static Dictionary fixedLanguages = new Dictionary();
    /// 
    /// Texty, ktere jazyky
    /// V klici je dvou znakovy nazev jazyku, v hodnote pak texty "Podle nastaveneho jazyka OS" a "Depending on the OS language"
    /// 
    static Dictionary> systemLanguages = new Dictionary>();
    /// 
    /// V klici je dvouznakovy nazev jazyku, v hodnote cislo tohoto jazyku, ktere je v tride AppLang
    /// 
    static Dictionary languageCodes = new Dictionary();
    /// 
    /// Cislo 0
    /// 
    const byte fixedC = 0;
    /// 
    /// Cislo 1
    /// 
    const byte systemC = 1;
    const byte dependingOnLanguage = 0;
    //const byte dependingOnCountry = 1;
    /// 
    /// Text Podle nastaveneho jazyka OS
    /// Ackoliv ma v nazvu 0, pouziva se kdyz typ jazyku neni 0
    /// 
    const string cs0 = "Podle nastaveneho jazyka OS";
    //const string cs1 = "Podle nastaveneho regionu";
    /// 
    /// Text Depending on the OS language
    /// Ackoliv ma v nazvu 0, pouziva se kdyz typ jazyku neni 0
    /// 
    const string en0 = "Depending on the OS language";
    //const string en1 = "Depending on the country";
    public static AppLang selectedInCB = null;
    static AppLangHelper()
    {
        fixedLanguages.Add("cs", "Cestina");
        fixedLanguages.Add("en", "English");
        List systemLanguageCS = new List();
        systemLanguageCS.Add(cs0);
        //systemLanguageCS.Add(cs1);
        List systemLanguageEN = new List();
        systemLanguageEN.Add(en0);
        //systemLanguageEN.Add(en1);
        systemLanguages.Add("cs", systemLanguageCS);
        systemLanguages.Add("en", systemLanguageEN);
    }
    /// 
    /// Vrati nazev jazyku napriklad do ComboBoxu na zmenu jazyka
    /// Vraci pokud A1.TYpe neni 0 spravny text podle jazyka OS
    /// 
    /// 
    /// 
    /// 
    public static string ToString(AppLang actual)
    {
        string vr = "";
        //Langs l = Langs.cs;
        if (actual.Type == fixedC)
        {
            //l = (Langs)actual.Language;
            vr = fixedLanguages[((Langs)actual.Language).ToString()];
        }
        else
        {
            CultureInfo depending = null;
            if (actual.Language == dependingOnLanguage)
            {
                depending = currentUICulture;
            }
            else
            {
                depending = currentCulture;
            }
            //depending = CultureInfo.CurrentCulture;
            if (depending == null)
            {
                if (actual.Language == dependingOnLanguage)
                {
                    depending = CultureInfo.CurrentUICulture;
                }
                else
                {
                    depending = CultureInfo.CurrentCulture;
                }
            }
            if (depending.TwoLetterISOLanguageName == "cs")
            {
                //l = Enum.Parse(typeof(Langs), de
                //int i = 0;
                //foreach (var item in systemLanguages["cs"])
                //{
                //-OK, aktualni jazyk je podle CultureInfo.CurrentUICulture.TwoLetterISOLanguageName
                if (actual.Language == 0)
                {
                    vr = cs0 + " - " + fixedLanguages[CultureInfo.CurrentUICulture.TwoLetterISOLanguageName];
                }
                //else if (actual.Language == 1)
                //{
                //    vr = cs1 + " - " + fixedLanguages[CultureInfo.CurrentCulture.TwoLetterISOLanguageName];
                //}
                //i++;
                //}
            }
            else //if (depending.TwoLetterISOLanguageName == "en")
            {
                if (actual.Language == 0)
                {
                    vr = en0 + " - " + fixedLanguages[CultureInfo.CurrentUICulture.TwoLetterISOLanguageName];
                }
                //else if (actual.Language == 1)
                //{
                //    vr = en1 + " - " + fixedLanguages[CultureInfo.CurrentCulture.TwoLetterISOLanguageName];
                //}
            }
        }
        return vr;
    }
    /// 
    /// Metoda ktera mi vrati jazyk ve kterem se ma obsah zobrazit
    /// 
    /// 
    /// 
    public static Langs GetLang(string b)
    {
        Langs vr = Langs.cs;
        AppLang actual = AppLangConverter.ConvertTo(b);
        if (actual.Type == fixedC)
        {
            vr = (Langs)actual.Language;
        }
        else
        {
            //CultureInfo depending = CultureInfo.CurrentCulture;
            //if (depending.TwoLetterISOLanguageName == "cs")
            //{
            //l = Enum.Parse(typeof(Langs), de
            //int i = 0;
            //foreach (var item in systemLanguages["cs"])
            //{
            //-OK, aktualni jazyk je podle CultureInfo.CurrentUICulture.TwoLetterISOLanguageName
            if (actual.Language == 0)
            {
                vr = GetLang2(CultureInfo.CurrentUICulture.TwoLetterISOLanguageName);
            }
            else if (actual.Language == 1)
            {
                vr = GetLang2(CultureInfo.CurrentCulture.TwoLetterISOLanguageName);
            }
            //i++;
            //}
            //}
            //else if (depending.TwoLetterISOLanguageName == "en")
            //{
            //    if (actual.Language == 0)
            //    {
            //        vr = en0 + " - " + fixedLanguages[CultureInfo.CurrentUICulture.TwoLetterISOLanguageName];
            //    }
            //    else if (actual.Language == 1)
            //    {
            //        vr = en1 + " - " + fixedLanguages[CultureInfo.CurrentCulture.TwoLetterISOLanguageName];
            //    }
            //}
        }
        return vr;
    }
    /// 
    /// Pouziva se kdyz chci vratit jazyk ve vyctu Langs - A1 musi byt obsah vyctu Langs, tedy cs nebo en
    /// 2 ma v nazvu proto ze stejna metoda jiz existuje, ale ta mi vrati jazyk i podle OS
    /// 
    /// 
    /// 
    private static Langs GetLang2(string p)
    {
        Langs vr = Langs.cs;
        if (Enum.TryParse(p, out vr))
        {
            return vr;
        }
        return Langs.en;
    }
    /// 
    /// Vrati CultureInfo dane zeme bez specifikace jazyka pro jazyk A1
    /// 
    /// 
    /// 
    public static CultureInfo GetCi(Langs p)
    {
        CultureInfo ci = null;
        if (p == Langs.cs)
        {
            ci = new CultureInfo("cs");
        }
        else
        {
            ci = new CultureInfo("en");
        }
        return ci;
    }
    /// 
    /// Vrati mi vycet Langs, tedy jazyk na zaklade A1
    /// 
    /// 
    /// 
    public static Langs GetLang(CultureInfo cultureInfo)
    {
        if (cultureInfo.TwoLetterISOLanguageName == "cs")
        {
            return Langs.cs;
        }
        else
        {
            return Langs.en;
        }
    }
    /// 
    /// Prida polozky do A1 - nejdrive fixni jazyky a jak podle OS v jazyku OS
    /// Pokud vystup metody AppLangConverter.ConvertFrom aktualni iterace rovna A2, polozka se zvoli.
    /// 
    /// 
    /// 
    public static void AddItemsToComboBox(ComboBox cb, string settingsAl)
    {
        selectedInCB = null;
        byte i = 0;
        foreach (var item in fixedLanguages)
        {
            AppLang al = new AppLang(fixedC, i);
            cb.Items.Add(al);
            if (selectedInCB == null)
            {
                if (AppLangConverter.ConvertFrom(al) == settingsAl)
                {
                    selectedInCB = al;
                }
            }
            i++;
        }
        if (CultureInfo.CurrentUICulture.TwoLetterISOLanguageName == "cs")
        {
            i = 0;
            foreach (var item in systemLanguages["cs"])
            {
                AppLang al = new AppLang(systemC, i);
                cb.Items.Add(al);
                if (selectedInCB == null)
                {
                    if (AppLangConverter.ConvertFrom(al) == settingsAl)
                    {
                        selectedInCB = al;
                    }
                }
                i++;
            }
        }
        else
        {
            i = 0;
            foreach (var item in systemLanguages["en"])
            {
                AppLang al = new AppLang(systemC, i);
                cb.Items.Add(al);
                if (selectedInCB == null)
                {
                    if (AppLangConverter.ConvertFrom(al) == settingsAl)
                    {
                        selectedInCB = al;
                    }
                }
                i++;
            }
        }
        cb.SelectedItem = selectedInCB;
        //-To by se snad nikdy nemelo stat
        //if (selectedInCB == null)
        //{
        //}
    }
}

Langs:

public enum Langs : byte
{
    cs = 0,
    en = 1
}

Jedinecne pro kazdou aplikaci:

//-Vytvorim slovnik s klicy stejnymi jako polozky kterymi se budou naplnovat a dale anglicky text a cesky preklad
Dictionary<string, Translation> trans = new Dictionary<string, Translation>();
trans.Add("btnAddFolder.Content", new Translation("Add folders", "Pridat slozky"));
//-
RL.Initialize(trans);
//-Zde muzete take fixne urcit nejaky jazyk nebo pomoci CultureInfo tridy nastavit jazyk podle systemu
//-Ja zde nacitam jazyk ze souboru, program si jej pamatuje
RL.l = AppLangHelper.GetLang(SunamoAlarmSettings.Language);
//-Ulozim si vycet Langs z RL.l do lokalni promenne pro snazsi pristup k ni
l = RL.l;
//-Takhle uz pak jen vsude nastavite lingualne spravny text
btnAddFolder.Content = RL.GetString("btnAddFolder.Content");

Leave a Reply