2014-10-24 17:02:38 +00:00
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using System.Windows.Forms ;
namespace MSGer.tk
{
public class Language
{ //2014.04.19.
public static Dictionary < string , Language > UsedLangs = new Dictionary < string , Language > ( ) ;
public Dictionary < string , string > Strings = new Dictionary < string , string > ( ) ;
private Language ( string lang )
{
UsedLangs . Add ( lang , this ) ;
}
public Language ( ) //2014.09.06.
{
if ( ! Directory . Exists ( "languages" ) )
Directory . CreateDirectory ( "languages" ) ;
string [ ] files = Directory . GetFiles ( "languages" ) ;
if ( files . Length = = 0 )
{
MessageBox . Show ( "Error: No languages found." ) ;
return ; //Még nem jelentkezett be, ezért ki fog lépni
}
for ( int x = 0 ; x < files . Length ; x + + )
{
string [ ] lines = File . ReadAllLines ( files [ x ] ) ;
var dict = lines . Select ( l = > l . Split ( '=' ) ) . ToDictionary ( a = > a [ 0 ] , a = > a [ 1 ] ) ;
new Language ( new FileInfo ( files [ x ] ) . Name . Split ( '.' ) [ 0 ] ) . Strings = dict ; //(FileInfo: 2014.09.01.) - Eltárol egy új nyelvet, majd a szövegeket hozzátársítja
}
2014-10-24 17:48:59 +00:00
/ * if ( Language . FromString ( Storage . Settings [ "lang" ] ) = = null )
2014-10-24 17:02:38 +00:00
{
MessageBox . Show ( "Error: Could not find language: " + Storage . Settings [ "lang" ] ) ;
return ;
2014-10-24 17:48:59 +00:00
} * /
2014-10-24 17:02:38 +00:00
CurrentUser . Language = Language . FromString ( Storage . Settings [ "lang" ] ) ;
if ( CurrentUser . Language = = null )
{
if ( Language . UsedLangs . ContainsKey ( "en" ) )
{
MessageBox . Show ( "Error: The specified language (" + Storage . Settings [ "lang" ] + ") is not found.\nThe program will use english that you can change later." ) ;
Storage . Settings [ "lang" ] = "en" ;
}
else
{
MessageBox . Show ( "Error: The specified language (" + Storage . Settings [ "lang" ] + "), nor enlish are found.\nPlease download translations." ) ;
return ;
}
}
}
public override string ToString ( )
{
return UsedLangs . FirstOrDefault ( x = > x . Value = = this ) . Key ;
}
public static Language FromString ( string value )
{
Language tmp = null ;
UsedLangs . TryGetValue ( value , out tmp ) ;
return tmp ;
}
public static Language GetCuurentLanguage ( )
{
return Language . FromString ( Storage . Settings [ "lang" ] ) ;
}
public static string Translate ( string id )
{ //2014.08.19.
Language lang = GetCuurentLanguage ( ) ;
if ( lang . Strings . ContainsKey ( id ) )
return lang . Strings [ id ] ;
else
{
MessageBox . Show ( "Translation string not found: " + id + "\nIn file: " + lang + ".txt" ) ;
return "Str not found" ;
}
}
}
}