Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET MVC
  4. Translating Website Data Using Azure Translator API in ASP.NET MVC

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

Date- Feb 01,2025 2983 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

How to generate pdf using itextsharp in asp.net mvc
Aug 06, 2023
How to export view as pdf in Asp.Net Core
Jul 05, 2022
Integrating Google Sign in Asp.net MVC
Jan 22, 2022
Payumoney Integration With Asp.Net MVC
Nov 02, 2020
Previous in ASP.NET MVC
Integrating ChatGPT API in ASP.NET
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    HTTP Error 500.32 Failed to load ASP NET Core runtime 6,925 views
  • 2
    Error-An error occurred while processing your request in .… 11,259 views
  • 3
    Comprehensive Guide to Error Handling in Express.js 216 views
  • 4
    ConfigurationBuilder does not contain a definition for Set… 19,449 views
  • 5
    Mastering Unconditional Statements in C: A Complete Guide … 21,488 views
  • 6
    Mastering JavaScript Error Handling with Try, Catch, and F… 147 views
  • 7
    Unable to connect to any of the specified MySQL hosts 6,217 views

On this page

🎯

Interview Prep

Ace your ASP.NET MVC interview with curated Q&As for all levels.

View ASP.NET MVC Interview Q&As

More in ASP.NET MVC

  • Implement Stripe Payment Gateway In ASP.NET 58737 views
  • Jquery Full Calender Integrated With ASP.NET 39652 views
  • Microsoft Outlook Add Appointment and Get Appointment using … 27574 views
  • How to implement JWT Token Authentication and Validate JWT T… 25274 views
  • MVC Crud Operation with Interfaces and Repository Pattern wi… 21880 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 | 1770
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
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
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor