Introduction to Bing Translate
Bing Translate is a powerful API service provided by Microsoft that allows developers to integrate language translation capabilities into their applications. This service supports dozens of languages and provides reliable translations along with multiple useful features.
Getting Started with Bing Translate API
To start using Bing Translate API, you need to sign up for an Azure account and subscribe to the Translator Text API. Once you have the subscription key, you can start making requests to the API.
Basic Translation
The basic translation API allows you to translate text from one language to another:
const axios = require('axios'); const subscriptionKey = 'YOUR_AZURE_SUBSCRIPTION_KEY'; const endpoint = 'https://api.cognitive.microsofttranslator.com'; async function translateText(text, toLang) { const response = await axios({ baseURL: endpoint, url: '/translate', method: 'post', headers: { 'Ocp-Apim-Subscription-Key': subscriptionKey, 'Ocp-Apim-Subscription-Region': 'YOUR_REGION', 'Content-type': 'application/json' }, params: { 'api-version': '3.0', 'to': [toLang] }, data: [{ 'text': text }], responseType: 'json' }); console.log(response.data); } translateText('Hello, world!', 'es');
Language Detection
The language detection API can automatically detect the language of a given text:
async function detectLanguage(text) { const response = await axios({ baseURL: endpoint, url: '/detect', method: 'post', headers: { 'Ocp-Apim-Subscription-Key': subscriptionKey, 'Ocp-Apim-Subscription-Region': 'YOUR_REGION', 'Content-type': 'application/json' }, params: { 'api-version': '3.0', }, data: [{ 'text': text }], responseType: 'json' }); console.log(response.data); } detectLanguage('Bonjour tout le monde!');
Transliteration
The transliteration API converts text in one script to another script:
async function transliterateText(text, fromScript, toScript) { const response = await axios({ baseURL: endpoint, url: '/transliterate', method: 'post', headers: { 'Ocp-Apim-Subscription-Key': subscriptionKey, 'Ocp-Apim-Subscription-Region': 'YOUR_REGION', 'Content-type': 'application/json' }, params: { 'api-version': '3.0', 'language': 'ja', 'fromScript': fromScript, 'toScript': toScript }, data: [{ 'text': text }], responseType: 'json' }); console.log(response.data); } transliterateText('こんにちは', 'jpan', 'latn');
Named Entity Recognition
The Named Entity Recognition (NER) API identifies and categorizes entities in the text:
async function recognizeEntities(text) { const response = await axios({ baseURL: endpoint, url: '/entities', method: 'post', headers: { 'Ocp-Apim-Subscription-Key': subscriptionKey, 'Ocp-Apim-Subscription-Region': 'YOUR_REGION', 'Content-type': 'application/json' }, params: { 'api-version': '3.0', }, data: [{ 'text': text }], responseType: 'json' }); console.log(response.data); } recognizeEntities('Microsoft was founded by Bill Gates and Paul Allen.');
App Example
Below is a simple example of a Node.js application that integrates the Bing Translate APIs to create a multilingual translation service:
const express = require('express'); const axios = require('axios'); const app = express(); const subscriptionKey = 'YOUR_AZURE_SUBSCRIPTION_KEY'; const endpoint = 'https://api.cognitive.microsofttranslator.com'; app.use(express.json()); app.post('/translate', async (req, res) => { const { text, toLang } = req.body; try { const response = await axios({ baseURL: endpoint, url: '/translate', method: 'post', headers: { 'Ocp-Apim-Subscription-Key': subscriptionKey, 'Ocp-Apim-Subscription-Region': 'YOUR_REGION', 'Content-type': 'application/json' }, params: { 'api-version': '3.0', 'to': [toLang] }, data: [{ 'text': text }], responseType: 'json' }); res.json(response.data); } catch (error) { res.status(500).send(error); } }); app.listen(3000, () => { console.log('Server running on port 3000'); });
This application provides an endpoint /translate
which accepts POST requests with JSON payload. It uses Bing Translate API to translate the input text to the desired language and returns the translated text.
Hash: b040910b373dd3bbb7b406fb7966d2a9471c2c07e155a5d7097123f5bf8b170c