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
It automatically download and add all the jar files into your project by just adding the dependencies into the POM.xml file of your project.
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.
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.
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.
First enter the folder path where input files are kept. After-that, enter Output folder location where output files is to 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.
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 the 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.
The find() method of matcher Class finds the next sub-sequence 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);
In the SetBrowser() function we set the property of the ChromerDriver and give the location of it in our workspace
/** * 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(); }
Firstly we 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.
Secondly we 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 open according to the language which user has selected for converting the language file.
We find the location of text area using xpath where we enter the text which need to be converted and then send the text using sendKeys() method.
We 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.
Now 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"; }
Finally, main() class is called and then whole process works 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.
When you enter this command then it will ask you to enter Input, Output and ChromeDriver location.
After providing the path of the folder it will ask you to enter the Language in which you want to convert.
When language input is provided than chrome browser starts automatically and then project execute according to the program which is written and in output folder your files will be shown with converted language.
Support
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.
Please explore our cs-cart Development Services and Quality cs-cart add-ons
6 comments
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
Good Work
Hoping for the new article on selenium for CS-Cart.
Thanks for your appreciation. We will do so in future too.