Reputation: 45
I only code occasionally and this is the best I could put together, I hope someone can point me in the right direction. (I have been looking for other answers but they only got me this far)
import xml.etree.ElementTree as ET
tree = ET.parse('FMTranslations.resx') # localised file - contains sub set of strings found in Master
transsubset = tree.getroot()
import xml.etree.ElementTree as ET2
tree2 = ET2.parse('FMTranslations.fr-FR.resx') # Master file for French strings.
master = tree2.getroot()
count=0
for child in transsubset:
#print (child.tag, child.attrib)
#print (child.tag)
count += 1
match = 0
x = 5 # 5th element is the 1st data element we are interested in
print('translations : \n')
while x < count:
print(transsubset[x].attrib,'\n', transsubset[x][0].text)
print('\n\n\n\n')
for child in master: #
if child.attrib == transsubset[x].attrib:
print(child.attrib)
print(transsubset[x].attrib)
print('\n\n')
# copy transsubset[x][0].text value into child something...
match += 1
print(' -------------------- \n')
x += 1
Basically I want to update a resx file with content from another resx file; the sections to change could be anywhere in the file & structure needs to be maintained.
In more detail… We use RESX files to hold our localised strings – 1 RESX per language. Due to the size of these files, when we get strings to translate we only send out the new or changed strings, the delta, rather than the entire file each time.
For example in the middle of our RESX we have
<data name="S_LANGUAGE_ENF_IE" xml:space="preserve">
<value>English</value>
<comment>Language English (Ireland)</comment>
</data>
<data name="S_LANGUAGE_GER" xml:space="preserve">
<value>German</value>
<comment>Language German</comment>
</data>
<data name="S_LANGUAGE_FRA" xml:space="preserve">
<value>French</value>
<comment>language French</comment>
</data>
<data name="S_LANGUAGE_NED" xml:space="preserve">
<value>Dutch</value>
<comment>Language Dutch</comment>
</data>
<data name="S_LANGUAGE_SPA" xml:space="preserve">
<value>Spanish</value>
<comment>Language Spanish</comment>
</data>
And if for example I sent out for localisation a RESX containing only
<data name="S_LANGUAGE_FRA" xml:space="preserve">
<value>French</value>
<comment>language French</comment>
</data>
<data name="S_LANGUAGE_NED" xml:space="preserve">
<value>Dutch</value>
<comment>Language Dutch</comment>
</data>
When these came back translated, I’d have (to add to the French file).
<data name="S_LANGUAGE_FRA" xml:space="preserve">
<value>Français</value>
<comment>language French</comment>
</data>
<data name="S_LANGUAGE_NED" xml:space="preserve">
<value>Néerlandais</value>
<comment>Language Dutch</comment>
</data>
And I’d want a python script which would
a) Read the translated RESX file or parse through it
b) find the matching element attribute in the original, complete RESX e.g. <data name="S_LANGUAGE_FRA" xml:space="preserve">
c) replace the original VALUE with the translated VALUE <value>French</value>
is replaced by <value> Français</value>
Note: I’m sending valid RESX files containing header & xsd info etc – the translation company can localise the RESX and its easier to do that.
I’m trying to write a python script to do this – replace English strings with translated strings in a RESX based on the attribute.
Where I get stuck is# copy transsubset[x][0].text value into child something...
I don’t know what to put here & I’m trying different things but with no success
Perhaps I’m coming at this the wrong way and the two can’t really work together but I only code occasionally so this may be obvious to the rest of you.
Grateful for any advice given Thanks
Upvotes: 3
Views: 1025
Reputation: 5604
I'd use BeautifulSoup to achieve the result you desire. It won't validate the files against the schema (which you could do as a separate step if you want), but it's so clean to write:
# -*- coding: utf-8 -*-
SOURCE_MARKUP = """
<root>
<data name="S_LANGUAGE_ENF_IE" xml:space="preserve">
<value>English</value>
<comment>Language English (Ireland)</comment>
</data>
<data name="S_LANGUAGE_GER" xml:space="preserve">
<value>German</value>
<comment>Language German</comment>
</data>
<data name="S_LANGUAGE_FRA" xml:space="preserve">
<value>French</value>
<comment>language French</comment>
</data>
<data name="S_LANGUAGE_NED" xml:space="preserve">
<value>Dutch</value>
<comment>Language Dutch</comment>
</data>
<data name="S_LANGUAGE_SPA" xml:space="preserve">
<value>Spanish</value>
<comment>Language Spanish</comment>
</data>
</root>
"""
DELTA_MARKUP = """
<root>
<data name="S_LANGUAGE_FRA" xml:space="preserve">
<value>Français</value>
<comment>language French</comment>
</data>
<data name="S_LANGUAGE_NED" xml:space="preserve">
<value>Néerlandais</value>
<comment>Language Dutch</comment>
</data>
</root>
"""
from bs4 import BeautifulSoup
source = BeautifulSoup(SOURCE_MARKUP)
delta = BeautifulSoup(DELTA_MARKUP)
delta_data = delta.find_all('data', attrs={'name': True})
for change in delta_data:
# find in source
origin = source.find('data', attrs={'name': change['name']})
# replace tag
origin.replace_with(change)
print source.prettify()
Upvotes: 2