使用translateR调用微软翻译报错:无法解析datamarket.accesscontrol.windows.net
Hey there, let's break down your issues step by step:
1. The "Could not resolve host" Error
This is the critical problem stopping your code from running. The translateR package relies on Microsoft's deprecated Data Market API, which was shut down years ago. The host datamarket.accesscontrol.windows.net no longer exists, so your R session can't connect to it to authenticate or process translations.
Unfortunately, translateR hasn't been updated to support Microsoft's current translation service (Azure Translator Text API), so you'll need to switch to a maintained package that works with the modern API.
2. The Language Detection Warning
The warning about MIDDLE_FRISIAN vs ENGLISH is a side effect of the old API's outdated language detection model. Once you switch to a modern package, this issue will likely disappear, but it's also a good reminder to double-check your source.lang parameter matches the actual content.
Recommended Solution: Use Azure Translator Text API with a Modern R Package
Here are two reliable alternatives to get your French-to-English translations working:
Option 1: Use the azure.cognitive.services Package
First, install and load the package:
install.packages("azure.cognitive.services") library(azure.cognitive.services)
Then, you'll need an Azure Translator Text resource (create one in the Azure Portal, get your subscription key and endpoint). Use this code:
# Replace with your Azure subscription key and endpoint azure_key <- "YOUR_AZURE_TRANSLATOR_KEY" azure_endpoint <- "YOUR_AZURE_TRANSLATOR_ENDPOINT" # Initialize the translator translator <- translator_api(subscription_key = azure_key, endpoint = azure_endpoint) # Translate French text to English french_text <- c("Bonjour le monde.", "Ceci est un test.") translated_text <- translate(translator, text = french_text, from = "fr", to = "en") # View results translated_text
Option 2: Use the translate Package
This package has a simpler interface for Azure Translator:
install.packages("translate") library(translate) # Set your Azure credentials Sys.setenv( AZURE_TRANSLATOR_KEY = "YOUR_AZURE_TRANSLATOR_KEY", AZURE_TRANSLATOR_REGION = "YOUR_AZURE_REGION" # e.g., "eastus" ) # Translate french_text <- c("Bonjour le monde.", "Ceci est un test.") translated <- translate(french_text, from = "fr", to = "en") print(translated)
Key Notes
- Make sure you replace the placeholder keys/endpoints with your actual Azure credentials (you can get these from the Azure Portal after creating a Translator Text resource).
- Both packages are actively maintained and support the latest Azure Translator API, so you won't run into the deprecated host error anymore.
内容的提问来源于stack exchange,提问作者Mahnoor

