ABP开发手记14 - 更新多语言

Posted it-evan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ABP开发手记14 - 更新多语言相关的知识,希望对你有一定的参考价值。

点这里进入ABP开发手记目录 

新增语言

在基础设施层(即JD.CRS.EntityFrameworkCore)的EntityFrameworkCoreSeedHost下打开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

新增资源

在领域层(即JD.CRS.Core)的LocalizationSourceFiles

新增资源

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

引用资源

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

 

以上是关于ABP开发手记14 - 更新多语言的主要内容,如果未能解决你的问题,请参考以下文章

[Abp 源码分析]十多语言(本地化)处理

ABP开发手记13 - 更新菜单

ABP开发手记7 - 基础设施层更新数据库

ABP开发手记15 - 小结

ABP开发手记2 - 体系架构

ABP开发手记1 - 开篇