diff --git a/docs/en/Localization.md b/docs/en/Localization.md index 11b838cf7f..53f62223cf 100644 --- a/docs/en/Localization.md +++ b/docs/en/Localization.md @@ -87,6 +87,21 @@ A JSON localization file content is shown below: * Every localization file should define the `culture` code for the file (like "en" or "en-US"). * `texts` section just contains key-value collection of the localization strings (keys may have spaces too). +### Default Resource + +`AbpLocalizationOptions.DefaultResourceType` can be set to a resource type, so it is used when the localization resource was not specified: + +````csharp +Configure(options => +{ + options.DefaultResourceType = typeof(TestResource); +}); +```` + +> The [application startup template](Startup-Templates/Application.md) sets `DefaultResourceType` to the localization resource of the application. + +See the *Client Side* section below for a use case. + ### Short Localization Resource Name Localization resources are also available in the client (JavaScript) side. So, setting a short name for the localization resource makes it easy to use localization texts. Example: @@ -180,18 +195,36 @@ Refer to the [Microsoft's localization documentation](https://docs.microsoft.com ABP provides JavaScript services to use the same localized texts in the client side. -Get a localization resource: +#### getResource + +`abp.localization.getResource` function is used to get a localization resource: ````js var testResource = abp.localization.getResource('Test'); ```` -Localize a string: +Then you can localize a string based on this resource: ````js var str = testResource('HelloWorld'); ```` +#### localize + +`abp.localization.localize` function is a shortcut where you can both specify the text name and the resource name: + +````js +var str = abp.localization.localize('HelloWorld', 'Test'); +```` + +`HelloWorld` is the text to localize, where `Test` is the localization resource name here. + +If you don't specify the localization resource name, it uses the default localization resource defined on the `AbpLocalizationOptions` (see the *Default Resource* section above). Example: + +````js +var str = abp.localization.localize('HelloWorld'); //uses the default resource +```` + ## See Also * [Localization in Angular UI](UI/Angular/Localization.md) \ No newline at end of file diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationLocalizationConfigurationDto.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationLocalizationConfigurationDto.cs index a5e6574e93..8064ff99d2 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationLocalizationConfigurationDto.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ApplicationLocalizationConfigurationDto.cs @@ -14,6 +14,8 @@ namespace Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations public CurrentCultureDto CurrentCulture { get; set; } + public string DefaultResourceName { get; set; } + public ApplicationLocalizationConfigurationDto() { Values = new Dictionary>(); diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationAppService.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationAppService.cs index 6271505ead..52af9090fa 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationAppService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationAppService.cs @@ -157,6 +157,9 @@ namespace Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations } localizationConfig.CurrentCulture = GetCurrentCultureInfo(); + localizationConfig.DefaultResourceName = LocalizationResourceNameAttribute.GetName( + _localizationOptions.DefaultResourceType + ); Logger.LogDebug("Executed AbpApplicationConfigurationAppService.GetLocalizationConfigAsync()"); diff --git a/framework/src/Volo.Abp.Localization/Volo/Abp/Localization/AbpLocalizationOptions.cs b/framework/src/Volo.Abp.Localization/Volo/Abp/Localization/AbpLocalizationOptions.cs index ea5232bb04..aba376d773 100644 --- a/framework/src/Volo.Abp.Localization/Volo/Abp/Localization/AbpLocalizationOptions.cs +++ b/framework/src/Volo.Abp.Localization/Volo/Abp/Localization/AbpLocalizationOptions.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using Volo.Abp.Collections; namespace Volo.Abp.Localization @@ -7,6 +8,11 @@ namespace Volo.Abp.Localization { public LocalizationResourceDictionary Resources { get; } + /// + /// Used as the default resource when resource was not specified on a localization operation. + /// + public Type DefaultResourceType { get; set; } + public ITypeList GlobalContributors { get; } public List Languages { get; } diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain.Shared/MyProjectNameDomainSharedModule.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain.Shared/MyProjectNameDomainSharedModule.cs index f20fb70a7e..d10c63986d 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain.Shared/MyProjectNameDomainSharedModule.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain.Shared/MyProjectNameDomainSharedModule.cs @@ -39,6 +39,8 @@ namespace MyCompanyName.MyProjectName .Add("en") .AddBaseTypes(typeof(AbpValidationResource)) .AddVirtualJson("/Localization/MyProjectName"); + + options.DefaultResourceType = typeof(MyProjectNameResource); }); } }