How to override localization strings of depending modules
Source Code
You can find the source of the example solution used in this article here.
Getting Started
This example is based on the following document
https://docs.abp.io/en/abp/latest/Localization#extending-existing-resource
We will change the default DisplayName:Abp.Timing.Timezone
and Description:Abp.Timing.Timezone
of AbpTimingResource
and add localized text in Russian language(ru
).
I created the AbpTiming
folder in the Localization
directory of the ExtendLocalizationResource.Domain.Shared
project.
Create en.json
and ru.json
in its directory.
en.json
{
"culture": "en",
"texts": {
"DisplayName:Abp.Timing.Timezone": "My Time zone",
"Description:Abp.Timing.Timezone": "My Application time zone"
}
}
ru.json
{
"culture": "ru",
"texts": {
"DisplayName:Abp.Timing.Timezone": "Часовой пояс",
"Description:Abp.Timing.Timezone": "Часовой пояс приложения"
}
}
We have below content in ExtendLocalizationResource.Domain.Shared.csproj
file, See Virtual-File-System understand how it works.
<ItemGroup>
<EmbeddedResource Include="Localization\ExtendLocalizationResource\*.json" />
<Content Remove="Localization\ExtendLocalizationResource\*.json" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="5.0.*" />
</ItemGroup>
Change the code of the ConfigureServices
method in ExtendLocalizationResourceDomainSharedModule
.
Configure<AbpLocalizationOptions>(options =>
{
options.Resources
.Add<ExtendLocalizationResourceResource>("en")
.AddBaseTypes(typeof(AbpValidationResource))
.AddVirtualJson("/Localization/ExtendLocalizationResource");
//add following code
options.Resources
.Get<AbpTimingResource>()
.AddVirtualJson("/Localization/AbpTiming");
options.DefaultResourceType = typeof(ExtendLocalizationResourceResource);
});
Execute ExtendLocalizationResource.DbMigrator
to migrate the database and run ExtendLocalizationResource.Web
.
We have changed the English localization text and added Russian localization.
Index page
<p>@AbpTimingResource["DisplayName:Abp.Timing.Timezone"]</p>
@using(CultureHelper.Use("ru"))
{
<p>@AbpTimingResource["DisplayName:Abp.Timing.Timezone"]</p>
}
Comments
Halil İbrahim Kalkan 167 weeks ago
Thanks @maliming for the article :)
Serdar Genc 165 weeks ago
Thank you !
fjinc 79 weeks ago
I encountered the error "Volo.Abp.AbpException: Can not find a resource with given type ..." but solved this by adding the module as a dependency in the DomainSharedModule.
chandraprakash 1 week ago
@maliming Is it possible to override the existing 'LoginIsNotAllowed' key in 'AccountResource'? Can this be implemented in the AuthServer project?
Liming Ma 1 week ago
Of course, You can override the
LoginIsNotAllowed
ofAccountResource
in theAuthServer
project. : )chandraprakash 6 days ago
@maliming Yes, it works, thanks! However, when we call the API using postman "https://localhost:44334/connect/token", the response for a user who is not confirmed and inactive is: "error_description": "You are not allowed to login! Your account is inactive or needs to confirm your email/phone number." This is not the custom "LoginIsNotAllowed": "User email has not been verified." value we are setting. Why is this happening?
Liming Ma 6 days ago
hi
The response text/JSON of OAuth2/Openiddict will always be in English.
See https://stackoverflow.com/a/76483313/6680813
chandraprakash 6 days ago
@maliming Yes, we attempted to set the key in English with LoginIsNotAllowed: User email has not been verified in AuthServer Project. However, it is not working when testing the API endpoint https://localhost:44334/connect/token using Postman. And this English.