repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import dJSON from 'dirty-json'; |
| #2 | import 'dotenv/config'; |
| #3 | |
| #4 | import { config } from '../core/constants'; |
| #5 | import { callOpenAI } from '../core/model'; |
| #6 | import { Logger } from '../utils/logger'; |
| #7 | |
| #8 | /** |
| #9 | * 翻译 JSON 对象到指定语言 |
| #10 | * @param filename 文件名(用于日志显示) |
| #11 | * @param json 待翻译的 JSON 对象 |
| #12 | * @param outputLocale 目标语言代码 |
| #13 | * @param entryLocale 源语言代码 |
| #14 | * @returns 翻译后的 JSON 对象 |
| #15 | */ |
| #16 | export const translateJSON = async ( |
| #17 | filename: string, |
| #18 | json: any, |
| #19 | outputLocale: string, |
| #20 | entryLocale = config.entryLocale, |
| #21 | ) => { |
| #22 | Logger.start('生成国际化内容', filename, `${entryLocale} → ${outputLocale}`); |
| #23 | |
| #24 | const res = await callOpenAI( |
| #25 | [ |
| #26 | { |
| #27 | content: [ |
| #28 | `Translate the i18n JSON file to ${outputLocale} according to BCP 47 standards`, |
| #29 | `Keep the key names the same as the original file, ensuring the output is still a valid i18n JSON file.`, |
| #30 | `Special attention: For "role" fields, keep the original values unchanged (such as "user", "assistant", "system", "function"), do not translate them.`, |
| #31 | `Only translate the content of "content" fields and other text content.`, |
| #32 | `Do not include any other text or explanations outside the JSON object. Start directly with a left brace and end with a right brace.`, |
| #33 | ].join('\n'), |
| #34 | role: 'system', |
| #35 | }, |
| #36 | { |
| #37 | content: JSON.stringify(json), |
| #38 | role: 'user', |
| #39 | }, |
| #40 | ], |
| #41 | { |
| #42 | response_format: { type: 'json_object' }, |
| #43 | }, |
| #44 | ); |
| #45 | |
| #46 | try { |
| #47 | const result = JSON.parse(res); |
| #48 | Logger.success('国际化内容生成完成', filename); |
| #49 | return result; |
| #50 | } catch { |
| #51 | Logger.warn('JSON 解析失败,尝试使用 dirty-json', filename); |
| #52 | try { |
| #53 | const data = dJSON.parse(res); |
| #54 | Logger.debug('使用 dirty-json 解析成功', JSON.stringify(data, null, 2)); |
| #55 | Logger.success('国际化内容生成完成', filename, '使用 dirty-json'); |
| #56 | return data; |
| #57 | } catch (error) { |
| #58 | Logger.error('国际化内容生成失败', `${filename}: ${error}`); |
| #59 | Logger.debug('原始响应内容', res); |
| #60 | return null; |
| #61 | } |
| #62 | } |
| #63 | }; |
| #64 | |
| #65 | |
| #66 |