Reputation: 27
We are implementing Azure Cognitive Search for our website which will be available in multiple languages. We have created multiple fields in our Index for each language and using Microsoft Analyzers on the fields.
Below is the default document schema in the Cosmos DB:
{
"id": "",
"Description": "some_value",
"Region": [ "some_value", "some_value" ],
"SpecificationData": [
{
"name": "some_key1",
"value": "some_value1",
},
{
"name": "some_key2",
"value": "some_value2",
}
]
}
We already have the data for fields mentioned above in English. However, we want to store data for other languages in the same index document and have added language analyzers for specific languages. Below is the updated schema after adding language analyzers:
{
"id": "",
"Description": "some_value",
"Description_pt": null,
"Description_fr": null,
"Region": [ "some_value", "some_value" ],
"SpecificationData": [
{
"name": "some_key1",
"value": "some_value1",
"name_pt": null,
"value_pt": null,
"name_fr": null,
"value_fr": null
},
{
"name": "some_key2",
"value": "some_value2",
"name_pt": null,
"value_pt": null,
"name_fr": null,
"value_fr": null
}
]
}
Initially, the corresponding translate fields are null. We are actually looking for a way to translate a field and update the corresponding field in another language. For example, translate the value of SpecificationData.name, SpecificationData.value to french and save the translation to SpecificationData.name_fr, SpecificationData.value_fr.
Upvotes: 0
Views: 472
Reputation: 17814
The simplest way to automatically generate the French description is to link your Cognitive Search service to a Cognitive Services account and then add a skillset with a translation skill configured like this:
{
"@odata.type": "#Microsoft.Skills.Text.TranslationSkill",
"defaultToLanguageCode": "fr",
"suggestedFrom": "en",
"context": "/document",
"inputs": [
{
"name": "text",
"source": "/document/Description"
}
],
"outputs": [
{
"name": "translatedText",
"targetName": "Description_fr"
}
]
}
More details in the documentation for the translation skill.
If you can't or don't want to use a skillset and are writing directly to your index, you can write code that does that, as outlined in Thiago's answer.
Upvotes: 1
Reputation: 18387
If you're using Azure Cognitive Search indexer, you can attach a Cognitive Service and it will translate automatically for you:
https://learn.microsoft.com/en-us/azure/search/search-indexer-overview#stage-3-skillset-execution
If you're adding documents through code, you can also use Cognitive Services in order to get the translations and proper set to the desired field:
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json; // Install Newtonsoft.Json with NuGet
class Program
{
private static readonly string key = "YOUR-KEY";
private static readonly string endpoint = "https://api.cognitive.microsofttranslator.com/";
// Add your location, also known as region. The default is global.
// This is required if using a Cognitive Services resource.
private static readonly string location = "YOUR_RESOURCE_LOCATION";
static async Task Main(string[] args)
{
// Input and output languages are defined as parameters.
string route = "/translate?api-version=3.0&from=en&to=de&to=it";
string textToTranslate = "Hello, world!";
object[] body = new object[] { new { Text = textToTranslate } };
var requestBody = JsonConvert.SerializeObject(body);
using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
{
// Build the request.
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(endpoint + route);
request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
request.Headers.Add("Ocp-Apim-Subscription-Key", key);
request.Headers.Add("Ocp-Apim-Subscription-Region", location);
// Send the request and get response.
HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
// Read response as a string.
string result = await response.Content.ReadAsStringAsync();
Console.WriteLine(result);
}
}
}
source:
Upvotes: 1