Browse Source
Merge pull request #9552 from abpframework/maliming/DataAnnotationAutoLocalization
Add the class name as property localization prefix.
pull/9587/head
Halil İbrahim Kalkan
5 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with
60 additions and
9 deletions
-
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpDataAnnotationAutoLocalizationMetadataDetailsProvider.cs
-
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/App/LocalizationTestController.cs
-
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/App/Views/LocalizationTest/PersonForm.cshtml
-
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Localization/MvcLocalization_Tests.cs
-
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Localization/Resource/en.json
-
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Localization/Resource/tr.json
|
|
|
@ -54,7 +54,29 @@ namespace Volo.Abp.AspNetCore.Mvc |
|
|
|
|
|
|
|
displayMetadata.DisplayName = () => |
|
|
|
{ |
|
|
|
var localizedString = localizer[PropertyLocalizationKeyPrefix + context.Key.Name]; |
|
|
|
/* |
|
|
|
* DisplayName:ClassName:PropertyName |
|
|
|
* DisplayName:PropertyName |
|
|
|
* ClassName:PropertyName |
|
|
|
* PropertyName |
|
|
|
*/ |
|
|
|
|
|
|
|
LocalizedString localizedString = null; |
|
|
|
|
|
|
|
if (context.Key.ContainerType != null) |
|
|
|
{ |
|
|
|
localizedString = localizer[PropertyLocalizationKeyPrefix + context.Key.ContainerType.Name + ":" + context.Key.Name]; |
|
|
|
} |
|
|
|
|
|
|
|
if (localizedString == null || localizedString.ResourceNotFound) |
|
|
|
{ |
|
|
|
localizedString = localizer[PropertyLocalizationKeyPrefix + context.Key.Name]; |
|
|
|
} |
|
|
|
|
|
|
|
if (localizedString.ResourceNotFound && context.Key.ContainerType != null) |
|
|
|
{ |
|
|
|
localizedString = localizer[context.Key.ContainerType.Name + ":" + context.Key.Name]; |
|
|
|
} |
|
|
|
|
|
|
|
if (localizedString.ResourceNotFound) |
|
|
|
{ |
|
|
|
@ -65,4 +87,4 @@ namespace Volo.Abp.AspNetCore.Mvc |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -21,12 +21,20 @@ namespace Volo.Abp.AspNetCore.App |
|
|
|
|
|
|
|
public class PersonModel |
|
|
|
{ |
|
|
|
//[Display(Name = nameof(BirthDate))]
|
|
|
|
public string BirthDate { get; set; } |
|
|
|
|
|
|
|
public string BirthDate1 { get; set; } |
|
|
|
|
|
|
|
public string BirthDate2 { get; set; } |
|
|
|
|
|
|
|
public string BirthDate3 { get; set; } |
|
|
|
|
|
|
|
public PersonModel() |
|
|
|
{ |
|
|
|
BirthDate = DateTime.Now.ToString("yyyy-MM-dd"); |
|
|
|
BirthDate1 = BirthDate; |
|
|
|
BirthDate2 = BirthDate; |
|
|
|
BirthDate3 = BirthDate; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -5,4 +5,13 @@ |
|
|
|
<form> |
|
|
|
<label asp-for="BirthDate"></label> |
|
|
|
<input asp-for="BirthDate" /> |
|
|
|
</form> |
|
|
|
|
|
|
|
<label asp-for="BirthDate1"></label> |
|
|
|
<input asp-for="BirthDate1" /> |
|
|
|
|
|
|
|
<label asp-for="BirthDate2"></label> |
|
|
|
<input asp-for="BirthDate2" /> |
|
|
|
|
|
|
|
<label asp-for="BirthDate3"></label> |
|
|
|
<input asp-for="BirthDate3" /> |
|
|
|
</form> |
|
|
|
|
|
|
|
@ -30,7 +30,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Localization |
|
|
|
var result = await GetResponseAsStringAsync("/LocalizationTest/HelloJohn"); |
|
|
|
result.ShouldBe("Hello <b>John</b>."); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task Should_Localize_Display_Attribute() |
|
|
|
{ |
|
|
|
@ -38,12 +38,18 @@ namespace Volo.Abp.AspNetCore.Mvc.Localization |
|
|
|
{ |
|
|
|
var result = await GetResponseAsStringAsync("/LocalizationTest/PersonForm"); |
|
|
|
result.ShouldContain("<label for=\"BirthDate\">Birth date</label>"); |
|
|
|
result.ShouldContain("<label for=\"BirthDate1\">Birth date1</label>"); |
|
|
|
result.ShouldContain("<label for=\"BirthDate2\">Birth date2</label>"); |
|
|
|
result.ShouldContain("<label for=\"BirthDate3\">Birth date3</label>"); |
|
|
|
} |
|
|
|
|
|
|
|
using (CultureHelper.Use("tr")) |
|
|
|
{ |
|
|
|
var result = await GetResponseAsStringAsync("/LocalizationTest/PersonForm"); |
|
|
|
result.ShouldContain("<label for=\"BirthDate\">Dogum gunu</label>"); |
|
|
|
result.ShouldContain("<label for=\"BirthDate1\">Dogum gunu1</label>"); |
|
|
|
result.ShouldContain("<label for=\"BirthDate2\">Dogum gunu2</label>"); |
|
|
|
result.ShouldContain("<label for=\"BirthDate3\">Dogum gunu3</label>"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -1,7 +1,10 @@ |
|
|
|
{ |
|
|
|
"culture": "en", |
|
|
|
"texts": { |
|
|
|
"DisplayName:PersonModel:BirthDate1": "Birth date1", |
|
|
|
"DisplayName:BirthDate2": "Birth date2", |
|
|
|
"PersonModel:BirthDate3": "Birth date3", |
|
|
|
"BirthDate": "Birth date", |
|
|
|
"Value1": "Value One" |
|
|
|
"Value1": "Value One" |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -1,7 +1,10 @@ |
|
|
|
{ |
|
|
|
"culture": "tr", |
|
|
|
"texts": { |
|
|
|
"DisplayName:PersonModel:BirthDate1": "Dogum gunu1", |
|
|
|
"DisplayName:BirthDate2": "Dogum gunu2", |
|
|
|
"PersonModel:BirthDate3": "Dogum gunu3", |
|
|
|
"BirthDate": "Dogum gunu", |
|
|
|
"Value1": "Değer Bir" |
|
|
|
"Value1": "Değer Bir" |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|