在Python中,尝试使用第二个RESX中的内容更新RESX

时间:2015-05-12 15:06:32

标签: python xml xml-parsing

我只是偶尔编码,这是我能组合起来的最好的,我希望有人可以指出我正确的方向。 (我一直在寻找其他答案,但他们只让我这么远)

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

基本上我想用另一个resx文件中的内容更新resx文件;要更改的部分可以是文件中的任何位置。结构需要保持。

更详细...... 我们使用RESX文件来保存我们的本地化字符串 - 每种语言1个RESX。 由于这些文件的大小,当我们得到要翻译的字符串时,我们只发送新的或更改的字符串,即delta,而不是每次都发送整个文件。

例如,在我们的RESX中间,我们有

<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>

例如,如果我发送了本地化的RESX只包含

 <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_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>

我想要一个

的python脚本

a)阅读翻译的RESX文件或通过它解析

b)在原始的完整RESX中找到匹配的元素属性,例如<data name="S_LANGUAGE_FRA" xml:space="preserve">

c)将原来的VALUE替换为翻译的VALUE <value>French</value>替换为<value> Français</value>

注意:我正在发送包含标题和放大器的有效RESX文件。 xsd info等 - 翻译公司可以本地化RESX,它更容易做到。

我正在尝试编写一个python脚本来执行此操作 - 使用基于属性的RESX中的已翻译字符串替换英语字符串。 我被困的地方是# copy transsubset[x][0].text value into child something... 我不知道该把什么放在这里我正在尝试不同的事情,但没有成功 也许我是以错误的方式来到这里,两个人不能真正合作,但我只是偶尔编码所以这对你们其他人来说可能是显而易见的。

感谢任何建议 感谢

1 个答案:

答案 0 :(得分:2)

我使用BeautifulSoup来达到您想要的效果。它不会根据模式验证文件(如果需要,可以单独执行),但写起来很干净:

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

server {
    listen 80;
    listen [::]:80;

    server_name mywebsite.com;

    return 301 https://$server_name$request_uri;
}

server {
        listen 443 default_server;
        listen [::]:443 default_server ipv6only=on;

        root /usr/share/nginx/html;
        index index.php index.html index.htm;

        ssl on;
        ssl_certificate path_to_ssl_certificate;
        ssl_certificate_key path_to_ssl_key;

        ssl_session_timeout 5m;

        ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
        ssl_prefer_server_ciphers on;

        # Make site accessible from http://localhost/
        server_name localhost;

        add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";

        location /web {
            alias /usr/share/nginx/html/web/app/webroot;
            try_files $uri $uri/ /web/webroot/index.php;
        }

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

        # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
        #location /RequestDenied {
        #       proxy_pass http://127.0.0.1:8080;
        #}

        #error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #
        #error_page 500 502 503 504 /50x.html;
        #location = /50x.html {
        #       root /usr/share/nginx/html;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #       # With php5-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#       listen 8000;
#       listen somename:8080;
#       server_name somename alias another.alias;
#       root html;
#       index index.html index.htm;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}