Login Register
Code2night
  • Home
  • Guest Posts
  • Blog Archive
  • Tutorial
  • Languages
    • Angular
    • C
    • c#
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • Security
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
  1. Home
  2. Blogpost

Translating Website Data Using Azure Translator API in ASP.NET MVC

Date- Feb 01,2025

2917

Free Download
Azure tranlsate Api Multilingual Website

Translating Website Data Using Azure Translator API in ASP.NET MVC

In this article, we'll explore how to integrate Microsoft Azure Translator API into an ASP.NET MVC application to translate website text dynamically.

Prerequisites

  • Azure Subscription with Translator Text API enabled.
  • ASP.NET MVC application.
  • Newtonsoft.Json package for JSON processing.

Code Explanation

The HomeController is responsible for handling the translation functionality. Let's break down each part of the code:

1. Setting up Azure Translator API Credentials

We define the API endpoint, subscription key, and region required to access the Azure Translator API.

private static readonly string TranslatorEndpoint = 
    "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0";
private static readonly string SubscriptionKey = "YOUR_AZURE_SUBSCRIPTION_KEY";
private static readonly string Region = "centralindia"; // Replace with your region
    

Ensure that you replace YOUR_AZURE_SUBSCRIPTION_KEY with your actual Azure Translator API key.

2. Handling Language Culture

The OnActionExecuting method is overridden to set the current thread's culture based on the language parameter in the route.

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    string culture = (string)filterContext.RouteData.Values["lang"] ?? "en";
    Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

    base.OnActionExecuting(filterContext);
}
    

This ensures that translations follow the selected culture throughout the application.

3. Translating Text using Azure Translator API

The TranslateTextAsync method sends a POST request to Azure Translator API with the text to be translated.

private async Task TranslateTextAsync(string text, string targetLanguage = "es")
{
    using (HttpClient client = new HttpClient())
    {
        client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", SubscriptionKey);
        client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Region", Region);

        var requestBody = new[]
        {
            new { Text = text }
        };

        string requestJson = JsonConvert.SerializeObject(requestBody);
        StringContent content = new StringContent(requestJson, Encoding.UTF8, "application/json");

        HttpResponseMessage response = await client.PostAsync($"{TranslatorEndpoint}&to={targetLanguage}", content);
        response.EnsureSuccessStatusCode();

        string responseJson = await response.Content.ReadAsStringAsync();
        var translations = JsonConvert.DeserializeObject(responseJson);
        return translations[0].translations[0].text;
    }
}
   public async Task<ActionResult> TranslatePage(string[] texts, string targetLanguage = "es")
 {
     using (HttpClient client = new HttpClient())
     {
         client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", SubscriptionKey);
         client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Region", Region);

         var requestBody = texts.Select(text => new { Text = text }).ToArray();

         string requestJson = JsonConvert.SerializeObject(requestBody);
         StringContent content = new StringContent(requestJson, Encoding.UTF8, "application/json");

         HttpResponseMessage response = await client.PostAsync($"{TranslatorEndpoint}&to={targetLanguage}", content);
         response.EnsureSuccessStatusCode();

         string responseJson = await response.Content.ReadAsStringAsync();
         var translations = JsonConvert.DeserializeObject<dynamic>(responseJson);

         // Use a loop instead of LINQ
         List<string> translatedTexts = new List<string>();
         foreach (var item in translations)
         {
             translatedTexts.Add((string)item.translations[0].text);
         }

         return Json(translatedTexts.ToArray(),JsonRequestBehavior.AllowGet);
     }
 }  

This method:

  • Creates an HTTP client.
  • Sets the necessary authentication headers.
  • Serializes the text into JSON format.
  • Sends a POST request to the Translator API.
  • Parses the JSON response and extracts the translated text.

4. Calling Translation in the Index Action

In the Index action, we invoke TranslateTextAsync to translate the word "Contact" into Spanish.

public async Task Index()
{
    var data = await TranslateTextAsync("Contact");
    return View();
}
    

5. Other Actions

The controller includes basic actions like About and Contact, which display static pages.

public ActionResult About()
{
    ViewBag.Message = "Your application description page.";
    return View();
}

public ActionResult Contact()
{
    ViewBag.Message = "Your contact page.";
    return View();
}
    

Now copy following code on your Layout view

@using TranslatePOC.Helpers
@{
    string currentLanguage = ViewContext.RouteData.Values["lang"]?.ToString() ?? "en";
    string currentUrl = HttpContext.Current.Request.RawUrl;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-dark bg-dark">
        <div class="container">
            @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
            <button type="button" class="navbar-toggler" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" title="Toggle navigation" aria-controls="navbarSupportedContent"
                    aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse d-sm-inline-flex justify-content-between">
                <ul class="navbar-nav flex-grow-1">
                    <ul class="navbar-nav flex-grow-1">
                        @*<li>@Html.ActionLink(TranslatePOC.Resources.Resource.Home, "Index", "Home", new { area = "" }, new { @class = "nav-link" })</li>
        <li>@Html.ActionLink(TranslatePOC.Resources.Resource.About, "About", "Home", new { area = "" }, new { @class = "nav-link" })</li>
        <li>@Html.ActionLink(TranslatePOC.Resources.Resource.Contact, "Contact", "Home", new { area = "" }, new { @class = "nav-link" })</li>*@

                        <li>
                            @Html.ActionLinkWithDataTranslate("Home", "Index", "Home", "home_link", null, new { @class = "nav-link" })
                        </li>
                        <li>
                            @Html.ActionLinkWithDataTranslate("About", "About", "Home", "about_link", null, new { @class = "nav-link" })
                        </li>
                        <li>
                            @Html.ActionLinkWithDataTranslate("Contact", "Contact", "Home", "contact_link", null, new { @class = "nav-link" })
                        </li>
                    </ul>

                </ul>

                <div class="language-selector">
                    <label for="languageSelector" style="color:white !important" data-translate>Language:</label>
                    <select id="languageDropdown" onchange="changeLanguage(this.value)">
                        <option value="en" @(ViewContext.RouteData.Values["lang"]?.ToString() == "en" ? "selected" : "")>English</option>
                        <option value="es" @(ViewContext.RouteData.Values["lang"]?.ToString() == "es" ? "selected" : "")>Spanish</option>
                        <option value="he" @(ViewContext.RouteData.Values["lang"]?.ToString() == "he" ? "selected" : "")>Hebrew</option>
                    </select>
                </div>
            </div>
        </div>
    </nav>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year </p>
        </footer>
    </div>

    <script>
        function changeLanguage(selectedLanguage) {
            let currentUrl = window.location.pathname;

            // Check if the URL already includes a language code
            let regex = /^\/(en|es|he)/;
            if (regex.test(currentUrl)) {
                // Replace the language code in the URL
                currentUrl = currentUrl.replace(regex, `/${selectedLanguage}`);
            } else {
                // Add the language code to the URL
                currentUrl = `/${selectedLanguage}${currentUrl}`;
            }

            // Redirect to the updated URL
            window.location.href = currentUrl;
        }
    </script>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    <script>
         $(document).ready(function () {
     debugger;
     var targetLanguage = $('#languageDropdown').val();

     // Collect all elements with `data-translate` attribute
     var elements = $('[data-translate]');
     var texts = elements.map(function () { return $(this).text(); }).get();

     // Send texts to server for translation
     $.post('/' + targetLanguage +'/Home/TranslatePage', { texts: texts, targetLanguage: targetLanguage }, function (response) {
         console.log(response)
         if (response) {
             // Update the DOM with translated texts
             elements.each(function (index) {
                 $(this).text(response[index]);
             });
         } else {
             alert('Error: ' + response.message);
         }
     });

 });
    </script>
    @RenderSection("scripts", required: false)
</body>
</html>

Copy following code on your Index.html add data-translate class on elements you want to translate

@{
    ViewBag.Title = "Home Page";
  
}

<main>
    <section class="row" aria-labelledby="aspnetTitle">
       
        <h1 id="title" data-translate>Welcome to Translate Demo</h1>
        <p class="lead" data-translate>ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
      <p><a href="https://asp.net" class="btn btn-primary btn-md" data-translate>Learn more &raquo;</a></p>


    </section>

  
</main>

<script>
    // JavaScript to handle language change
    document.getElementById("languageSelector").addEventListener("change", function () {
        window.location.href = this.value; // Redirect to selected language
    });
</script>

Conclusion

By implementing this approach, you can dynamically translate website content using Azure Translator API in your ASP.NET MVC application.

Next Steps:

  • Extend the functionality to translate entire pages dynamically.
  • Store translations in a database for faster retrieval.
  • Allow users to select their preferred language.

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Implement Stripe Payment Gateway In ASP.NET
Sep 10, 2020
Jquery Full Calender Integrated With ASP.NET
Sep 30, 2020
Microsoft Outlook Add Appointment and Get Appointment using Asp.Net MVC
Oct 03, 2020
How to implement JWT Token Authentication and Validate JWT Token in ASP.NET MVC using JWT
Oct 12, 2022

Comments

Contents

More in ASP.NET MVC

  • Payumoney Integration With Asp.Net MVC 23081 views
  • MVC Crud Operation with Interfaces and Repository Pattern wi… 21768 views
  • Using Ajax in Asp.Net MVC 21120 views
  • Stopping Browser Reload On saving file in Visual Studio Asp.… 20542 views
  • Exception Handling and Creating Exception Logs in Asp.net MV… 20376 views
View all ASP.NET MVC posts →

Tags

AspNet
C#
programming
AspNet MVC
c programming
AspNet Core
C
software development
tutorial
MVC
memory management
Paypal
coding
coding best practices
data structures
programming tutorial
tutorials
object oriented programming
Slick Slider
StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
By Language
  • Angular
  • C
  • c#
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page