点这里进入ABP开发手记目录
设置语种
新增语种
数据库操作
打开多语言表AbpLanguages,添加一条记录.
程序操作
在基础设施层(即JD.CRS.EntityFrameworkCore)的\EntityFrameworkCore\Seed\Host\下打开DefaultLanguagesCreator.cs //用以存放多语言相关信息
添加一行代码.
new ApplicationLanguage(tenantId, "zh-Hant", "繁體中文", "famfamfam-flags hk"), //增加繁体中文
1 private static ListGetInitialLanguages() 2 { 3 var tenantId = CRSConsts.MultiTenancyEnabled ? null : (int?)MultiTenancyConsts.DefaultTenantId; 4 return new List 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 }
删除语种
打开多语言表AbpLanguages,删除一条记录.
设置默认语种
打开默认设置表AbpSettings,更新一条记录.
Abp.Localization.DefaultLanguageName从en(即英文)修改为zh-Hans(即简体中文).
设置资源
新增资源文件
在领域层(即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>
编辑资源文件
在领域层(即JD.CRS.Core)的\Localization\SourceFiles\
编辑资源文件(例如CRS.xml),以下两种方式皆可.
1 xml version="1.0" encoding="utf-8" ?> 2 <localizationDictionary culture="en"> 3 <texts> 4 <text name="AppName" >CRStext> 5 <text name="CRS" value="CRS" /> 6 <text name="StartTyping">Start Typingtext> 7 <text name="Version">Versiontext> 8 <text name="CopyRight" >© 2019 CRStext> 9 <text name="HomePage" value="Home page" /> 10 <text name="Data">Datatext> 11 <text name="Service">Servicetext> 12 <text name="Report">Reporttext> 13 <text name="Setting">Settingtext> 14 <text name="Office">Officetext> 15 <text name="Department">Departmenttext> 16 <text name="Course">Coursetext> 17 <text name="Instructor">Instructortext> 18 <text name="Student">Studenttext> 19 <text name="OfficeInstructor">Office-Allocationtext> 20 <text name="DepartmentInstructor">Department-Instructortext> 21 <text name="DepartmentCourse">Department-Coursetext> 22 <text name="InstructorCourse">Instructor-Coursetext> 23 <text name="StudentCourse">Student-Coursetext> 24 <text name="OfficeReport">Office Reporttext> 25 <text name="DepartmentReport">Department Reporttext> 26 <text name="CourseReport">Course Reporttext> 27 <text name="InstructorReport">Instructor Reporttext> 28 <text name="StudentReport">Student Reporttext> 29 <text name="About" value="About" /> 30 <text name="Skins">Skinstext> 31 <text name="Settings">Settingstext> 32 <text name="WelcomeMessage" value="Welcome to CRS!" /> 33 <text name="FormIsNotValidMessage" value="Form is not valid. Please check and fix errors." /> 34 <text name="TenantNameCanNotBeEmpty" value="Tenant name can not be empty" /> 35 <text name="InvalidUserNameOrPassword" value="Invalid user name or password" /> 36 <text name="ThereIsNoTenantDefinedWithName{0}" value="There is no tenant defined with name {0}" /> 37 <text name="TenantIsNotActive" value="Tenant {0} is not active." /> 38 <text name="UserIsNotActiveAndCanNotLogin" value="User {0} is not active and can not log in." /> 39 <text name="UserEmailIsNotConfirmedAndCanNotLogin">Your email address is not confirmed. You can not login.text> 40 <text name="UserLockedOutMessage">The user account has been locked out. Please try again later.text> 41 <text name="PleaseEnterLoginInformation" value="Please enter login information" /> 42 <text name="TenancyName" value="Tenancy name" /> 43 <text name="UserNameOrEmail" value="User name or email" /> 44 <text name="Password" value="Password" /> 45 <text name="ResetPassword" value="Reset Password" /> 46 <text name="UpdatePassword" value="Update Password" /> 47 <text name="RememberMe" value="Remember me" /> 48 <text name="LogIn" value="Log in" /> 49 <text name="LoginFailed" value="Login failed!" /> 50 <text name="NameSurname" value="Name surname" /> 51 <text name="UserName" value="User name" /> 52 <text name="Name" value="Name" /> 53 <text name="Surname" value="Surname" /> 54 <text name="EmailAddress" value="Email address" /> 55 <text name="Tenants" value="Tenants" /> 56 <text name="SavedSuccessfully" value="Saved successfully" /> 57 <text name="CreateNewTenant" value="Create new tenant" /> 58 <text name="AdminEmailAddress" value="Admin email address" /> 59 <text name="AdminPassword" value="Admin password" /> 60 <text name="Save" value="Save" /> 61 <text name="Cancel" value="Cancel" /> 62 <text name="TenantName_Regex_Description" value="Tenant name must be at least 2 chars, starts with a letter and continue with letter, number, dash or underscore." /> 63 <text name="DefaultPasswordIs" value="Default password is {0}" /> 64 <text name="CanBeEmptyToLoginAsHost" value="Can be empty to login as host." /> 65 <text name="Register" value="Register" /> 66 <text name="OrLoginWith" value="Or login with" /> 67 <text name="WaitingForActivationMessage" value="Your account is waiting to be activated by system admin." /> 68 <text name="TenantSelection" value="Tenant Selection" /> 69 <text name="TenantSelection_Detail" value="Please select one of the following tenants." /> 70 <text name="Logout" value="Logout" /> 71 <text name="RegisterFormUserNameInvalidMessage">Please don't enter an email address for username.text> 72 <text name="DatabaseConnectionString" value="Database connection string" /> 73 <text name="Users" value="Users" /> 74 <text name="IsActive" value="Is active" /> 75 <text name="FullName" value="Full name" /> 76 <text name="CreateNewUser" value="Create new user" /> 77 <text name="Yes" value="Yes" /> 78 <text name="No" value="No" /> 79 <text name="Optional" value="Optional" /> 80 <text name="LeaveEmptyToSwitchToHost">Leave empty to switch to the hosttext> 81 <text name="CurrentTenant">Current tenanttext> 82 <text name="NotSelected">Not selectedtext> 83 <text name="Change">Changetext> 84 <text name="ChangeTenant">Change tenanttext> 85 <text name="MultiLevelMenu">Multi Level Menutext> 86 <text name="Back">Backtext> 87 <text name="SuccessfullyRegistered">Successfully registeredtext> 88 <text name="WaitingForEmailActivation">Your email address should be activatedtext> 89 <text name="Roles">Rolestext> 90 <text name="DisplayName">Display Nametext> 91 <text name="Edit">Edittext> 92 <text name="Delete">Deletetext> 93 <text name="CreateNewRole">Create new roletext> 94 <text name="RoleName">Role Nametext> 95 <text name="Actions">Actionstext> 96 <text name="CouldNotCompleteLoginOperation">Could not complete login operation. Please try again later.text> 97 <text name="CouldNotValidateExternalUser">Could not validate external usertext> 98 <text name="EditRole">Edit roletext> 99 <text name="EditTenant">Edit tenanttext> 100 <text name="EditUser">Edit usertext> 101 <text name="TenantIdIsNotActive{0}">TenantId {0} is not activetext> 102 <text name="UnknownTenantId{0}">Unknown tenantId {0}text> 103 <text name="ThisFieldIsRequired">This field is requiredtext> 104 <text name="PleaseWait">Please wait...text> 105 <text name="Administration">Administrationtext> 106 <text name="ClearAll">Clear alltext> 107 <text name="ClearOthers">Clear otherstext> 108 <text name="LabelOptions">Label optionstext> 109 <text name="Permissions">Permissionstext> 110 <text name="RoleDescription">Role descriptiontext> 111 <text name="Create">Createtext> 112 <text name="UserDetails">User detailstext> 113 <text name="UserRoles">User rolestext> 114 <text name="ConfirmPassword">Confirm passwordtext> 115 <text name="On">Ontext> 116 <text name="Off">Offtext> 117 <text name="AreYouSureWantToDelete">Are you sure want to delete {0}?text> 118 <text name="Filter">Filtertext> 119 <text name="Refresh">Refreshtext> 120 121 <text name="CreateCourse">Create Coursetext> 122 <text name="EditCourse">Edit Coursetext> 123 <text name="Code">Codetext> 124 <text name="DepartmentCode">Department Codetext> 125 <text name="Credits">Creditstext> 126 <text name="Remarks">Remarkstext> 127 <text name="Status">Statustext> 128 texts> 129 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.NameAndSurnameli> 10 <li><span class="text-muted">@L("TenancyName"):span> @Model.TenancyNameli> 11 <li><span class="text-muted">@L("UserName"):span> @Model.UserNameli> 12 <li><span class="text-muted">@L("EmailAddress"):span> @Model.EmailAddressli> 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 }