init lserp cs 5.0
This commit is contained in:
@@ -0,0 +1,171 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Zhaizj.Framework.Web;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 语言包工具,用于加载多国语言。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 默认语言包文件存放在 /framework/lang/ 中,比如 /framework/lang/zh-cn/ 。只要在 /framework/lang/ 中新增一个语言包文件夹,则系统将其作为语言包列表自动加载。可添加的语言包名称包括:en-us,en-gb,zh-cn,zh-tw,ja,ko,fr,de,it
|
||||
/// </remarks>
|
||||
public class lang {
|
||||
|
||||
// 这个一定要放在第一行,以保证第一个加载
|
||||
private static Dictionary<String, Dictionary<String, LanguageSetting>> langLocaleAll = getLangLocale();
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前语言字符(比如 zh-cn,或 en-us)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static String getLangString() {
|
||||
|
||||
String defaultLang = "zh-cn";
|
||||
|
||||
String langCookie = CurrentRequest.getLangCookie();
|
||||
if (strUtil.HasText( langCookie ) && langLocaleAll.ContainsKey( langCookie )) return langCookie;
|
||||
|
||||
if (CurrentRequest.getUserLanguages() == null) return defaultLang;
|
||||
String[] reqLangs = CurrentRequest.getUserLanguages();
|
||||
if (reqLangs.Length == 0) return defaultLang;
|
||||
if (langLocaleAll.ContainsKey( reqLangs[0] )) return reqLangs[0];
|
||||
|
||||
return defaultLang;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取某 key 的语言值
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static String get( String key ) {
|
||||
try {
|
||||
String langStr = getLangString();
|
||||
return getCoreLang( langStr ).getLangMap() [key];
|
||||
|
||||
}
|
||||
catch (KeyNotFoundException) {
|
||||
throw new KeyNotFoundException( "language's key not found: " + key );
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取在 core.config 中定义的核心语言包
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static LanguageSetting getCoreLang() {
|
||||
return getCoreLang( getLangString() );
|
||||
}
|
||||
|
||||
private static LanguageSetting getCoreLang( String langStr ) {
|
||||
Dictionary<String, LanguageSetting> langlist = langLocaleAll[langStr];
|
||||
return langlist["core"];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据类型 t 获取语言列表
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
/// <returns></returns>
|
||||
public static LanguageSetting getByApp( Type t ) {
|
||||
|
||||
Dictionary<String, LanguageSetting> langlist = langLocaleAll[getLangString()];
|
||||
LanguageSetting result;
|
||||
langlist.TryGetValue( t.FullName, out result );
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
private static Dictionary<String, Dictionary<String, LanguageSetting>> getLangLocale() {
|
||||
|
||||
String dirRoot = PathHelper.Map( getLangRootPath() );
|
||||
|
||||
String[] dirPaths = Directory.GetDirectories( dirRoot );
|
||||
|
||||
Dictionary<String, Dictionary<String, LanguageSetting>> results = new Dictionary<String, Dictionary<String, LanguageSetting>>();
|
||||
foreach (String path in dirPaths) {
|
||||
String langName = Path.GetFileName( path );
|
||||
results.Add( langName.ToLower(), getLangList( path ) );
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private static Dictionary<String, LanguageSetting> getLangList( String path ) {
|
||||
|
||||
String[] files = Directory.GetFiles( path );
|
||||
|
||||
Dictionary<String, LanguageSetting> results = new Dictionary<String, LanguageSetting>();
|
||||
|
||||
foreach (String file in files) {
|
||||
|
||||
if (Path.GetExtension( file ) != ".config") continue;
|
||||
|
||||
String fileName = Path.GetFileNameWithoutExtension( file );
|
||||
|
||||
Dictionary<String, String> _lang = cfgHelper.Read( file, '=' );
|
||||
LanguageSetting lbl = new LanguageSetting( fileName, _lang );
|
||||
|
||||
results.Add( fileName, lbl );
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
private static String getLangRootPath() {
|
||||
return cfgHelper.FrameworkRoot + "lang/";
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有支持的语言包
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static List<Dictionary<String, String>> GetSupportedLang() {
|
||||
|
||||
List<Dictionary<String, String>> list = new List<Dictionary<String, String>>();
|
||||
foreach (String key in langLocaleAll.Keys) {
|
||||
Dictionary<String, String> pair = new Dictionary<String, String>();
|
||||
pair.Add( "Name", GetLangInfo( key ) );
|
||||
pair.Add( "Value", key );
|
||||
list.Add( pair );
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private static String GetLangInfo( String langStr ) {
|
||||
if (langMap.ContainsKey( langStr )) return langMap[langStr];
|
||||
return langStr;
|
||||
}
|
||||
|
||||
private static Dictionary<String, String> langMap = getLangInfo();
|
||||
|
||||
private static Dictionary<String, String> getLangInfo() {
|
||||
Dictionary<String, String> map = new Dictionary<String, String>();
|
||||
map.Add( "en-us", "English (US)" );
|
||||
map.Add( "en-gb", "English (British)" );
|
||||
map.Add( "zh-cn", "中文(简体)" ); // skipLang
|
||||
map.Add( "zh-tw", "正體中文(繁體)" ); // skipLang
|
||||
map.Add( "ja", "日本語" );// skipLang
|
||||
map.Add( "ko", "한국어" );
|
||||
map.Add( "fr", "Français" );
|
||||
map.Add( "de", "Deutsch" );
|
||||
map.Add( "it", "Italiano" );
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
|
||||
using System.Web;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 某个语言包配置文件的内容,包括一个名称和一个语言包的 Dictionary
|
||||
/// </summary>
|
||||
public class LanguageSetting {
|
||||
|
||||
private String name;
|
||||
private Dictionary<String, String> langMap;
|
||||
|
||||
public LanguageSetting( String name, Dictionary<String, String> lang ) {
|
||||
this.name = name;
|
||||
this.langMap = lang;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 key 获取语言值
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public String get( String key ) {
|
||||
return langMap[key];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取语言的键值对 Dictionary
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Dictionary<String, String> getLangMap() {
|
||||
return this.langMap;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
|
||||
using System.Web;
|
||||
|
||||
namespace Zhaizj.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// 获取某个 app 的语言包
|
||||
/// </summary>
|
||||
public class alang {
|
||||
|
||||
/// <summary>
|
||||
/// 根据 app 的类型 t 获取某 key 的语言值
|
||||
/// </summary>
|
||||
/// <param name="t">app 的类型</param>
|
||||
/// <param name="key">语言 key</param>
|
||||
/// <returns></returns>
|
||||
public static String get( Type t, String key ) {
|
||||
LanguageSetting ls = lang.getByApp( t );
|
||||
return ls == null ? null : ls.get( key );
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user