Resolved #3790: Add AbpLocalizationOptions.DefaultResourceType.

pull/3831/head
Halil İbrahim Kalkan 6 years ago
parent ef54a6416f
commit 52b6c10a55

@ -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<AbpLocalizationOptions>(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)

@ -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<string, Dictionary<string, string>>();

@ -157,6 +157,9 @@ namespace Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations
}
localizationConfig.CurrentCulture = GetCurrentCultureInfo();
localizationConfig.DefaultResourceName = LocalizationResourceNameAttribute.GetName(
_localizationOptions.DefaultResourceType
);
Logger.LogDebug("Executed AbpApplicationConfigurationAppService.GetLocalizationConfigAsync()");

@ -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; }
/// <summary>
/// Used as the default resource when resource was not specified on a localization operation.
/// </summary>
public Type DefaultResourceType { get; set; }
public ITypeList<ILocalizationResourceContributor> GlobalContributors { get; }
public List<LanguageInfo> Languages { get; }

@ -39,6 +39,8 @@ namespace MyCompanyName.MyProjectName
.Add<MyProjectNameResource>("en")
.AddBaseTypes(typeof(AbpValidationResource))
.AddVirtualJson("/Localization/MyProjectName");
options.DefaultResourceType = typeof(MyProjectNameResource);
});
}
}

Loading…
Cancel
Save