Build a Simple Baidu Translate Desktop App with WPF in Minutes

This guide walks you through creating a lightweight WPF desktop application that leverages Baidu Translate’s public API to translate words, covering API key acquisition, HTTP GET requests, JSON parsing with Newtonsoft.Json, UI design in XAML, and handling special characters, enabling instant word translation without ads.

Baidu Tech Salon
Baidu Tech Salon
Baidu Tech Salon
Build a Simple Baidu Translate Desktop App with WPF in Minutes

Overview

We want a lightweight word‑translation tool that calls Baidu Translate’s public API, avoiding ads and extra UI clutter.

Getting the API key

Register on Baidu Developer Platform to obtain a client_id (also called APP ID). The request URL is:

http://openapi.baidu.com/public/2.0/bmt/translate?client_id=YourApiKey&q=WORD&from=auto&to=auto

Parameters: client_id – your API key; q – text to translate; from and to – source and target languages (use auto for automatic detection).

API limits and supported languages

The free tier provides 1,000 requests per hour and supports 13 language pairs. The response is JSON, e.g.:

{"from":"en","to":"zh","trans_result":[{"src":"Hello","dst":"\u4f60\u597d"}]}

Building the WPF client

Create a new WPF project (named BaiduTrans) and design a simple UI in MainWindow.xaml with a TextBox for input, a Button to trigger translation, and another TextBox to display the result.

<Window x:Class="BaiduTrans.MainWindow" Title="查词翻译" Height="439" Width="525" WindowStartupLocation="CenterScreen" KeyDown="Window_KeyDown">
  <Grid>
    <TextBlock Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" FontSize="14" Name="textBlock1" Text="输入单词:" VerticalAlignment="Top" />
    <TextBox Height="94" HorizontalAlignment="Left" Margin="10,40,0,0" Name="txtWord" VerticalAlignment="Top" Width="487" />
    <Button Content="翻译" FontSize="12" Height="26" HorizontalAlignment="Left" Margin="415,7,0,0" Name="button1" VerticalAlignment="Top" Width="82" Click="button1_Click" />
    <TextBox Height="242" HorizontalAlignment="Left" Margin="10,157,0,0" Name="txtResult" VerticalAlignment="Top" Width="487" />
  </Grid>
</Window>

Data model for JSON deserialization

Define two classes that match the JSON structure:

using System;
using System.Collections.Generic;

namespace BaiduTrans
{
    public class TransObj
    {
        public string from { get; set; }
        public string to { get; set; }
        public List<TransResult> trans_result { get; set; }
    }

    public class TransResult
    {
        public string src { get; set; }
        public string dst { get; set; }
    }
}

Installing Newtonsoft.Json

Use NuGet to install the Newtonsoft.Json package, which provides JsonSerializer for parsing the API response.

Button click implementation

The click handler builds the request URL, replaces special characters (e.g., #%23), downloads the JSON, deserializes it, and shows the translated text.

private void button1_Click(object sender, RoutedEventArgs e)
{
    WebClient client = new WebClient();
    string txtInput = txtWord.Text;
    txtInput = txtInput.Replace(@"#","%23");
    string url = string.Format(
        "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=YourApiKey&q={0}&from=auto&to=auto",
        txtInput);
    var buffer = client.DownloadData(url);
    string result = Encoding.UTF8.GetString(buffer);
    var r = new JsonSerializer().Deserialize<TransObj>(new JsonTextReader(new StringReader(result)));
    txtResult.Text = r.trans_result[0].dst;
}

A key‑down event forwards the Enter key to the same method, allowing quick queries.

private void Window_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        button1_Click(null, null);
    }
}

Testing the service

Enter “Hello” in the browser URL shown above; the response JSON appears as shown earlier. The same result is displayed in the WPF app, matching the web version of Baidu Translate.

{"from":"en","to":"zh","trans_result":[{"src":"Hello","dst":"\u4f60\u597d"}]}

Result comparison

Screenshots demonstrate that the desktop tool produces identical translations to the official web interface.

Feel free to extend the UI, add language selection, or handle additional special characters.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

c++JSONAPIDesktop AppWPFBaidu Translate
Baidu Tech Salon
Written by

Baidu Tech Salon

Baidu Tech Salon, organized by Baidu's Technology Management Department, is a monthly offline event that shares cutting‑edge tech trends from Baidu and the industry, providing a free platform for mid‑to‑senior engineers to exchange ideas.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.