|
116 | 116 | }], |
117 | 117 | ]; |
118 | 118 | // Server errors wrapping ErrSharedScheduleReferences (internal/repo/shared_schedule_assets.go) |
119 | | - // read "共享任务引用资产不可用:<detail>", and the detail carries asset ids. No dictionary key |
120 | | - // can match such a message whole, and translating only the prefix leaves the detail Chinese, |
121 | | - // so each detail is translated as one sentence before the fragment pass runs. |
| 119 | + // read "共享任务引用资产不可用:<detail>", and the detail carries asset ids and names. No |
| 120 | + // dictionary key can match such a message whole, and translating only the prefix leaves the |
| 121 | + // detail Chinese, so the whole message is translated here and the fragment passes never see it |
| 122 | + // (they would rewrite a role named "代码审查" into "Code review"). An asset reference is |
| 123 | + // "type/id" or a bare type: no spaces, no full-width colon. |
122 | 124 | const SHARED_REF = 'A project asset referenced by the shared task is unavailable: '; |
| 125 | + const REF = '([^\\s:]+)'; |
| 126 | + // Causes returned by the sharing path (internal/repo/catalog_sharing.go) when publishing a task |
| 127 | + // shares a referenced LOOP; names in quotes are user data and stay as written. |
| 128 | + const shareCauses = [ |
| 129 | + [/^共享 LOOP 只能使用已共享的沙箱$/, () => 'a shared LOOP can only use shared sandboxes'], |
| 130 | + [/^沙箱不属于当前项目且尚未共享$/, () => "its sandbox isn't in this project and isn't shared yet"], |
| 131 | + [/^角色 (".*") 不存在$/, (_, name) => `role ${name} doesn't exist`], |
| 132 | + [/^角色 (".*") 不属于当前项目且尚未共享$/, (_, name) => `role ${name} isn't in this project and isn't shared yet`], |
| 133 | + ]; |
| 134 | + const shareCause = cause => { |
| 135 | + for (const [re, rep] of shareCauses) if (re.test(cause)) return cause.replace(re, rep); |
| 136 | + return HAN.test(cause) ? translate(cause) : cause; |
| 137 | + }; |
123 | 138 | const wrapped = [ |
124 | | - [/共享任务引用资产不可用:(.+?) 不存在或尚未共享(?=\s*$)/, (_, ref) => `${SHARED_REF}${ref} doesn't exist or isn't shared yet`], |
125 | | - [/共享任务引用资产不可用:(.+?) 不属于当前项目且尚未共享(?=\s*$)/, (_, ref) => `${SHARED_REF}${ref} isn't in this project and isn't shared yet`], |
126 | | - [/共享任务引用资产不可用:(.+?) 不支持随 AI 定时任务共享(?=\s*$)/, (_, ref) => `${SHARED_REF}${ref} can't be shared with an AI scheduled task`], |
127 | | - [/共享任务引用资产不可用:(.+?) 的归属已变更,请发布新版本(?=\s*$)/, (_, ref) => `${SHARED_REF}the owner of ${ref} has changed. Publish a new version`], |
128 | | - [/共享任务引用资产不可用:(.+?) 的权限或归属已变更(?=\s*$)/, (_, ref) => `${SHARED_REF}the access or owner of ${ref} has changed`], |
129 | | - [/共享任务引用资产不可用:(.+?) 不存在(?=\s*$)/, (_, ref) => `${SHARED_REF}${ref} doesn't exist`], |
| 139 | + // First, so a cause ending in 不存在 etc. is not taken for a reference by the rules below. |
| 140 | + [new RegExp(`共享任务引用资产不可用:共享 ${REF} 失败:(.+?)(?=\\s*$)`), (_, ref, cause) => `${SHARED_REF}failed to share ${ref}: ${shareCause(cause)}`], |
| 141 | + [new RegExp(`共享任务引用资产不可用:${REF} 不存在或尚未共享(?=\\s*$)`), (_, ref) => `${SHARED_REF}${ref} doesn't exist or isn't shared yet`], |
| 142 | + [new RegExp(`共享任务引用资产不可用:${REF} 不属于当前项目且尚未共享(?=\\s*$)`), (_, ref) => `${SHARED_REF}${ref} isn't in this project and isn't shared yet`], |
| 143 | + [new RegExp(`共享任务引用资产不可用:${REF} 不支持随 AI 定时任务共享(?=\\s*$)`), (_, ref) => `${SHARED_REF}${ref} can't be shared with an AI scheduled task`], |
| 144 | + [new RegExp(`共享任务引用资产不可用:${REF} 的归属已变更,请发布新版本(?=\\s*$)`), (_, ref) => `${SHARED_REF}the owner of ${ref} has changed. Publish a new version`], |
| 145 | + [new RegExp(`共享任务引用资产不可用:${REF} 的权限或归属已变更(?=\\s*$)`), (_, ref) => `${SHARED_REF}the access or owner of ${ref} has changed`], |
| 146 | + [new RegExp(`共享任务引用资产不可用:${REF} 不存在(?=\\s*$)`), (_, ref) => `${SHARED_REF}${ref} doesn't exist`], |
130 | 147 | [/共享任务引用资产不可用:最多引用 (\d+) 个资产(?=\s*$)/, (_, n) => `${SHARED_REF}a task can reference up to ${n} assets`], |
131 | 148 | [/共享任务引用资产不可用:直接引用列表与共享版本不一致(?=\s*$)/, () => `${SHARED_REF}the referenced assets don't match the shared version`], |
132 | | - // The cause after the last colon comes from the sharing path and goes through the usual passes. |
133 | | - [/共享任务引用资产不可用:共享 (\S+) 失败:/, (_, ref) => `${SHARED_REF}failed to share ${ref}: `], |
134 | 149 | ]; |
| 150 | + // Text before the wrapped error (a caller's own prefix) still goes through the usual passes. |
| 151 | + function translateWrapped(text) { |
| 152 | + for (const [re, rep] of wrapped) { |
| 153 | + const m = re.exec(text); |
| 154 | + if (!m) continue; |
| 155 | + const head = translate(text.slice(0, m.index)); |
| 156 | + return head + (head && !/\s$/.test(head) ? ' ' : '') + text.slice(m.index).replace(re, rep); |
| 157 | + } |
| 158 | + return null; |
| 159 | + } |
135 | 160 | const cache = new Map(); |
136 | 161 |
|
137 | 162 | function translateExact(text) { |
138 | 163 | const trimmed = text && text.trim(); |
139 | 164 | return trimmed && Object.prototype.hasOwnProperty.call(dict, trimmed) ? text.replace(trimmed, dict[trimmed]) : text; |
140 | 165 | } |
141 | 166 |
|
| 167 | + // Output of translateWrapped can still hold user names in Chinese. Writing it back to a text |
| 168 | + // node fires the observer again, and that second pass must not run the fragment rules over it. |
| 169 | + const wrappedDone = new Set(); |
| 170 | + |
142 | 171 | function translate(text) { |
143 | | - if (!text || !HAN.test(text)) return text; |
| 172 | + if (!text || !HAN.test(text) || wrappedDone.has(text)) return text; |
144 | 173 | const hit = cache.get(text); |
145 | 174 | if (hit !== undefined) return hit; |
146 | 175 | const trimmed = text.trim(); |
147 | 176 | let out; |
| 177 | + const whole = Object.prototype.hasOwnProperty.call(dict, trimmed) ? null : translateWrapped(text); |
| 178 | + if (whole !== null) { |
| 179 | + if (cache.size > 20000) { cache.clear(); wrappedDone.clear(); } |
| 180 | + cache.set(text, whole); |
| 181 | + if (HAN.test(whole)) wrappedDone.add(whole); |
| 182 | + return whole; |
| 183 | + } |
148 | 184 | if (Object.prototype.hasOwnProperty.call(dict, trimmed)) { |
149 | 185 | out = text.replace(trimmed, dict[trimmed]); |
150 | 186 | } else { |
151 | | - out = text; |
152 | | - for (const [re, rep] of wrapped) out = out.replace(re, rep); |
153 | | - if (fragmentRE) out = out.replace(fragmentRE, m => dict[m]); |
| 187 | + out = fragmentRE ? text.replace(fragmentRE, m => dict[m]) : text; |
154 | 188 | if (sentenceRE && HAN.test(out)) out = out.replace(sentenceRE, m => dict[m]); |
155 | 189 | for (const [re, rep] of patterns) out = out.replace(re, rep); |
156 | 190 | } |
|
0 commit comments