expressappframework-403982-backend-web-api-service-access-localization-from-web-api-controller.md
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.
Refer to the following article for instructions on how to localize UI strings for an XAF application: Localization.
Note
This option of our Web API Service ships as part of the DevExpress Universal Subscription.
Use the localization controller API to obtain translations for the following strings:
Class captions
Member captions
Action captions
Custom strings
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.
Note
The localization controller does not require authorization. You need to take the following data access specifics into account:
Without authorization, the controller has significantly better performance, which means it can process many more requests per second than other Web API endpoints.
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.
// 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.
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);
{SolutionName}.Blazor.Server project.Microsoft.AspNetCore.Mvc.Testing package reference.See Also