Menu Close
    Searching for an experienced eCommerce & Marketplace Development Company ?

    Language File Conversion in CS-Cart using Selenium WebDriver

    Introduction

    Converting the language file of CS-Cart add-on into any desired language of the user choice is time taking.

    If we do it manually and to test that desired add-on into any language of the user choice is important nowadays, as a customer can require the add-on in any language of his choice, so to make our add-on compatible according to their language is quite important.

    Therefore, we have created a small automation program for converting CS-Cart language file into any desired language file of the user’s choice.

    Steps to Convert the Language File

    Create a New Maven Project

    If we create a Maven Project then we don’t have to add all jar files into the library which are needed for our project.

    It will automatically download and add all the jar files into your project which are required by just adding the dependencies into the POM.xml file of your project.

    POM_file

    Create a New Package

    After the above step create a new package under our project by any desired name of your choice. The package which you are creating is a user-defined package which will create a folder in your workspace.

    Package_creation

    Create a New Class

    After creating the package you have to create a class under the package which you have created in above step. A class is a user defined prototype from which objects are created. We can also create an object of a class and access it from another class.

    Class_creation

    Create a Function to Get Files

    We will declare all the variables at once before using them in any method using the Private modifier. We don’t have to declare any variable again and can use it anywhere inside any function of our project.

    Variables_define

    First enter the path of the folder where input files has been kept. After-that, enter Output folder location where output files will be stored. You should have ChromeDriver file according to the OS you are using and should enter whole path with file name.

    Note: In case you are using another browser you have to change the code accordingly.

    User_Input

    Now pick default English language file from given location using File class. In case of multiple files we will store it in an array to convert each file.
    Pick each file from the input location and copy the name of that language file. And create a converted language file at the Output location. If the file with same name will be present then it will delete that language file and create a new language file.

    At the end of this function we will call another function while passing parameters and converting that parameters to the string and return success.

    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @author    Webkul
     * @copyright Copyright (c) 2010-2020 Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    
    // Getting file
    File files = new File(wk_InputLocation);
    File[] file = files.listFiles();
    for (File Singlefile : file) 
    {
    	wk_pattern = Pattern.compile(".*/(.*)");
    	wk_matcher = wk_pattern.matcher(Singlefile.toString());
    	if (wk_matcher.find()) 
    	{
    		wk_outputFile = wk_OutputLocation + "/" + wk_matcher.group(1);
    		wk_checkOutputFile = new File(wk_outputFile);
    		if (wk_checkOutputFile.exists()) 
    		{
    			wk_checkOutputFile.delete();
    		}
    	}
    	readFiles(Singlefile.toString(), wk_outputFile.toString());
    }
    return "Success";
    }

    Now ask the user in which language he wants to convert the file using Scanner Class.

    Input_Language

    Now read the file by line using readLine() method if it’s not null than we will start a loop. After-that we will find the text which we want to convert using Pattern class. Which is used for regular expression with the help of matcher class which matches entire input sequence against the pattern.
    The find() method of matcher Class attempts to find the next sub-sequence of the input sequence that find the pattern and then store the result into a string variable ‘wk_resultText’.

    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @author    Webkul
     * @copyright Copyright (c) 2010-2020 Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    
    try {
    	wk_bufferedReader = new BufferedReader(new FileReader(new File(file.toString())));
    	wk_bufferedWriter = new BufferedWriter(new FileWriter(new File(outputFile)));
    	wk_fileLine = wk_bufferedReader.readLine();
    	while (wk_fileLine != null) 
    	{
    		if (!wk_fileLine.equalsIgnoreCase("msgid \"\"")) 
    		{
    			wk_pattern = Pattern.compile("msgstr.*?\"(.*)\"");
    			wk_matcher = wk_pattern.matcher(wk_fileLine.toString());
    			if (wk_matcher.find()) 
    			{
    				wk_resultText = wk_matcher.group(1);

    If ‘wk_resultText’ is not null than we will call the method which will open the Chrome browser using WebDriver . We will pass the path of the ChromeDriver stored in our workspace while calling the function.

    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @author    Webkul
     * @copyright Copyright (c) 2010-2020 Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    
    if (wk_resultText != null) 
    {
    	SetBrowser(wk_chromedir);

    Now in this SetBrowser() function we are setting the property of the ChromerDriver and giving the location of ChromeDriver in our workspace so it will open the Chrome Browser and then maximise it.

    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @author    Webkul
     * @copyright Copyright (c) 2010-2020 Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    
    public void SetBrowser(String chromeDriverPath) 
    {
    	System.setProperty("webdriver.chrome.driver", chromeDriverPath);// need
    	driver = new ChromeDriver();
    	driver.manage().window().maximize();
    }

    Now we will call other function TextConvert() after opening the Chrome Driver then we will give the url of Google in driver.get() method then we will find the input field of Google by using xpath.
    Now we will send the text into the input field using sendKeys() method and we will click on Search button of Google by finding the path of the Search button using xpath.

    Google Translate page will be opened according to the language which user has selected for converting the language file.
    We will find the location of text area using xpath where we will enter the text which need to be converted and will send the text using sendKeys() method.

    We will wait until Translated text-area is visible for picking up the value using xpath with the help of ExpectedConditions.visibilityOfElementLocated of WebDriverWait which is used to wait for an element to be visible on the page. After that we will get the text using getText() method and store them into a String variable ‘getConvertedText’.

    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @author    Webkul
     * @copyright Copyright (c) 2010-2020 Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    
    public String TextConvert(String englisText) throws InterruptedException 
    {
    	wait = new WebDriverWait(driver, 30);
    	driver.get("https://www.google.com/");
    	wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@name='q']")));
    	driver.findElement(By.xpath("//input[@name='q']")).sendKeys(wk_Language);
    	driver.findElement(By.xpath("/html/body/div[1]/div[2]/form/div[2]/div[1]/div[3]/center/input[1]")).click();
    	driver.findElement(By.xpath("//textarea[@id='tw-source-text-ta']")).click();
    	driver.findElement(By.xpath("//textarea[@id='tw-source-text-ta']")).sendKeys(englisText.toString().trim());
    	Thread.sleep(3000);
    	wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//pre[@id='tw-target-text']//span")));
    	String getConvertedText= driver.findElement(By.xpath("//pre[@id='tw-target-text']//span")).getText();
    	try 
    	{
    		byte bytes[] = getConvertedText.getBytes("UTF-8");
    		wk_value = new String(bytes, "UTF-8");
    	} 
    	catch (UnsupportedEncodingException e) 
    	{
    		e.printStackTrace();
    	}
    	driver.close();
    	return wk_value;
    }

    After this rest of the lines in the English Language file will be paste as it is using BufferedWriter class to write the line into the output file.

    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @author    Webkul
     * @copyright Copyright (c) 2010-2020 Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    
    if (wk_resultText != null) 
    {
    	SetBrowser(wk_chromedir);
    	wk_Result = TextConvert(wk_resultText.toString());
    	wk_bufferedWriter.write("msgstr \""+wk_Result.toString()+"\"");
    	wk_bufferedWriter.newLine();
    	wk_bufferedWriter.flush();
    }
    }
    else
    {
    	wk_bufferedWriter.write(wk_fileLine.toString());
    	wk_bufferedWriter.newLine();
    	wk_bufferedWriter.flush();
    }
    }
    else 
    {
    	wk_bufferedWriter.write(wk_fileLine.toString());
    	wk_bufferedWriter.newLine();
    	wk_bufferedWriter.flush();
    }
    wk_fileLine = wk_bufferedReader.readLine();
    }
    } 
    catch (Exception e) 
    {
    	e.printStackTrace();
    } 
    finally 
    {
    try 
    {
    	wk_bufferedReader.close();
    	wk_bufferedWriter.close();
    }
    catch (Exception e) 
    {
    	e.printStackTrace();
    }
    }
    return "Work done";
    }

    After all this, main() class will be called and then whole process will work according to it.

    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @author    Webkul
     * @copyright Copyright (c) 2010-2020 Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    
    public static void main(String[] args) 
    {
    	new Converter().getFiles();
    }

    Create jar file of your project and run it from Command Line by going to the specific location where you have stored your jar file and run the command java –jar FileName.jar.

    jar_file

    When you enter this command then it will ask you to enter Input, Output and ChromeDriver location.

    User_input_location

    After providing the path of the folder it will ask you to enter the Language in which you want to convert.

    user_location_console

    When you provide language input than chrome browser will start automatically and then your project will execute according to the program which you have written and in output folder your files will be shown with converted language.

    Chrome_image


    In case you have any queries then feel free to ask in the comment section below.
    This is all about CS-Cart Language Conversion through Selenium WebDriver.

    Thanks for reading this blog

    Happy testing!!

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    6 comments

  • Steffan
    Can we convert multiple files at the same time ??
    • Aishwarya Tiwari (Moderator)
      Hello,
      Yes, we can convert multiple files at the same time if there are multiple files in the location.
      You can convert any of the files into any language.
      Thanks & regards,
      Webkul
  • Allen
    Informative Content !!
    Good Work
    • Aishwarya Tiwari (Moderator)
      Thank you for your appreciation.
  • Cristina
    Well described blog!!!
    Hoping for the new article on selenium for CS-Cart.
    • Gautam Bagchi (Moderator)

      Thanks for your appreciation. We will do so in future too. 

  • Back to Top