From 1c08ea4101289c062cd17f21b7f65984c9fcd1d2 Mon Sep 17 00:00:00 2001 From: Joris Kalz Date: Thu, 28 Dec 2023 10:11:23 +0100 Subject: [PATCH] Enable Agent-Specific Real-Time Translation --- README.md | 28 +++++++++++++++ .../real-time-translation/webResourceV2.js | 36 ++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 019bcd1..d2445cf 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,34 @@ This repo provides samples for apps, solutions, and related services for Dynamics 365. For Dynamics 365 docs, visit . +## Agent-Specific Real-Time Translation + +To support agent-specific real-time translation in Omnichannel for Customer Service, a new field must be added to the `systemuser` entity within the Dataverse. This field will store the ISO 639-1 language code representing the agent's preferred language. The language code should be a two-letter code (e.g., 'de' for German, 'es' for Spanish, etc.). + +To enable and configure agent-specific real-time translation, follow these steps: + +1. Add a custom field to the `systemuser` entity in the Dataverse to store the ISO 639-1 language code for each agent's preferred language (e.g., 'en' for English, 'de' for German). + +2. In the script, locate the `C1WebResourceNamespace` object and find the `agentPreferredLanguageField` property. Replace `'jkalz_preferredlanguagecode'` with the actual schema name of the custom field you added in step 1. This should be a string in single quotes. + +3. Set the `enableAgentSpecificTranslation` property to `true` to activate the agent-specific translation feature. + +Example configuration in the script: +```javascript +var C1WebResourceNamespace = { + // ... (other properties) + + // The actual schema name of the custom field for agent's preferred language + agentPreferredLanguageField: 'your_custom_field_name_here', + + // Enable agent-specific translations + enableAgentSpecificTranslation: true, + + // ... (rest of the script) +}; +``` + +After configuring these values, the script will use the preferred language settings from each agent's profile to perform translations during customer interactions. ## Related samples repos - AL language code samples for Dynamics 365 Business Central: . diff --git a/customer-service/omnichannel/real-time-translation/webResourceV2.js b/customer-service/omnichannel/real-time-translation/webResourceV2.js index 408f403..6cfce05 100644 --- a/customer-service/omnichannel/real-time-translation/webResourceV2.js +++ b/customer-service/omnichannel/real-time-translation/webResourceV2.js @@ -20,7 +20,9 @@ var C1WebResourceNamespace = { useAzureTranslationApis: true,//please override it to false if planning to use google translation v2 api messageBuffer: new Map(), enableLanguageDetectionWithHistoryMessages: false, - + agentPreferredLanguageField: 'jkalz_preferredlanguagecode', // Replace with the actual field name for agent's preferred language on the systemuser entity + enableAgentSpecificTranslation: false, // Set to true to enable agent specific translations + //ISO 639-1 language code. It is supported by Azure Cognitive Translate API and Google V2 translation API ISO6391LanguageCodeToOcLanguageCodeMap: { 'gu': ['71', '1095'], @@ -241,6 +243,38 @@ var C1WebResourceNamespace = { conversationConfig, c1Language }); + + const getAgentPreferredLanguage = async () => { + try { + // Get the current user's ID + const userId = window.top.Xrm.Utility.getGlobalContext().userSettings.userId.replace("{", "").replace("}", ""); + + // Define the entity name and the columns to retrieve + const entityName = "systemuser"; + + // Define the columns to retrieve, jkalz_preferredlanguagecode is the language preference field + // which should be added to the systemuser entity + const columns = ["fullname", C1WebResourceNamespace.agentPreferredLanguageField]; + + // Fetch the user's details including their language preference + let userDataResponse = await window.top.Xrm.WebApi.retrieveRecord(entityName, userId, `?$select=${columns.join(',')}`); + + let agentPreferredLanguage = userDataResponse[C1WebResourceNamespace.agentPreferredLanguageField]; + return agentPreferredLanguage; + } catch (error) { + console.error("Error in fetching agent's preferred language:", error); + return null; // Handle error case appropriately + } + }; + + // Use the function to get the agent's preferred language, if it's null, then use the default + const agentPreferredLanguage = await getAgentPreferredLanguage(); + + if (C1WebResourceNamespace.enableAgentSpecificTranslation) { + const agentPreferredLanguage = await getAgentPreferredLanguage(); + c1Language = agentPreferredLanguage || c1Language; + } + //error handling if invalid language is found if (c1Language == "invalid code") { consoleLogHelper(conversationId, "Invalid c1Language found");