Obtain Localization Strings from a Web API Controller Endpoint(从 Web API 控制器端点获取本地化字符串 )
This topic demonstrates how to obtain localized UI strings through HTTP requests to a Web API service. You can use the localized UI strings in your custom application that uses the XAF Web API as a backend.
本主题演示如何通过对Web API服务的HTTP请求获取本地化UI字符串。您可以在使用XAF Web API作为后端的自定义应用程序中使用本地化UI字符串。
Refer to the following article for instructions on how to localize UI strings for an XAF application: Localization.
有关如何本地化XAF应用程序的UI字符串的说明,请参阅以下文章:本地化。
Note
This option of our Web API Service ships as part of the DevExpress Universal Subscription.
我们的Web API服务的此选项作为DevExpress通用订阅的一部分提供。
Localization Controller(本地化控制器)
Localization Controller API(本地化控制器API)
Use the localization controller API to obtain translations for the following strings:
使用本地化控制器API获取以下字符串的翻译:
- Class captions(类字幕)
Web API Endpoint: Localization/ClassCaption?classFullName=…
Method called in the controller: CaptionHelper.GetClassCaption
- Member captions(会员字幕)
Web API Endpoint: Localization/MemberCaption?typeFullName=…&memberName=…
Method called in the controller: CaptionHelper.GetMemberCaption(Type, String)
The typeFullName string parameter is equal to typeof(objectType).FullName.
typeFullName字符串参数等于typeof(object tType)。全名。
- Action captions(动作字幕)
Web API Endpoint: Localization/ActionCaption?actionName=…
Method called in the controller: CaptionHelper.GetActionCaption
- Custom strings(自定义字符串)
Web API Endpoint: Localization/LocalizedText?groupPath=…&itemName=…
Method called in the controller: CaptionHelper.GetLocalizedText
You can control the language for localization through the Accept-Language request header. This header accepts language identifiers such as en, de, ja, and others. For additional details, refer to Globalization and localization in ASP.NET Core - QueryStringRequestCultureProvider.
您可以通过Accept-Language请求标头控制本地化的语言。此标头接受语言标识符,例如en、de、ja等。有关其他详细信息,请参阅ASP.NETCore-QueryStringRequestCultureProvider中的全球化和本地化。
Note
The localization controller does not require authorization. You need to take the following data access specifics into account:
本地化控制器不需要授权。您需要考虑以下数据访问细节:
You can access the localization controller without JWT tokens that are required for Web API endpoints.
您可以在没有Web API端点所需的JWT令牌的情况下访问本地化控制器。The localization controller provides access to a shared model layer, without considering user changes.
本地化控制器提供对共享模型层的访问,而无需考虑用户更改。You cannot use the localization controller to access secure data.
您不能使用本地化控制器访问安全数据。Without authorization, the controller has significantly better performance, which means it can process many more requests per second than other Web API endpoints.
未经授权,控制器的性能明显更好,这意味着它每秒可以处理比其他Web API端点更多的请求。
Example(示例)
This example demonstrates how to obtain a localized string. The code in this example sends a request to the Localization/LocalizedText endpoint with the groupPath and itemName parameters.
此示例演示如何获取本地化字符串。此示例中的代码使用groupPath和itemName参数向Localize/LocalizedText端点发送请求。
C#
// This code requests a German version of
// the `Messages:CannotUploadFile` caption.
HttpClient httpClient = new HttpClient();
// Set up client locale and Uri.
httpClient.BaseAddress = new Uri("https://localhost:5001/");
httpClient.DefaultRequestHeaders.Add("Accept-Language", "de");
// Arguments for the LocalizedText method.
var groupPath = "Messages";
var itemName = "CannotUploadFile";
// Send request for a localized string.
var response = await httpClient.GetAsync($"api/Localization/LocalizedText?groupPath={groupPath}&itemName={itemName}");
// Parse the result from HttpResponseMessage.
var localizedString = await response.Content.ReadAsStringAsync();
You can configure the request locale directly on an HttpRequestMessage as follows. For more details, read Accept-Language header in HttpRequestHeaders.
您可以直接在HttpRequestMessage上配置请求区域设置,如下所示。有关详细信息,请阅读HttpRequestHeaders中的Accept-Language标头。
C#
var customRequest = new HttpRequestMessage(HttpMethod.Get, $"api/Localization/LocalizedText?groupPath={groupPath}&itemName={itemName}");
customRequest.Headers.Add("Accept-Language", "en");
var response = await httpClient.SendAsync(customRequest);
Add Unit Tests for Localization Controller(为本地化控制器添加单元测试)
1.If your solution does not have a testing project, add a new xUnit test project.
如果您的解决方案没有测试项目,请添加一个新的xUnit测试项目。
2.Add a reference to {SolutionName}.Blazor.Server project.
添加对{SolutionName}的引用。Blazor. Server项目。
3.Add the Microsoft.AspNetCore.Mvc.Testing package reference.
添加Microsoft. AspNetCore.Mvc.测试包参考。
4.Add the following test to the xUnit project:
将以下测试添加到xUnit项目:
C#
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Testing;
using Xunit;
public class LocalizationUnitTests
: IClassFixture<WebApplicationFactory<MySolution.Blazor.Server.Startup>> {
HttpClient httpClient;
public LocalizationUnitTests(WebApplicationFactory<MySolution.Blazor.Server.Startup> webApplicationFactory) {
httpClient = webApplicationFactory.CreateClient();
httpClient.DefaultRequestHeaders.Add("Accept-Language", "de");
}
// A simple test example.
[Fact]
public async Task SampleLocalizationTest() {
var groupPath = "Messages";
var itemName = "CannotUploadFile";
var response = await httpClient.GetAsync($"api/Localization/LocalizedText?groupPath={groupPath}&itemName={itemName}");
var localizedString = await response.Content.ReadAsStringAsync();
Assert.NotEmpty(localizedString);
}
// An advanced test example.
[Theory]
[InlineData("en", "Messages", "CannotUploadFile")]
[InlineData("de", "Messages", "CannotUploadFile")]
public async Task TestLocalizedText(string locale, string groupPath, string itemName) {
var customRequest = new HttpRequestMessage(HttpMethod.Get, $"api/Localization/LocalizedText?groupPath={groupPath}&itemName={itemName}");
customRequest.Headers.Add("Accept-Language", locale);
var response = await httpClient.SendAsync(customRequest);
var localizedString = await response.Content.ReadAsStringAsync();
Assert.NotEmpty(localizedString);
}
}