Localization doesn't work properly for extended properties in Angular
Is there an existing issue for this?
- [x] I have searched the existing issues
Description
While extending an existing entity from pre-built modules, (e.g. Role in IdentityManager), even when a proper DisplayName parameter is added with a valid LocalizationResource, the localization of the new property always defaults to the default LocalizationResource instead of the one specified in the DisplayName parameter.
Reproduction Steps
- Create a new module named
MyModule - Add a new property to an existing entity from pre-built modules in and add a
DisplayNamevalue as specified in the example:
In MyModule.Domain.Shared/MyModuleExtensionConfigurator.cs:
public static void ConfigureExtraProperties()
{
OneTimeRunner.Run(() =>
{
ObjectExtensionManager.Instance.Modules()
.ConfigureIdentity(identity =>
{
identity.ConfigureUser(user =>
{
user.AddOrUpdateProperty<string>( //property type: string
"SocialSecurityNumber", //property name
property =>
{
//localization
property.DisplayName =
LocalizableString.Create<MyProjectNameResource>(
"UserSocialSecurityNumberDisplayName"
);
}
);
});
});
});
}
- Add a localization string in
MyProject.Demo.Shared/Localization/MyProjectNameResource/en.json:
"UserSocialSecurityNumberDisplayName": "Social Security Number"
- Add this
MyModuleto an application and run the application and launch Angular application. - Navigate to
identity/rolespath and try adding a new role or edit an existing role
Expected behavior
The label in the form should show "Social Security Number"
Actual behavior
The label shows "UserSocialSecurityNumberDisplayName"
Regression?
No
Known Workarounds
None
Version
8.3.4
User Interface
Angular
Database Provider
EF Core (Default)
Tiered or separate authentication server
None (Default)
Operation System
Windows (Default)
Other information
Issue also present in latest version.
I have identified the root cause of the issue and am raising a PR for the fix.
Hello @chandralk
I cannot produce the same problem on my end. Can you share the response for such a request /api/abp/application-localization ?
Hi @sumeyyeKurtulus, Sorry for the delay. I'll share a minimally reproducible example today.
Hi @sumeyyeKurtulus , please find attached a minimally reproducible example. Acme.BookStore.zip
Hello,
Thank you for sharing the sample. I checked the application and found that it defines the extra property in two different places.
The first one is the extension provided by the template:
// aspnet-core/Acme.BookStore/BookStoreModuleExtensionConfigurator.cs
// ...
private static void ConfigureExtraProperties()
{
ObjectExtensionManager.Instance.Modules()
.ConfigureIdentity(identity =>
{
identity.ConfigureUser(user =>
{
user.AddOrUpdateProperty<string>( //property type: string
"SocialSecurityNumber", //property name
property =>
{
//validation rules
property.Attributes.Add(new RequiredAttribute());
property.Attributes.Add(new StringLengthAttribute(64) {MinimumLength = 4});
//...other configurations for this property
property.DisplayName = LocalizableString.Create<BookStoreResource>("SocialSecurityNumber");
}
);
});
});
}
The second one is defined in your module:
// aspnet-core/modules/Acme.IdentityExtensions/IdentityExtensionsModuleExtensionConfigurator.cs
// ...
private static void ConfigureExtraProperties()
{
ObjectExtensionManager.Instance.Modules()
.ConfigureIdentity(identity =>
{
identity.ConfigureUser(user =>
{
user.AddOrUpdateProperty<string>( //property type: string
"SocialSecurityNumber", //property name
property =>
{
//validation rules
property.Attributes.Add(new RequiredAttribute());
property.Attributes.Add(new StringLengthAttribute(64) { MinimumLength = 4 });
//...other configurations for this property
property.DisplayName = LocalizableString.Create<IdentityExtensionsResource>("SocialSecurityNumber");
}
);
});
});
}
The issue is that extension localization currently supports only the application's default localization resource, which is BookStoreResource in your sample. You can find more details in this comment from a similar issue and the related documentation:
https://github.com/abpframework/abp/issues/23876#issuecomment-3425490743
https://abp.io/docs/latest/framework/ui/angular/localization
For now, you'll need to provide the localization key in the default resource. Based on my investigation, this does not appear to be a bug. That said, we could consider adding support for specifying a resource name in the UI. What do you think considering other UI types rather than Angular @maliming?
@sumeyyeKurtulus , I'm terribly sorry, I forgot to remove the definition of the extra property from the BookStoreModuleExtensionConfigurator.cs. It is supposed to be present only in IdentityExtensionsModuleExtensionConfigurator.cs. There is no bug in the backend as I can see that the name and the resource shows up properly in the localization.
But the way in which it is being handled in Angular is causing the issue. If you can take a look at my PR https://github.com/abpframework/abp/pull/25750, I have explained what is going wrong in Angular side. But even after that fix, it seems to only partially solve the problem.
I am not able to provide the localization key in the default resource because it needs to be packaged as part of my IdentityExtensionsModule since that is where my extra property is defined. I'm trying to distribute this IdentityExtensionModule as a separate NuGet which can be used by multiple applications. I am currently not able to do so without also asking the application developers to add the localizations for the extra properties into their application's localization files.
Thank you for the explanation. In this case, the related component should still support it like this https://github.com/abpframework/abp/blob/f002961019c27df8e668f611a132829d420e3104/npm/ng-packs/packages/components/extensible/src/lib/components/extensible-form/extensible-form-prop.component.html#L161
expected to be
{{ prop.resourceName + '::' + prop.displayName | abpLocalization }}
@sumeyyeKurtulus the direction is right, but we don't need to add the resource name in the template — prop has no resourceName field, and prop().displayName is already the resource-qualified key that createDisplayNameLocalizationPipeKeyGenerator produces (e.g. IdentityExtensions::SocialSecurityNumber). So the extra branch can use the same expression as a normal prop:
{{ prop().displayName | abpLocalization }}
The table header in extensible-table.component.html has the same isExtra ? '::' + prop.displayName and needs the same change.
@chandralk could you update #25750 to cover both templates and keep the values lookup as a fallback? resources[name].texts[key] first, then values[name][key] — Angular always loads resources from the localization endpoint, and values is filled too when IncludeLocalizationResources is on, so reading only resources breaks the existing values-based specs. Worth keeping both paths covered by tests.
@maliming, thanks for the direction. I have created a new PR which addresses both the issues: #25801.
Please review the same.
Also let me know if it is possible to back port this into ABP 8.x.
Hello @chandralk again,
Thank you for your contribution. I have checked and merged your PR. It will be released in the next version. As you do not prefer upgrading your application, I can suggest you this workaround
// angular/src/app/app.module.ts
import {
INCUDE_LOCALIZATION_RESOURCES_TOKEN,
} from '@abp/ng.core';
@NgModule({
providers: [
{
provide: INCUDE_LOCALIZATION_RESOURCES_TOKEN,
useValue: true,
},
],
})
export class AppModule {}
// angular/src/app/app-routing.module.ts
import { eIdentityComponents } from '@abp/ng.identity';
const routes: Routes = [
{
path: 'identity',
loadChildren: () =>
import('@abp/ng.identity').then(m =>
m.IdentityModule.forLazy({
createFormPropContributors: {
[eIdentityComponents.Users]: [useQualifiedExtraPropertyDisplayName],
},
editFormPropContributors: {
[eIdentityComponents.Users]: [useQualifiedExtraPropertyDisplayName],
},
}),
),
},
export class AppRoutingModule {}
function useQualifiedExtraPropertyDisplayName(propList: any) {
propList.forEach(({ value: prop }: { value: FormProp }) => {
if (!prop.isExtra) return;
(prop as any).displayTextResolver = () => prop.displayName;
});
}
You can let us know if you need further assistance. Thank you for your cooperation.
Thank you very much @sumeyyeKurtulus and @maliming for the workaround. We'll verify whether this suits our needs or not and apply it accordingly.
Meanwhile, I'm closing this issue.