How to curb the translation of an application on Android

Question:

I can't find how to do string concatenation for translations in Android.

There is a set of values ​​and there is a translation of it values-ru. How to add missing lines to values-ru from values? After all, strings.xml is an ordinary xml, I was looking for how to combine xml under GNU / Linux, I did not find anything, only paid utilities with one merge function under windows.

If there is some kind of utility, then it is needed for GNU / Linux, and I did not understand if this can be done using xmlutils.

or tell me this function is only available in Android Studio?

In general, we need the functions of the gettext (msgmerge) utilities, only for Android translations, I thought it was easy to find and Google normally talks about it, but no. I did not find anything like it.

An example of what we are talking about:

There are values ​​/ strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello!</string>
    <string name="world">World</string>
</resources>

And there is values-ru / strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Привет!</string>
</resources>

how to combine these 2 files to get the output

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Привет!</string>
    <string name="world">World</string>
</resources>

If someone has not heard about the "Official" way out of the situation, then yes, you just have to write your own parsing. It's just strange that the standard mechanism for translation in Android does not have such necessary and simple functions as: combining and deleting already missing lines in translation files

Answer:

In short, I never heard an answer. The closest and most understandable of all, the work was through php, so I forced the union of android xml resources. very rough and straightforward, only for the "String" type. in general, what was needed specifically for me.

function mergeAndrLangXml($from, $to){
    $xml_from = new DOMDocument();
    $xml_from->load($from);
    $xml_to = new DOMDocument();
    $xml_to->load($to);
    $xml_to->formatOutput =true;
    $xpath_from = new DOMXpath($xml_from);
    $xpath_to = new DOMXpath($xml_to);

    //Удаляем отсутствующие в from из to
    foreach( $xml_to->getElementsByTagName('string') as $v ){
        $find = $xpath_from->query('string[@name="'.$v->getAttribute("name").'"]');

        if($find->length == 0){
            $v->parentNode->removeChild($v);
        }
    }

    // Добавляем новые из from в to
    foreach( $xml_from->getElementsByTagName('string') as $v ){
      if($v->getAttribute('translatable') != 'false'){
        $find = $xpath_to->query('string[@name="'.$v->getAttribute("name").'"]');
        if($find->length == 0){
            $newElem = $xml_to->createElement('string',$v->textContent);
            $newElem->setAttribute('name',$v->getAttribute('name'));
            $xml_to->getElementsByTagName('resources')->item(0)->appendChild($newElem);
        }
      }
    }

    //марафет
    $file = $xml_to->saveXML($xml_to, LIBXML_NOEMPTYTAG);
    $file = str_replace("ng><","ng>\n<", $file);
    $file = str_replace("\n<string","\n    <string", $file);
    $file = str_replace("\n    \n","\n", $file);

    file_put_contents($to,$file);
}
Scroll to Top