#程序员
这是一个分享如何以最佳方式支持国际化的方法,思路来源于 VS Code 源代码。方法如下:
无需实现编写国际化的 key,只需在代码中写入 key + message。例如:
localize("setting.auto_on_title", "Auto On")
编写完成后,使用 Babel 解析源代码,分析 AST。找出源码中全部的 localize 调用,提取出 key 和 message。
types.isIdentifier(callee) && callee.name === "localize"
根据当前全部 key message 列表,生成对应的 en-US.json。
计算出当前 zh-CN 和 en-US 的差异,生成 ChatGPT prompt。
-----prompt 开始分割线
帮我把下面的 key 翻译一下吧:
{ 'zh-CN.json': [ 'setting.auto_on_block_list_subtile' ] }
可以参考下面的英文:
{ 'setting.auto_on_block_list_subtile': 'The webpage annotation feature will not be automatically enabled on the following webpages.' }
返回格式为:{ "language.json": { "key": "message" } }
-----prompt 结束分割线
使用脚本解析 ChatGPT 返回的结果,生成新的 zh-CN.json 文件。
我的高亮插件就是以这种方式实现国际化的,暂时没时间把脚本抽出来当开源库, 直接看源码吧。
https://github.com/hamsterbase/hamsterbase-highlighter/blob/main/scripts/format.mts
这是一个分享如何以最佳方式支持国际化的方法,思路来源于 VS Code 源代码。方法如下:
无需实现编写国际化的 key,只需在代码中写入 key + message。例如:
localize("setting.auto_on_title", "Auto On")
编写完成后,使用 Babel 解析源代码,分析 AST。找出源码中全部的 localize 调用,提取出 key 和 message。
types.isIdentifier(callee) && callee.name === "localize"
根据当前全部 key message 列表,生成对应的 en-US.json。
计算出当前 zh-CN 和 en-US 的差异,生成 ChatGPT prompt。
-----prompt 开始分割线
帮我把下面的 key 翻译一下吧:
{ 'zh-CN.json': [ 'setting.auto_on_block_list_subtile' ] }
可以参考下面的英文:
{ 'setting.auto_on_block_list_subtile': 'The webpage annotation feature will not be automatically enabled on the following webpages.' }
返回格式为:{ "language.json": { "key": "message" } }
-----prompt 结束分割线
使用脚本解析 ChatGPT 返回的结果,生成新的 zh-CN.json 文件。
我的高亮插件就是以这种方式实现国际化的,暂时没时间把脚本抽出来当开源库, 直接看源码吧。
https://github.com/hamsterbase/hamsterbase-highlighter/blob/main/scripts/format.mts