ABP入门教程14 - 更新多语言

点这里进入ABP入门教程目录 

设置语种

新增语种

数据库操作

打开多语言表AbpLanguages,添加一条记录.

程序操作

在基础设施层(即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         }
View Code

删除语种

打开多语言表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>
View Code

编辑资源文件

在领域层(即JD.CRS.Core)的\Localization\SourceFiles\

编辑资源文件(例如CRS.xml),以下两种方式皆可.

<text name="UserName" value="User Name" />

<text name="UserName">User Name</text>

  1 <?xml version="1.0" encoding="utf-8" ?>
  2 <localizationDictionary culture="en">
  3   <texts>
  4     <text name="AppName" >CRS</text>
  5     <text name="CRS" value="CRS" />
  6     <text name="StartTyping">Start Typing</text>
  7     <text name="Version">Version</text>
  8     <text name="CopyRight" >© 2019 CRS</text>
  9     <text name="HomePage" value="Home page" />
 10     <text name="Data">Data</text>
 11     <text name="Service">Service</text>
 12     <text name="Report">Report</text>
 13     <text name="Setting">Setting</text>
 14     <text name="Office">Office</text>
 15     <text name="Department">Department</text>
 16     <text name="Course">Course</text>
 17     <text name="Instructor">Instructor</text>
 18     <text name="Student">Student</text>
 19     <text name="OfficeInstructor">Office-Allocation</text>
 20     <text name="DepartmentInstructor">Department-Instructor</text>
 21     <text name="DepartmentCourse">Department-Course</text>
 22     <text name="InstructorCourse">Instructor-Course</text>
 23     <text name="StudentCourse">Student-Course</text>
 24     <text name="OfficeReport">Office Report</text>
 25     <text name="DepartmentReport">Department Report</text>
 26     <text name="CourseReport">Course Report</text>
 27     <text name="InstructorReport">Instructor Report</text>
 28     <text name="StudentReport">Student Report</text>
 29     <text name="About" value="About" />
 30     <text name="Skins">Skins</text>
 31     <text name="Settings">Settings</text>
 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 host</text>
 81     <text name="CurrentTenant">Current tenant</text>
 82     <text name="NotSelected">Not selected</text>
 83     <text name="Change">Change</text>
 84     <text name="ChangeTenant">Change tenant</text>
 85     <text name="MultiLevelMenu">Multi Level Menu</text>
 86     <text name="Back">Back</text>
 87     <text name="SuccessfullyRegistered">Successfully registered</text>
 88     <text name="WaitingForEmailActivation">Your email address should be activated</text>
 89     <text name="Roles">Roles</text>
 90     <text name="DisplayName">Display Name</text>
 91     <text name="Edit">Edit</text>
 92     <text name="Delete">Delete</text>
 93     <text name="CreateNewRole">Create new role</text>
 94     <text name="RoleName">Role Name</text>
 95     <text name="Actions">Actions</text>
 96     <text name="CouldNotCompleteLoginOperation">Could not complete login operation. Please try again later.</text>
 97     <text name="CouldNotValidateExternalUser">Could not validate external user</text>
 98     <text name="EditRole">Edit role</text>
 99     <text name="EditTenant">Edit tenant</text>
100     <text name="EditUser">Edit user</text>
101     <text name="TenantIdIsNotActive{0}">TenantId {0} is not active</text>
102     <text name="UnknownTenantId{0}">Unknown tenantId {0}</text>
103     <text name="ThisFieldIsRequired">This field is required</text>
104     <text name="PleaseWait">Please wait...</text>
105     <text name="Administration">Administration</text>
106     <text name="ClearAll">Clear all</text>
107     <text name="ClearOthers">Clear others</text>
108     <text name="LabelOptions">Label options</text>
109     <text name="Permissions">Permissions</text>
110     <text name="RoleDescription">Role description</text>
111     <text name="Create">Create</text>
112     <text name="UserDetails">User details</text>
113     <text name="UserRoles">User roles</text>
114     <text name="ConfirmPassword">Confirm password</text>
115     <text name="On">On</text>
116     <text name="Off">Off</text>
117     <text name="AreYouSureWantToDelete">Are you sure want to delete {0}?</text>
118     <text name="Filter">Filter</text>
119     <text name="Refresh">Refresh</text>
120     
121     <text name="CreateCourse">Create Course</text>
122     <text name="EditCourse">Edit Course</text>
123     <text name="Code">Code</text>
124     <text name="DepartmentCode">Department Code</text>
125     <text name="Credits">Credits</text>
126     <text name="Remarks">Remarks</text>
127     <text name="Status">Status</text>
128   </texts>
129 </localizationDictionary>
View Code

引用资源

在需要引用多语言资源的地方用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>
View Code

后端示例

 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 }
View Code

 

posted @ 2019-10-03 23:53  智慧园区-老朱  阅读(2031)  评论(4编辑  收藏  举报