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

2020-12-13 16:41

阅读:308

标签:图片   iss   return   mode   note   tty   short   case   edm   

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

新增语言

在基础设施层(即JD.CRS.EntityFrameworkCore)的\EntityFrameworkCore\Seed\Host\下打开DefaultLanguagesCreator.cs //用以存放多语言相关信息

新增语言

new ApplicationLanguage(tenantId, "zh-Hant", "繁體中文", "famfamfam-flags hk"), //增加繁体中文

技术图片技术图片
 1 private static List GetInitialLanguages()
 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         }
View Code

新增资源

在领域层(即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

引用资源

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

标签:图片   iss   return   mode   note   tty   short   case   edm   

原文地址:https://www.cnblogs.com/IT-Evan/p/ABP14.html


评论


亲,登录后才可以留言!