diff --git a/code/Chinese2English.rar b/code/Chinese2English.rar new file mode 100644 index 00000000..8cd39a9e Binary files /dev/null and b/code/Chinese2English.rar differ diff --git a/code/replase3.py b/code/replase3.py new file mode 100644 index 00000000..74eb73b3 --- /dev/null +++ b/code/replase3.py @@ -0,0 +1,77 @@ +import xml.etree.ElementTree as et +import re +import os +from bs4 import BeautifulSoup + +def read_resx_to_dict(path): + tree = et.parse(path) + root = tree.getroot() + res_dict_x = {} + for data in root.findall('data'): + name = data.attrib['name'] + value = data.find('value').text + res_dict_x[value] = name + return res_dict_x + + +def replace_text(content, old_text, new_text): + # 替换在标签之间的文本 + # content = re.sub(r'>(.*?)' + re.escape(old_text) + r'(.*?)<', + # f'>\\1\\2<', + # content) + + content = re.sub(r'>' + re.escape(old_text) + r'<', + f'><', + content) + + # # 替换在标签之间的文本2 + # content = content.replace(old_text+"<",'<') + + # 替换在属性值中的文本 + content = re.sub(r'"([^"]*)"', + lambda m: f'\"<%$ Resources:Lan,{new_text}%>\"' if m.group(1) == old_text else m.group(0), + content) + return content + +def replace_text_in_aspx(aspx_file, res_dict): + with open(aspx_file, 'r', encoding='utf-8', newline='') as file: + aspx_content = file.read() + + for old_text, new_text in res_dict.items(): + if new_text != "": + aspx_content = replace_text(aspx_content, old_text, new_text) + + with open(aspx_file, 'w', encoding='utf-8', newline='') as file: + file.write(aspx_content) + print(f"{aspx_file} ...替换完成") + + +def get_aspx_files(directory): + aspx_files = [] + for root, dirs, files in os.walk(directory): + for file in files: + if file.endswith('.aspx'): + aspx_files.append(os.path.join(root, file)) + return aspx_files + + +#示例调用 +resx_path = "E:\\work_zhj\\Chinese2English\\ZHGL.resx" +directory_path = 'E:\work\project\成达\sggl_cd\SGGL\FineUIPro.Web\ZHGL' + +res_dict = read_resx_to_dict(resx_path) +print("资源文件分析完成") +print("开始替换") + +aspx_files = get_aspx_files(directory_path) +for file in aspx_files: + replace_text_in_aspx(file, res_dict) + +# 替换 单个aspx 文件中的文本 +# resx_path = "E:\\work_zhj\\Chinese2English\\rest.resx" +# +# res_dict = read_resx_to_dict(resx_path) +# +# directory_path = "E:\work\project\成达\sggl_cd\SGGL\FineUIPro.Web\index.aspx" +# +# replace_text_in_aspx(directory_path, res_dict) \ No newline at end of file diff --git a/img/img.png b/img/img.png new file mode 100644 index 00000000..30543e12 Binary files /dev/null and b/img/img.png differ diff --git a/img/img_1.png b/img/img_1.png new file mode 100644 index 00000000..845f02ce Binary files /dev/null and b/img/img_1.png differ diff --git a/img/功能一览.xmind b/img/功能一览.xmind new file mode 100644 index 00000000..75f26a34 Binary files /dev/null and b/img/功能一览.xmind differ diff --git a/img/成达翻译工作步骤.png b/img/成达翻译工作步骤.png new file mode 100644 index 00000000..68adbfb6 Binary files /dev/null and b/img/成达翻译工作步骤.png differ diff --git a/img/项目总览.png b/img/项目总览.png new file mode 100644 index 00000000..a6e740ec Binary files /dev/null and b/img/项目总览.png differ diff --git a/readme.md b/readme.md new file mode 100644 index 00000000..cc4d069c --- /dev/null +++ b/readme.md @@ -0,0 +1,79 @@ +## 对.net项目进行整体的国际化 + +### 整体思路 +- 手动处理的方式,面对整体项目文件较多时效率有限, +- 举例来说,总数为20000+需要替换的地方 +- 其中大部分为重复的替换操作。 + +- 生成的资源文件[Chinese2English.rar](code/Chinese2English.rar) +- 替换资源时的参考代码[replase3.py](code/replase3.py) +- 生成资源方式参考![img_1.png](img/img_1.png) +### 目前欠缺 +- ASPX.cs 文件内未修改,资源确认, Menu未国际化。 +- 一部分主页未替换 +- 建议每个页面都打开确认,有时候可能漏了个别页面。 +![img.png](img/img.png) + +### 半自动化处理流程一览 +![成达翻译工作步骤.png](img/%E6%88%90%E8%BE%BE%E7%BF%BB%E8%AF%91%E5%B7%A5%E4%BD%9C%E6%AD%A5%E9%AA%A4.png) + +### 具体操作步骤 + +* 判断页面总量,参考img/项目总览.png + * 查看代码判断哪些部分需要进行处理 + - aspx文件 + - ` 标题 <%$ Resources:Lan,{new_text}%>` + - aspx.cs 文件中的提示词 + - Alert.ShowInParent("请至少选择一条记录!", MessageBoxIcon.Warning); + - 下拉框的 "请选择" + * 参考res.resx 文件的写法,使用工具生成对应文件的资源文件,拿通义灵码举例:指定代码后使用promote + + 为了实现 这个文件的国际化,我们需要为文件中的所有静态文本添加资源文件中的键值。 + 以下是 Lan.zh-CN.resx 和 Lan.en.resx 文件中需要添加的资源项。 + 输出参考: 装置/单元编号 装置/单元名称 + 不需要给出aspx文件,但是需要注意所有会被展示的中文文本都需要国际化,比如说单位。 + 请使用注释标明文件名。不需要给出修改后的aspx文件。 + name的前缀为文件名,不允许使用中文, + 如果我输入1,代表之前的输出很满意,请继续按照之前的方式输出。 + + + +- 获得中英文资源类似 + + 开工报告 + 按名称查询 + 查询 + 增加 + 修改 + 删除 + 确定要删除数据吗? + 序号 + 编号 + 名称 + 编制人 + 上传日期 + 备注 + 附件查看 + 每页记录数: + 页面编辑 + 注意:首次获得的返回资源非常重要一定要调整到正确,之后每次输入1即可。 + +- 使用资源文件对代码进行替换 参考代码见code文件夹 replace3.py +- 将中英文两份资源文件复制到 lan.resx Lan.en-US.resx Lan.zh-CN.resx 等资源文件并确认能否正常显示 +- 如果存在资源名称重复可以使用rename.py ,注意name的大小写在resx文件中是不敏感的 +- 运行代码,然后查漏补缺!!! +- aspx.cs文件中由于重复度相对比较高,建议使用python代码 进行批量替换操作然后再查漏补缺 + +### 操作流程 +1. 查看需要进行国际化的部分 +2. 生成资源文件,建议批量处理。 +3. 使用python代码通过资源文件,批量替换代码中的关键词 +4. 生成的资源文件整合进项目文件 +5. 运行并查漏补缺。 + + + + +### 项目总览(后来发现还有一页!外加几个菜单!) +![项目总览.png](img/%E9%A1%B9%E7%9B%AE%E6%80%BB%E8%A7%88.png) \ No newline at end of file