0

My question may be easy. But I've never interested with XML. So it is not understandable for me. There is an installation program and it's language files are xml format. Turkish file is very narrow that other languages. So I want to import "turkish.xml" into "general.xml". In "general.xml" file, if there are same nodes, nodes value will be changed. If not, keep it same. For example this is content of my turkish.xml.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <DICTIONARY type="singlelanguage" lang="tr" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="dictionary.xsd">
      <STRING id="AI.Directory.DefaultDefaultDir" value="Yeni Klasör"/>
      <STRING id="AI.Directory.Default32BitName" value="Otuziki bit"/>
    </DICTIONARY>

And below "general.xml" file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DICTIONARY type="singlelanguage" lang="neutral" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="dictionary.xsd">
  <STRING id="AI.Directory.DefaultDefaultDir" value="New Folder"/>
  <STRING id="AI.Directory.Default32BitName" value="32-bit"/>
  <STRING id="AI.DuplicateFile.DestName" value="Duplicate%s of %s"/>
  <STRING id="AI.Feature.DefaultTitle" value="Feature"/>
  </DICTIONARY>

If I manage it, it will be like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DICTIONARY type="singlelanguage" lang="neutral" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="dictionary.xsd">
  <STRING id="AI.Directory.DefaultDefaultDir" value="Yeni Klasör"/>
  <STRING id="AI.Directory.Default32BitName" value="Otuziki bit"/>
  <STRING id="AI.DuplicateFile.DestName" value="Duplicate%s of %s"/>
   <STRING id="AI.Feature.DefaultTitle" value="Feature"/>
  </DICTIONARY>

I do not know how I will manage it. Kind regards.

2 Answers 2

2

You can try to load both files in to a DOMDocument, then loop through the general file. Inside the loop, you can see if there is a turkish value and just replace it. See http://www.php.net/manual/en/book.dom.php

I will just give it a try. I have not tested this code, but hopefully you will manage to fix it. Should be something like this.

EDIT I did not use the $result value in the loop, try now

    <?php
    $turkish = new DOMDocument();
    $turkish->load('turkish.xml');

    $general = new DOMDocument();
    $general->load('general.xml');   

    $strings = $general->getElementsByTagName('STRING');
    $turkstrings = $turkish->getElementsByTagName('STRING');
    foreach($strings as $string) {
        foreach($turkstrings as $turk) {
            if($turk->getAttribute("id") == $string->getAttribute("id")) {
                $string->setAttribute("value", $turk->getAttribute("value"));
            }
        }
    }
    echo $general->saveXML();
    ?>
Sign up to request clarification or add additional context in comments.

1 Comment

You are doing too many loops for nothing with this. If you opt for DOMDocument, checkout getElementById(), it allows you to obtain the general element by the id from the turkish element, you can do this with one foreach over the smaller document changing the larger one.
1

I'm not sure if what you ask for is really useful because the software should already complete incomplete translation files with the defaults on the fly.

But sure technically it is possible, for example by going through the turkish XML and changing each corresponding entry in the general XML:

$new = simplexml_load_string($general);

$elements = simplexml_load_string($turkish)->STRING;

foreach ($elements as $element)
{
    $xpath = sprintf('//STRING[@id="%s"]', $element['id']);

    if (list($found) = $new->xpath($xpath))
    {
        $found['value']    = $element['value'];
        $found['imported'] = 'turkish';
    }
}

$new->asXML('php://output');

This example uses PHPs SimpleXML library. The output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DICTIONARY xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" type="singlelanguage" lang="neutral" xsi:noNamespaceSchemaLocation="dictionary.xsd">
  <STRING id="AI.Directory.DefaultDefaultDir" value="Yeni Klasör" imported="turkish"/>
  <STRING id="AI.Directory.Default32BitName" value="Otuziki bit" imported="turkish"/>
  <STRING id="AI.DuplicateFile.DestName" value="Duplicate%s of %s"/>
  <STRING id="AI.Feature.DefaultTitle" value="Feature"/>
</DICTIONARY>

See it in action.

2 Comments

Yes, alright. The software should already complete incomplete translation files with the defaults on the fly. But I want to translate all content to my native language. But when I have two diffrent language file, I miss which is which. Thanks for your help.
Ah this is good to know. So that you can better distinguish which one is from where (also in a machine-readable manner), I updated the answer setting the imported attribute to the source-files language name if the element was imported. So that you can more easily check how many strings need to be translated from the merged file ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.