I can’t find how to merge strings for translations in Android.

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

If there is any utility, I need it under GNU / Linux, and I didn’t understand whether it can be done with xmlutils.

Or tell me this function is only in Android Studio?

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

An example of what it is 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 are values-ru / strings.xml

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

how to merge these 2 files to get

 <?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 parsing. It's just strange that the standard mechanism for translating to Android does not have such necessary and simple functions as: merging and deleting already missing lines in the translation files

  • If there is any utility, then you need it under GNU / Linux - please give an example of what you have and what you want to get as a result. - aleksandr barakin
  • If there is any utility - it is not on topic . Utilities are for Google and other sites. Regarding the union - the matter itself is not tricky, so if you find nothing, then you can simply write a script that will do it. - Regent
  • Added an example. The fact is that there is a lot of such translation files and adding lines manually is not an option. - X-NicON pm
  • @Regent, This was not your answer. Updated the question. Yes, the script is clear. I just climbed into the Android project, never lazy before and can’t find anyone to ask why there’s no such toolkit. - X-NicON
  • 2
    you can simply not add untranslated strings to a localized file. Then they (missing strings) will be taken from values ​​/ strings - Vladyslav Matviienko

1 answer 1

In short, I never heard the answer. Closer and clearer of all, the work was through php, so I combined the android xml resources. very rough and straightforward, only for type "String". 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); }