2

I need to translate a page from Japanese to English, using selenium in chrome browser. I tried different ways one of sample code snippet is as following

import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Main {

    private WebDriver driver=null;
    WebDriverLoad a;

    @Test
    public void successfulDesignerLogin() throws Exception{
//      final DesiredCapabilities capabilities = DesiredCapabilities.chrome();
//        capabilities.setJavascriptEnabled(true);
        String chromedriver =  "/dev/Saved/chromedriver";
        System.setProperty("webdriver.chrome.driver",chromedriver);

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--lang=en-ca");
        //Map<String, Object> prefs = new HashMap<String, Object>();
        //prefs.put("intl.accept_languages", "en,en_US");
        //options.setExperimentalOption("prefs", prefs);


       ChromeDriver driver = new ChromeDriver(options);
       driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
       driver.get("https://www.bbc.com/japanese");
       driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
       driver.close();


}
}

I tried a couple of solution options.addArguments options.setExperimentalOption but nothing works can any one suggests me what can be the solution

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • How about just using the English version of `https://www.bbc.com/japanese` , I guess URL would be `https://www.bbc.com/english` – cruisepandey Dec 11 '18 at 05:05
  • No its just a sample site I need the code to change lang – Kumrun Nahar Keya Dec 11 '18 at 05:18
  • I can see a `HTML` tag with attribute as `lang` which is set to `ja`. Using javascript executor we can change that value to 'en'. The problem is we don't want to refresh the page yet DOM to be refreshed. I don't know any solution for this using selenium, maybe JQuery can help us. Anyway in case if you were wondering how to change the attribute value, you can use this code : `JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("document.getElementById('responsive-news').setAttribute('lang', 'en')");` – cruisepandey Dec 11 '18 at 08:03

2 Answers2

5

You need to enable translate and add target language ID to the whitelist {"from" : "to"}.

"translate":{"enabled":"true"}
"translate_whitelists": {"ja":"en"}

in java:

Map<String, Object> prefs = new HashMap<String, Object>();
Map<String, Object> langs = new HashMap<String, Object>();
langs.put("ja", "en");
prefs.put("translate", "{'enabled' : true}");
prefs.put("translate_whitelists", langs);
options.setExperimentalOption("prefs", prefs);
ewwink
  • 18,382
  • 2
  • 44
  • 54
2

Here is a C# version, directly add "translate" and "translate_whitelists" in "AddUserProfilePreference"

ChromeOptions options = new ChromeOptions();

Dictionary<string, object> LanguageList = new Dictionary<string, object>();
LanguageList.Add("fr", "en");

Dictionary<string, bool> enableObject = new Dictionary<string, bool>();
enableObj.Add("enabled", true);

options.AddUserProfilePreference("translate", enableObject);
options.AddUserProfilePreference("translate_whitelists", LanguageList);