Multi Language Örnegi

CorsaiR

Emektar
27 Ara 2005
1,228
18
Çekirdekten
Multi Language Örnegi Formlarin yada cesitli componentlerin üzerindeki yazilarinizi degisik dilerde, calisirken nasil degistirilir konusunda bir örnekLanguageFile.xml
---------------------

<?xml version="1.0" encoding="utf-8" ?>
<LanguageFile>
<Region name="Main">
<Text key="CloseButton">
<D>Schließen</D>
<TR>Kapat</TR>
</Text>
<Text key="InfoButton">
<D>Info</D>
<TR>Bilgi</TR>
</Text>
<Text key="LanguageButton">
<D>Ändern</D>
<TR>Degistir</TR>
</Text>
<Text key="FormCaption">
<D>Deutsche Texte</D>
<TR>Türkce yazilar</TR>
</Text>
<Text key="lblOutput">
<D>Ein beispiel, wie XML Daten gelesen werden können</D>
<TR>Bir XML´inin icerigi nasil okunulduguna dahil bir örnek</TR>
</Text>
</Region>
<Region name="Info">
<Text key="CloseButton">
<D>Schließen</D>
<TR>Kapat</TR>
</Text>
<Text key="label1">
<D>XML BEISPIEL SPRACHUMSCHALTUNG</D>
<TR>XML ÖRNEGI DIL DEGISTIRME</TR>
</Text>
<Text key="label2">
<D>DEUTSCH _ TÜRKISCH</D>
<TR>ALMANCA - TÜRKCE</TR>
</Text>
<Text key="FormCaption">
<D>Info Maske</D>
<TR>Bilgi Penceresi</TR>
</Text>
</Region>
</LanguageFile>


cXMLString.cs
----------------

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Xml;

namespace LanguageSwitch
{
/// <summary>
/// Summary description for cXMLString.
/// </summary>
public class cXMLString
{
#region Variables

private Hashtable regions=new Hashtable();

// Konstantlar
const string CONFIG_DEFAULTLANGUAGE = "DefaultLanguage";
const string CONFIG_FILENAME = "XmlFile";
const string CACHE_KEY = "LanguageFile";
const string XML_ROOTNODE = "LanguageFile/Region";

#endregion

#region Property: DefaultLanguage

/// <summary>
/// Standarddil (Config-File´dan)
/// </summary>
public static string DefaultLanguage
{
get { return ConfigurationSettings.AppSettings[CONFIG_DEFAULTLANGUAGE]; }
}

#endregion

#region Property: SourceFile

/// <summary>
/// Xml-File´in bulundugu Path Dosya (Config-File´dan)
/// </summary>
public static string SourceFile
{
get { return ConfigurationSettings.AppSettings[CONFIG_FILENAME]; }
}

#endregion

#region Constructor
public cXMLString()
{
//
// TODO: Add constructor logic here
//
}
#endregion

#region Methods: GetInstance (Singleton-Pattern)

/// <summary>
/// cXMLString´ten (Singleton) yeni bir Instance veriyor
/// </summary>
/// <returns></returns>
public static cXMLString GetInstance()
{
// Yeni bir cXMLString-Instance olustur ve string´leri yükle
cXMLString st = new cXMLString();
st.LoadStrings();

return (cXMLString)st;
}
#endregion

#region Methods: LoadSourceFile

/// <summary>
/// XML-Source-File´ini bir Xml********-Object´te yüklüyor
/// </summary>
/// <param name="file">Source-File´in bulunmus oldugu dosya</param>
/// <returns>Xml********</returns>
private Xml******** LoadSourceFile(string file)
{
// Load the xml-file in a xml-********
Xml******** xDoc = new Xml********();

try
{
xDoc.Load(file);
}
catch (System.IO.FileNotFoundException)
{
throw new Exception("Xml-File "+file+" bulunamadi");
}
catch (System.Exception ex)
{
throw new Exception(file+" okurken bir hata olustu "+": " + ex.Message);
}

return xDoc;
}

#endregion

#region Methods: LoadStrings

/// <summary>
/// XML-File´inden String´leri dictionary´lerle bir data model´e yüklüyor
/// </summary>
///
public **** LoadStrings()
{
// Regions-Hashtable bosalt
regions.Clear();

// XML-File yükle
Xml******** xDoc = LoadSourceFile(SourceFile);

// XML icerigini oku
try
{
foreach(XmlNode regionNode in xDoc.SelectNodes(XML_ROOTNODE))
{
Hashtable strs = new Hashtable();

foreach(XmlNode textNode in regionNode.ChildNodes)
{

StringDictionary texts = new StringDictionary();

foreach(XmlNode txt in textNode.ChildNodes)
texts.Add(txt.Name, txt.InnerText);

strs.Add(textNode.Attributes["key"].Value, texts);
}

this.regions.Add(regionNode.Attributes["name"].Value, strs);
}
}
catch (System.Exception ex)
{
throw new Exception("Fehler beim einlesen des Xml-Files"+SourceFile+": " + ex.Message);
}
}

#endregion

#region Methods: GetString

/// <summary>
/// Key´e baglantili secilen dilin String´ini veriyor
/// </summary>
/// <param name="region">Aranan Region´un ismi</param>
/// <param name="key">String-verisinin Key´si</param>
/// <param name="language">DIL</param>
/// <returns>String</returns>
public string GetString(string region, string key, string language)
{
string text = null;

// Verilen parameterlere (region, key, language) Kelimeyi oku
try
{
// Region´un olup olmadigini arastir
if(regions.Count > 0)
{
// Aranan Region´un olup olmadigini arastir
if(regions[region] != null)
{
// Aranan dilin mevcut olup olmadigini arastir;
// aksi taktirde Default Language kulanilir
if( ((StringDictionary)((Hashtable)regions[region])[key]).ContainsKey(language.ToString()) )
text = ((StringDictionary)((Hashtable)regions[region])[key])[language];
else
text = ((StringDictionary)((Hashtable)regions[region])[key])[DefaultLanguage];

return text;

}
else

// Region mevcut degil
throw new Exception(string.Format(
"Region {0} ist nicht vorhanden", region));
}
else
return "";
}
// Hata bölümü
catch (System.Exception ex)
{
throw new Exception("Folgende Parameter ergaben kein Ergebnis im aktuellen Objekt:" +
"Region: " + region + " " +
"Key: " + key + " " +
"Language: " + language +
"Fehlermeldung: " + ex.Message);
}

}

public string GetString(string region, string key)
{
return GetString(region, key, DefaultLanguage);
}

#endregion
}
}

frmMain.cs
-------------

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace LanguageSwitch
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class frmMain : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblOutput;
private System.Windows.Forms.Button CommitButton;
private System.Windows.Forms.ComboBox cmbLanguage;
private System.Windows.Forms.Button LanguageButton;
private System.Windows.Forms.Button InfoButton;
private string _LanguageChar;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

private **** FillLanguages()
{
cmbLanguage.ResetText();
cmbLanguage.Items.Add("German");
cmbLanguage.Items.Add("Türkish");
}

public string LanguageChar
{
get {return _LanguageChar;}
set{_LanguageChar=value;}
}


public frmMain()
{
InitializeComponent();

lblOutput.ResetText();
lblOutput.Text = cXMLString.GetInstance().GetString("Main", "lblOutput", "D");

CommitButton.ResetText();
CommitButton.Text = cXMLString.GetInstance().GetString("Main", "CloseButton", "D");

InfoButton.ResetText();
InfoButton.Text = cXMLString.GetInstance().GetString("Main", "InfoButton", "D");

LanguageButton.ResetText();
LanguageButton.Text = cXMLString.GetInstance().GetString("Main", "LanguageButton", "D");

this.ResetText();
this.Text = cXMLString.GetInstance().GetString("Main", "FormCaption", "D");

_LanguageChar = "D";

FillLanguages();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override **** Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private **** InitializeComponent()
{
this.lblOutput = new System.Windows.Forms.Label();
this.CommitButton = new System.Windows.Forms.Button();
this.InfoButton = new System.Windows.Forms.Button();
this.cmbLanguage = new System.Windows.Forms.ComboBox();
this.LanguageButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblOutput
//
this.lblOutput.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblOutput.******** = new System.Drawing.Point(16, 24);
this.lblOutput.Name = "lblOutput";
this.lblOutput.Size = new System.Drawing.Size(496, 40);
this.lblOutput.TabIndex = 0;
this.lblOutput.Text = "label1";
//
// CommitButton
//
this.CommitButton.******** = new System.Drawing.Point(160, 80);
this.CommitButton.Name = "CommitButton";
this.CommitButton.Size = new System.Drawing.Size(120, 32);
this.CommitButton.TabIndex = 1;
this.CommitButton.Text = "CommitButton";
this.CommitButton.Click += new System.EventHandler(this.CommitButton_Click);
//
// InfoButton
//
this.InfoButton.******** = new System.Drawing.Point(288, 80);
this.InfoButton.Name = "InfoButton";
this.InfoButton.Size = new System.Drawing.Size(120, 32);
this.InfoButton.TabIndex = 2;
this.InfoButton.Text = "Info";
this.InfoButton.Click += new System.EventHandler(this.DeleteButton_Click);
//
// cmbLanguage
//
this.cmbLanguage.******** = new System.Drawing.Point(432, 80);
this.cmbLanguage.Name = "cmbLanguage";
this.cmbLanguage.Size = new System.Drawing.Size(104, 21);
this.cmbLanguage.TabIndex = 3;
this.cmbLanguage.Text = "comboBox1";
//
// LanguageButton
//
this.LanguageButton.******** = new System.Drawing.Point(32, 80);
this.LanguageButton.Name = "LanguageButton";
this.LanguageButton.Size = new System.Drawing.Size(120, 32);
this.LanguageButton.TabIndex = 4;
this.LanguageButton.Text = "LanguageButton";
this.LanguageButton.Click += new System.EventHandler(this.LanguageButton_Click);
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(568, 158);
this.Controls.Add(this.LanguageButton);
this.Controls.Add(this.cmbLanguage);
this.Controls.Add(this.InfoButton);
this.Controls.Add(this.CommitButton);
this.Controls.Add(this.lblOutput);
this.Name = "frmMain";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static **** Main()
{
Application.Run(new frmMain());
}

private **** LanguageButton_Click(object sender, System.EventArgs e)
{
if (cmbLanguage.SelectedIndex >= 0 && cmbLanguage.Text != string.Empty)
{
if (cmbLanguage.SelectedIndex==0)
{
lblOutput.ResetText();
lblOutput.Text = cXMLString.GetInstance().GetString("Main", "lblOutput", "D");

CommitButton.ResetText();
CommitButton.Text = cXMLString.GetInstance().GetString("Main", "CloseButton", "D");

InfoButton.ResetText();
InfoButton.Text = cXMLString.GetInstance().GetString("Main", "InfoButton", "D");

LanguageButton.ResetText();
LanguageButton.Text = cXMLString.GetInstance().GetString("Main", "LanguageButton", "D");

this.ResetText();
this.Text = cXMLString.GetInstance().GetString("Main", "FormCaption", "D");

_LanguageChar = "D";
}
else
{
lblOutput.ResetText();
lblOutput.Text = cXMLString.GetInstance().GetString("Main", "lblOutput", "TR");

CommitButton.ResetText();
CommitButton.Text = cXMLString.GetInstance().GetString("Main", "CloseButton", "TR");

InfoButton.ResetText();
InfoButton.Text = cXMLString.GetInstance().GetString("Main", "InfoButton", "TR");

LanguageButton.ResetText();
LanguageButton.Text = cXMLString.GetInstance().GetString("Main", "LanguageButton", "TR");

this.ResetText();
this.Text = cXMLString.GetInstance().GetString("Main", "FormCaption", "TR");

_LanguageChar = "TR";
}
}
else
MessageBox.Show("Please select a Language");
}

private **** DeleteButton_Click(object sender, System.EventArgs e)
{
frmInfo fInfo = new frmInfo(_LanguageChar);
fInfo.Show();
}

private **** CommitButton_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
}
}
 
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.