Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Generate XML Language File For Android Using PHP



This is a common headache for all android users to create xml file for multi languages app. Today i am sharing a simple PHP script that will generate xml language file for android app.

To use this script you need to add all your keywords in a csv file like following image.


Now create one php file, android.php and add following code
<?php

$filename = "multi-language-android-en.csv";
$outputfileName = str_replace(".csv",".xml",$filename);
$file = fopen($filename, 'r');

$xml_file = $outputfileName;
if (file_exists($xml_file)) {
    unlink($xml_file);
}

//$xml_file = "language.xml";
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->formatOutput = true;
$productsX = $doc->createElement("resources");
$doc->appendChild($productsX);

while (($line = fgetcsv($file)) !== FALSE) {
    //$line is an array of the csv elements
    //print_r($line);

    if (count($line) > 0) {

        $checktoskip = substr(trim($line[0]), 0, 2);

        if (trim($line[0]) == "" || $checktoskip == "//") {
            $lan_xml = $doc->createComment('Revised April 2008');
        }

        $lan_xml = $doc->createElement("string");

        if (isset($line[0])) {
            $lan_xml->setAttribute('name', trim($line[0]));
        }

        if (isset($line[1])) {
            $name = $doc->createTextNode(trim($line[1]));
        }

        $lan_xml->appendChild($name);

        $productsX->appendChild($lan_xml);
    }
}

file_put_contents($xml_file, $doc->saveXML(), FILE_APPEND);
echo "Language xml generated successfully";

fclose($file);



Note: In this php code $filename = "multi-language-android-en.csv" is your csv file name.

Once you execute this code, it will create xml file like following image.



Download

Please support us, Like us on Facebook.

0 comments:

Post a Comment

 

Copyright @ 2017 HKBlog.