ABP开发手记14 - 更新多语言
程序员文章站
2023-10-27 09:06:10
点这里进入ABP开发手记目录 新增语言 在基础设施层(即JD.CRS.EntityFrameworkCore)的\EntityFrameworkCore\Seed\Host\下打开DefaultLanguagesCreator.cs //用以存放多语言相关信息 新增语言 new Applicatio ......
新增语言
在基础设施层(即jd.crs.entityframeworkcore)的\entityframeworkcore\seed\host\下打开defaultlanguagescreator.cs //用以存放多语言相关信息
新增语言
new applicationlanguage(tenantid, "zh-hant", "繁體中文", "famfamfam-flags hk"), //增加繁体中文
1 private static list<applicationlanguage> getinitiallanguages() 2 { 3 var tenantid = crsconsts.multitenancyenabled ? null : (int?)multitenancyconsts.defaulttenantid; 4 return new list<applicationlanguage> 5 { 6 new applicationlanguage(tenantid, "en", "english", "famfamfam-flags gb"), 7 new applicationlanguage(tenantid, "ar", "العربية", "famfamfam-flags sa"), 8 new applicationlanguage(tenantid, "de", "german", "famfamfam-flags de"), 9 new applicationlanguage(tenantid, "it", "italiano", "famfamfam-flags it"), 10 new applicationlanguage(tenantid, "fr", "français", "famfamfam-flags fr"), 11 new applicationlanguage(tenantid, "pt-br", "português", "famfamfam-flags br"), 12 new applicationlanguage(tenantid, "tr", "türkçe", "famfamfam-flags tr"), 13 new applicationlanguage(tenantid, "ru", "русский", "famfamfam-flags ru"), 14 new applicationlanguage(tenantid, "zh-hans", "简体中文", "famfamfam-flags cn"), 15 new applicationlanguage(tenantid, "zh-hant", "繁體中文", "famfamfam-flags hk"), 16 new applicationlanguage(tenantid, "es-mx", "español méxico", "famfamfam-flags mx"), 17 new applicationlanguage(tenantid, "nl", "nederlands", "famfamfam-flags nl"), 18 new applicationlanguage(tenantid, "ja", "日本語", "famfamfam-flags jp") 19 }; 20 }
新增资源
在领域层(即jd.crs.core)的\localization\sourcefiles\
新增资源
crs-zh-hk.xml //用以存放繁体中文资源
1 <?xml version="1.0" encoding="utf-8" ?> 2 <localizationdictionary culture="zh-hant"> 3 <texts> 4 <text name="appname" >排班系統</text> 5 <text name="crs" value="排班系統" /> 6 <text name="starttyping">開始輸入</text> 7 <text name="version">版本</text> 8 <text name="copyright" >© 2019 排班系統</text> 9 <text name="homepage" value="主頁" /> 10 <text name="data">基礎資料</text> 11 <text name="service">綜合服務</text> 12 <text name="report">管理報表</text> 13 <text name="setting">系統設置</text> 14 <text name="office">辦公室資訊</text> 15 <text name="department">院系</text> 16 <text name="course">課程</text> 17 <text name="instructor">教職員</text> 18 <text name="student">學生</text> 19 <text name="officeinstructor">教職員辦公室分配</text> 20 <text name="departmentinstructor">院系主任設置</text> 21 <text name="departmentcourse">院系課程設置</text> 22 <text name="instructorcourse">教職員課程分配</text> 23 <text name="studentcourse">學生選課</text> 24 <text name="officereport">辦公室報表</text> 25 <text name="departmentreport">院系報表</text> 26 <text name="coursereport">課程報表</text> 27 <text name="instructorreport">教職員報表</text> 28 <text name="studentreport">學生報表</text> 29 <text name="about" value="關於" /> 30 <text name="skins">皮膚</text> 31 <text name="settings">設置</text> 32 <text name="welcomemessage" value="歡迎使用 crs!" /> 33 <text name="formisnotvalidmessage" value="部分輸入資訊不符合要求,請檢查並改正.." /> 34 <text name="tenantnamecannotbeempty" value="租戶名不能為空" /> 35 <text name="invalidusernameorpassword" value="用戶名或密碼無效" /> 36 <text name="thereisnotenantdefinedwithname{0}" value="租戶 {0}不存在" /> 37 <text name="tenantisnotactive" value="租戶 {0} 未啟動." /> 38 <text name="userisnotactiveandcannotlogin" value="用戶 {0} 未啟動,不能登錄." /> 39 <text name="pleaseenterlogininformation" value="請輸入登錄資訊" /> 40 <text name="tenancyname" value="租戶標識" /> 41 <text name="usernameoremail" value="用戶名或郵箱地址" /> 42 <text name="password" value="密碼" /> 43 <text name="rememberme" value="記住我" /> 44 <text name="forgetpassword" value="忘記密碼"/> 45 <text name="notselected" value="未選"/> 46 <text name="changetenant" value="更改租戶"/> 47 <text name="leaveemptytoswitchtohost" value="留空以切換到host"/> 48 <text name="login" value="登錄" /> 49 <text name="loginfailed" value="登錄失敗!" /> 50 <text name="usernameplaceholder" >請輸入帳戶</text> 51 <text name="passwordplaceholder" >請輸入密碼</text> 52 <text name="loginprompt" >正在登陸,請稍候!</text> 53 <text name="userprofile" >使用者資料</text> 54 <text name="users" >用戶</text> 55 <text name="roles" >角色</text> 56 <text name="tenants" >租戶</text> 57 <text name="logout" >註銷</text> 58 <text name="managemenu" >菜單</text> 59 <text name="labeloptions" >頁簽操作</text> 60 <text name="clearall" >關閉所有</text> 61 <text name="clearothers" >關閉其他</text> 62 <text name="create" >創建</text> 63 <text name="add" >添加</text> 64 <text name="edit" >編輯</text> 65 <text name="delete">刪除</text> 66 <text name="find" >查找</text> 67 <text name="creationtime">創建時間</text> 68 <text name="actions">操作</text> 69 <text name="keyword">關鍵字</text> 70 <text name="nodatas">沒有結果</text> 71 <text name="select">請選擇</text> 72 <text name="selectdate">請選擇</text> 73 <text name="tips">提示</text> 74 <text name="deleteconfirm">確定刪除?</text> 75 <text name="title" >標題</text> 76 <text name="content" >內容</text> 77 <text name="changepassword" >修改密碼</text> 78 <text name="passwordcomplexitynotsatisfied">密碼複雜度要求不符.</text> 79 <text name="passwordrequiredigit">密碼至少需要一位元是0到9的數位.</text> 80 <text name="passwordrequirelowercase">密碼至少需要一位元是a到z的小寫字母.</text> 81 <text name="passwordrequirenonalphanumeric">密碼至少需要包含一個特殊字元(非字母或數位的字元).</text> 82 <text name="passwordrequireuppercase">密碼至少需要一位元是a到z的大寫字母.</text> 83 <text name="passwordtooshort">密碼長度太短</text> 84 <text name="username">用戶名</text> 85 <text name="name">名稱</text> 86 <text name="isactive">是否啟用</text> 87 <text name="lastlogintime">最近登陸時間</text> 88 <text name="rolename">角色名</text> 89 <text name="displayname">顯示名</text> 90 <text name="description">描述</text> 91 <text name="isstatic">是否內置</text> 92 93 <text name="all">全部</text> 94 <text name="actived">啟用</text> 95 <text name="noactive">未啟用</text> 96 97 <text name="yes">是</text> 98 <text name="no">否</text> 99 100 <text name="cancel">取消</text> 101 <text name="ok">確定</text> 102 <text name="createnewrole">創建新角色</text> 103 <text name="roledetails">角色詳情</text> 104 <text name="rolepermission">角色許可權</text> 105 <text name="editrole">編輯角色</text> 106 <text name="deleterolesconfirm">確認刪除該角色?</text> 107 108 <text name="createnewuser">創建新用戶</text> 109 <text name="userdetails">用戶詳情</text> 110 <text name="userroles">用戶角色</text> 111 <text name="confirmpassword">確認密碼</text> 112 <text name="emailaddress">郵箱地址</text> 113 <text name="surname">姓</text> 114 <text name="deleteuserconfirm">確認刪除該用戶?</text> 115 <text name="edituser">編輯用戶</text> 116 117 <text name="createnewtenant">創建新租戶</text> 118 <text name="databaseconnectionstring">資料庫連接</text> 119 <text name="adminemailaddress">管理員郵箱地址</text> 120 <text name="defaultpasswordis">預設密碼為:{0}</text> 121 <text name="deletetenantconfirm">確認刪除該租戶?</text> 122 <text name="edittenant">編輯租戶</text> 123 124 125 </texts> 126 </localizationdictionary>
引用资源
在需要引用多语言资源的地方用l("key")的方式即可.
前端示例
1 @model jd.crs.web.models.account.registerresultviewmodel 2 @{ 3 viewbag.title = l("successfullyregistered"); 4 } 5 <div class="card"> 6 <div class="body"> 7 <h4>@l("successfullyregistered")</h4> 8 <ul> 9 <li><span class="text-muted">@l("namesurname"):</span> @model.nameandsurname</li> 10 <li><span class="text-muted">@l("tenancyname"):</span> @model.tenancyname</li> 11 <li><span class="text-muted">@l("username"):</span> @model.username</li> 12 <li><span class="text-muted">@l("emailaddress"):</span> @model.emailaddress</li> 13 </ul> 14 <div> 15 @if (!model.isactive) 16 { 17 <div class="alert alert-warning" role="alert"> 18 @l("waitingforactivationmessage") 19 </div> 20 } 21 22 @if (model.isemailconfirmationrequiredforlogin && !model.isemailconfirmed) 23 { 24 <div class="alert alert-warning" role="alert"> 25 @l("waitingforemailactivation") 26 </div> 27 } 28 </div> 29 </div> 30 </div>
后端示例
1 using system; 2 using abp; 3 using abp.authorization; 4 using abp.dependency; 5 using abp.ui; 6 7 namespace jd.crs.authorization 8 { 9 public class abploginresulttypehelper : abpservicebase, itransientdependency 10 { 11 public abploginresulttypehelper() 12 { 13 localizationsourcename = crsconsts.localizationsourcename; 14 } 15 16 public exception createexceptionforfailedloginattempt(abploginresulttype result, string usernameoremailaddress, string tenancyname) 17 { 18 switch (result) 19 { 20 case abploginresulttype.success: 21 return new exception("don't call this method with a success result!"); 22 case abploginresulttype.invalidusernameoremailaddress: 23 case abploginresulttype.invalidpassword: 24 return new userfriendlyexception(l("loginfailed"), l("invalidusernameorpassword")); 25 case abploginresulttype.invalidtenancyname: 26 return new userfriendlyexception(l("loginfailed"), l("thereisnotenantdefinedwithname{0}", tenancyname)); 27 case abploginresulttype.tenantisnotactive: 28 return new userfriendlyexception(l("loginfailed"), l("tenantisnotactive", tenancyname)); 29 case abploginresulttype.userisnotactive: 30 return new userfriendlyexception(l("loginfailed"), l("userisnotactiveandcannotlogin", usernameoremailaddress)); 31 case abploginresulttype.useremailisnotconfirmed: 32 return new userfriendlyexception(l("loginfailed"), l("useremailisnotconfirmedandcannotlogin")); 33 case abploginresulttype.lockedout: 34 return new userfriendlyexception(l("loginfailed"), l("userlockedoutmessage")); 35 default: // can not fall to default actually. but other result types can be added in the future and we may forget to handle it 36 logger.warn("unhandled login fail reason: " + result); 37 return new userfriendlyexception(l("loginfailed")); 38 } 39 } 40 41 public string createlocalizedmessageforfailedloginattempt(abploginresulttype result, string usernameoremailaddress, string tenancyname) 42 { 43 switch (result) 44 { 45 case abploginresulttype.success: 46 throw new exception("don't call this method with a success result!"); 47 case abploginresulttype.invalidusernameoremailaddress: 48 case abploginresulttype.invalidpassword: 49 return l("invalidusernameorpassword"); 50 case abploginresulttype.invalidtenancyname: 51 return l("thereisnotenantdefinedwithname{0}", tenancyname); 52 case abploginresulttype.tenantisnotactive: 53 return l("tenantisnotactive", tenancyname); 54 case abploginresulttype.userisnotactive: 55 return l("userisnotactiveandcannotlogin", usernameoremailaddress); 56 case abploginresulttype.useremailisnotconfirmed: 57 return l("useremailisnotconfirmedandcannotlogin"); 58 default: // can not fall to default actually. but other result types can be added in the future and we may forget to handle it 59 logger.warn("unhandled login fail reason: " + result); 60 return l("loginfailed"); 61 } 62 } 63 } 64 }