大好的时光,本来要出去玩玩的,出门不远遇车祸了,还好我没事,心里不爽,还是回家写代码的好。实现了一个多语言的接口,记录一下.
主要思想:
以前看过类似的东东,有些是将信息存储在xml中的,我今天做的这个是把信息存储在txt文件中,本文中称其为语言文件。
- 将不同页面需要显示的文字分别存入不同的txt文件。比如,用户登录页面是一个文件。用户信息一个文件。
- 有几种语言要求将上面建立的文件copy几封。并且文件名称以对应语言的简写结尾。
- 将所有建立的语言文件放置在同一个文件下面,
说的有点不清楚,截图:
- 语言文件以"key:value"的形式来存储对于语言需要显示的字,同一个key对应相应的语言。
截图:
- 程序会按照运行时的语言环境动态的去判断语言,然后读取到对应的值
void SetLanguage()
{
Language languageObj = LanguageManager.RetrieveLanguageObj( " userinfo " , currentLanguage);
userName = languageObj.RetrieveLanguage( " userName " );
}
{
Language languageObj = LanguageManager.RetrieveLanguageObj( " userinfo " , currentLanguage);
userName = languageObj.RetrieveLanguage( " userName " );
}
代码贴出来:
代码
namespace Alex.Common.Utilty.MultiLanguage
{
public class LanguageManager
{
private static string languagePath;
private static Hashtable languageFiles;
private static ArrayList languageDetials;
static LanguageManager()
{
if ( string .IsNullOrEmpty(languagePath))
{
InitializeLanguagePath();
}
if (languageFiles == null )
{
LoadLanguage();
}
if (languageDetials == null )
{
LoadLanguage();
}
}
public static Language RetrieveLanguageObj( string languageFileID, string currentLanguage)
{
string languageFileName = string .Format( " {0}_{1} " , languageFileID, currentLanguage);
object languageIndex = languageFiles[languageFileName];
Language language = new Language();
if (languageIndex != null )
{
language = (Language)languageDetials[Convert.ToInt32(languageIndex)];
}
return language;
}
private static void LoadLanguage()
{
languageFiles = new Hashtable();
languageDetials = new ArrayList();
string strFileLine = string .Empty;
string [] allFiles = Directory.GetFiles(languagePath, " *.txt " );
foreach ( string singleFile in allFiles)
{
Language language = new Language();
StreamReader streamReader = new StreamReader(singleFile);
while ( ! streamReader.EndOfStream)
{
strFileLine = streamReader.ReadLine();
if (strFileLine.IndexOf( " : " ) > 0 )
{
string [] languageArray = strFileLine.Split( ' : ' );
if ( ! language.Contains(languageArray[ 0 ]))
{
try
{
language.Add(languageArray[ 0 ], languageArray[ 1 ]);
}
catch (Exception ex)
{
string errMsg = ex.Message;
// here:write log
}
}
}
}
streamReader.Close();
string key = singleFile.Substring(singleFile.LastIndexOf( ' \\ ' ) + 1 , (singleFile.LastIndexOf( ' . ' ) - singleFile.LastIndexOf( ' \\ ' )) - 1 );
languageFiles.Add(key,languageDetials.Add(language));
}
}
private static void InitializeLanguagePath()
{
NameValueCollection appSettings = (NameValueCollection)HttpContext.Current.GetSection( " appSettings " );
languagePath = HttpContext.Current.Server.MapPath(appSettings[ " LanguagePath " ]);
}
public static void Refresh()
{
languagePath = string .Empty;
languageFiles = null ;
languageDetials = null ;
LoadLanguage();
}
}
}
{
public class LanguageManager
{
private static string languagePath;
private static Hashtable languageFiles;
private static ArrayList languageDetials;
static LanguageManager()
{
if ( string .IsNullOrEmpty(languagePath))
{
InitializeLanguagePath();
}
if (languageFiles == null )
{
LoadLanguage();
}
if (languageDetials == null )
{
LoadLanguage();
}
}
public static Language RetrieveLanguageObj( string languageFileID, string currentLanguage)
{
string languageFileName = string .Format( " {0}_{1} " , languageFileID, currentLanguage);
object languageIndex = languageFiles[languageFileName];
Language language = new Language();
if (languageIndex != null )
{
language = (Language)languageDetials[Convert.ToInt32(languageIndex)];
}
return language;
}
private static void LoadLanguage()
{
languageFiles = new Hashtable();
languageDetials = new ArrayList();
string strFileLine = string .Empty;
string [] allFiles = Directory.GetFiles(languagePath, " *.txt " );
foreach ( string singleFile in allFiles)
{
Language language = new Language();
StreamReader streamReader = new StreamReader(singleFile);
while ( ! streamReader.EndOfStream)
{
strFileLine = streamReader.ReadLine();
if (strFileLine.IndexOf( " : " ) > 0 )
{
string [] languageArray = strFileLine.Split( ' : ' );
if ( ! language.Contains(languageArray[ 0 ]))
{
try
{
language.Add(languageArray[ 0 ], languageArray[ 1 ]);
}
catch (Exception ex)
{
string errMsg = ex.Message;
// here:write log
}
}
}
}
streamReader.Close();
string key = singleFile.Substring(singleFile.LastIndexOf( ' \\ ' ) + 1 , (singleFile.LastIndexOf( ' . ' ) - singleFile.LastIndexOf( ' \\ ' )) - 1 );
languageFiles.Add(key,languageDetials.Add(language));
}
}
private static void InitializeLanguagePath()
{
NameValueCollection appSettings = (NameValueCollection)HttpContext.Current.GetSection( " appSettings " );
languagePath = HttpContext.Current.Server.MapPath(appSettings[ " LanguagePath " ]);
}
public static void Refresh()
{
languagePath = string .Empty;
languageFiles = null ;
languageDetials = null ;
LoadLanguage();
}
}
}
代码
namespace Alex.Common.Utilty.MultiLanguage
{
public class Language:Hashtable
{
public string RetrieveLanguage( string languageID)
{
object langObj = base [languageID];
if (langObj == null )
{
return string .Empty;
}
return langObj.ToString();
}
}
}
{
public class Language:Hashtable
{
public string RetrieveLanguage( string languageID)
{
object langObj = base [languageID];
if (langObj == null )
{
return string .Empty;
}
return langObj.ToString();
}
}
}
代码写完以后才发现实现了单件模式。因为静态变量是整个应用程序的跟啊。语言文件在第一次请求的时候会加入hashtable中,以后访问就无需再去读取文件,从而提高了性能。(第一次初始化以后,多次使用)
md,今天表达能力超差。