【发布时间】:2019-02-14 13:27:55
【问题描述】:
在使用多语言应用工具包时,我们的 resx 和 xlf 文件包含许多我们不想翻译的垃圾(例如控件的大小和位置)。
是否有自动排除所有额外信息?
如果不是,是否只有使用多语言编辑器手动浏览 XLIFF 文件并将translatable 更改为 False 的方法?
当资源文件包含图片时,问题就更严重了……
【问题讨论】:
标签: c# multilingual-app-toolkit
在使用多语言应用工具包时,我们的 resx 和 xlf 文件包含许多我们不想翻译的垃圾(例如控件的大小和位置)。
是否有自动排除所有额外信息?
如果不是,是否只有使用多语言编辑器手动浏览 XLIFF 文件并将translatable 更改为 False 的方法?
当资源文件包含图片时,问题就更严重了……
【问题讨论】:
标签: c# multilingual-app-toolkit
在这里提问几天后,我尝试在 official support page 上针对 Multilingual App Toolkit 提出同样的问题,但我也没有得到回复。
因此,作为该问题的解决方法,我编写了一个 python 脚本来设置 XLIFF 文件中除.Text 之外的所有属性,以便将它们标记为translate="No"。多语言编辑器没有将“可翻译”设置为批量操作的选项,手动执行此操作将需要很长时间。
#!/usr/bin/env python
from lxml import etree
import os
NEW_LINE = '\r\n'
def set_translatable(filepath):
"""
Each string in the XLF file is set to translatable False unless
it is the `.Text` attribute that we actually want translated.
"""
doc = etree.parse(filepath)
for d in doc.iter():
if 'trans-unit' in d.tag:
translate = False
for k, v in d.items():
if k == 'id':
if v.endswith('.Text'):
translate = True
translate = 'yes' if translate else 'no'
d.set('translate', translate)
with open(filepath, 'w') as out_file:
doc.write(out_file)
def main():
cwd = os.getcwd()
for subdir, dirs, files in os.walk(cwd):
for file in files:
if filepath.endswith('.xlf'):
set_translatable(filepath)
if __name__ == '__main__':
main()
在 Visual Studio 中,我还必须手动打开每个表单并将 Localizable 属性设置为 True,以便将 .Text 值保存在相应的 .resx 中,多语言应用工具包可以处理它们。
【讨论】:
在我的情况下,我想忽略每个文件,所以这对我很有帮助:https://multilingualapptoolkit.uservoice.com/forums/231158-general/suggestions/13627704-add-ignore-exclude-filter-for-resx-files
我基本上加了
<SuppressAutomaticLocalization>true</SuppressAutomaticLocalization>
在.csproj文件的EmbeddedResource标签中,导致:
<ItemGroup>
<EmbeddedResource Update="Properties\Authorization\Components_Actions.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Components_Actions.Designer.cs</LastGenOutput>
<SuppressAutomaticLocalization>true</SuppressAutomaticLocalization>
</EmbeddedResource>
...
更多信息: 如果 .resx 文件未在 .csproj 文件中列出,请确保在 .resx 文件编辑器中设置 AccessModifier。如果未进行选择,则该文件不会出现在 .csproj 中
【讨论】: