i18n.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import { MAX_FILE_BYTES, DOC_KINDS, extensionList, joinList } from "#office/contract.ts";
  2. import { PROFILE } from "#office/profile.ts";
  3. const FORMAT_NAMES = { docx: "Word", xlsx: "Excel", pptx: "PowerPoint", pdf: "PDF" } as const;
  4. const formats = DOC_KINDS.map((kind) => FORMAT_NAMES[kind]);
  5. const has = (kind: (typeof DOC_KINDS)[number]) => DOC_KINDS.includes(kind);
  6. const passage = has("docx") || has("pdf");
  7. /** What the user can select, e.g. "any passage, cell or slide". */
  8. const selectable = {
  9. en: `any ${joinList(
  10. [
  11. ...(passage ? ["passage"] : []),
  12. ...(has("xlsx") ? ["cell"] : []),
  13. ...(has("pptx") ? ["slide"] : []),
  14. ],
  15. "en",
  16. "or",
  17. )}`,
  18. zh: joinList(
  19. [
  20. ...(passage ? ["段落"] : []),
  21. ...(has("xlsx") ? ["单元格"] : []),
  22. ...(has("pptx") ? ["幻灯片"] : []),
  23. ],
  24. "zh-CN",
  25. "or",
  26. ),
  27. };
  28. /** The panel title as the start of a Chinese sentence: "Office 文档", or "PDF " before Chinese text. */
  29. const zhTitle = PROFILE.title["zh-CN"].replace(/[\x20-\x7e]$/, "$& ");
  30. const en = {
  31. title: PROFILE.title.en,
  32. openTitle: "Open a document",
  33. openFile: "Open file",
  34. emptyTitle: `Open your ${joinList(formats)} file`,
  35. emptyDescription: `Read alongside your conversation and discuss ${selectable.en}.`,
  36. emptyCaption: `Choose a local file · ${extensionList("en", "or")}`,
  37. importing: "Importing…",
  38. importTooLarge: `Choose a file no larger than ${MAX_FILE_BYTES / 1024 / 1024} MiB.`,
  39. recent: "Recent",
  40. switchDocument: "Switch document",
  41. pagesAndOutline: "Pages and outline",
  42. outlineOnly: "Outline",
  43. pages: "Pages",
  44. slides: "Slides",
  45. outline: "Outline",
  46. find: "Find",
  47. findPlaceholder: "Find in document",
  48. findCount: (index: number, total: number) => `${index} of ${total}`,
  49. noResults: "No results",
  50. previousMatch: "Previous match (⇧Enter)",
  51. nextMatch: "Next match (Enter)",
  52. closeFind: "Close (Esc)",
  53. more: "More",
  54. fitWidth: "Fit width",
  55. fitPage: "Fit page",
  56. actualSize: "Actual size",
  57. reload: "Reload from disk",
  58. previousPage: "Previous page",
  59. nextPage: "Next page",
  60. pageNumber: "Page number",
  61. ofPages: (count: number) => `of ${count}`,
  62. zoomIn: "Zoom in (⌘+)",
  63. zoomOut: "Zoom out (⌘−)",
  64. fit: "Fit",
  65. addToChat: "Add to chat",
  66. addShort: "Add",
  67. addToChatHint: "Attach this to your next message (⌘/Ctrl+Enter)",
  68. added: "Added to chat",
  69. ask: "Ask",
  70. askPlaceholder: "Ask about this…",
  71. send: "Send",
  72. cancel: "Cancel",
  73. sendKey: "send",
  74. cancelKey: "cancel",
  75. copy: "Copy",
  76. copied: "Copied",
  77. agent: "Agent",
  78. agentOf: (index: number, total: number) => `Agent · ${index} of ${total}`,
  79. nextTarget: "Next",
  80. moreOn: (count: number, label: string) => (count === 1 ? `1 more on ${label}` : `${count} more`),
  81. close: "Close",
  82. changed: "This file changed on disk.",
  83. reloadShort: "Reload",
  84. dismiss: "Dismiss",
  85. openAnother: "Open another file",
  86. sidePanel: "Open in side panel",
  87. meta: {
  88. pdf: (n: number) => (n === 1 ? "1 page" : `${n} pages`),
  89. pptx: (n: number) => (n === 1 ? "1 slide" : `${n} slides`),
  90. xlsx: (n: number) => (n === 1 ? "1 sheet" : `${n} sheets`),
  91. docx: (_n: number) => "",
  92. },
  93. errors: {
  94. generic: ["Couldn’t open this document", ""],
  95. password: [
  96. "This PDF is password protected",
  97. `${PROFILE.title.en} can’t open encrypted files yet.`,
  98. ],
  99. not_found: ["This file no longer exists", "It may have been moved or deleted."],
  100. file_too_large: ["This file is too large to open", ""],
  101. corrupt_file: [
  102. "This file appears to be damaged",
  103. `${PROFILE.title.en} couldn’t read its contents.`,
  104. ],
  105. unsupported_type: [
  106. "This kind of file isn’t supported",
  107. `${PROFILE.title.en} opens ${extensionList()} files.`,
  108. ],
  109. path_outside_workspace: ["This file is outside the workspace", ""],
  110. } as Record<string, [string, string]>,
  111. sendFailed: "The message was not sent.",
  112. contextFailed: "ZCode did not accept the reference.",
  113. where: {
  114. page: (n: string) => `Page ${n}`,
  115. pages: (a: string, b: string) => `Pages ${a}–${b}`,
  116. pageLower: (n: string) => `page ${n}`,
  117. slide: (n: string) => `Slide ${n}`,
  118. slides: (a: string, b: string) => `Slides ${a}–${b}`,
  119. slideLower: (n: string) => `slide ${n}`,
  120. shape: (n: string) => `Slide ${n} · Shape`,
  121. paragraph: (n: string) => `Paragraph ${n}`,
  122. paragraphs: (a: string, b: string) => `Paragraphs ${a}–${b}`,
  123. paragraphLower: (n: string) => `paragraph ${n}`,
  124. table: (n: string) => `Table ${n}`,
  125. cell: (n: string, row: string, col: string) => `Table ${n} · R${row}C${col}`,
  126. },
  127. };
  128. const zh: typeof en = {
  129. title: PROFILE.title["zh-CN"],
  130. openTitle: "打开文档",
  131. openFile: "打开文件",
  132. emptyTitle: `打开你的 ${joinList(formats, "zh-CN")} 文件`,
  133. emptyDescription: `边看边聊,选中${selectable.zh}即可带入对话。`,
  134. emptyCaption: `选择本地文件 · ${extensionList("zh-CN", "or")}`,
  135. importing: "正在导入…",
  136. importTooLarge: `请选择不超过 ${MAX_FILE_BYTES / 1024 / 1024} MiB 的文件。`,
  137. recent: "最近",
  138. switchDocument: "切换文档",
  139. pagesAndOutline: "页面与目录",
  140. outlineOnly: "目录",
  141. pages: "页面",
  142. slides: "幻灯片",
  143. outline: "目录",
  144. find: "查找",
  145. findPlaceholder: "在文档中查找",
  146. findCount: (index, total) => `${index} / ${total}`,
  147. noResults: "无结果",
  148. previousMatch: "上一个(⇧Enter)",
  149. nextMatch: "下一个(Enter)",
  150. closeFind: "关闭(Esc)",
  151. more: "更多",
  152. fitWidth: "适应宽度",
  153. fitPage: "适应页面",
  154. actualSize: "实际大小",
  155. reload: "从磁盘重新加载",
  156. previousPage: "上一页",
  157. nextPage: "下一页",
  158. pageNumber: "页码",
  159. ofPages: (count) => `/ ${count}`,
  160. zoomIn: "放大(⌘+)",
  161. zoomOut: "缩小(⌘−)",
  162. fit: "适应",
  163. addToChat: "添加到对话",
  164. addShort: "添加",
  165. addToChatHint: "附加到下一条消息(⌘/Ctrl+Enter)",
  166. added: "已添加到对话",
  167. ask: "提问",
  168. askPlaceholder: "就这里提问…",
  169. send: "发送",
  170. cancel: "取消",
  171. sendKey: "发送",
  172. cancelKey: "取消",
  173. copy: "复制",
  174. copied: "已复制",
  175. agent: "Agent",
  176. agentOf: (index, total) => `Agent · 第 ${index} 处,共 ${total} 处`,
  177. nextTarget: "下一处",
  178. moreOn: (count, label) => (count === 1 ? `${label}还有 1 处` : `还有 ${count} 处`),
  179. close: "关闭",
  180. changed: "文件已在磁盘上更改。",
  181. reloadShort: "重新加载",
  182. dismiss: "忽略",
  183. openAnother: "打开其他文件",
  184. sidePanel: "在侧栏打开",
  185. meta: {
  186. pdf: (n) => `${n} 页`,
  187. pptx: (n) => `${n} 张幻灯片`,
  188. xlsx: (n) => `${n} 个工作表`,
  189. docx: () => "",
  190. },
  191. errors: {
  192. generic: ["无法打开该文档", ""],
  193. password: ["该 PDF 受密码保护", `${zhTitle}暂不支持打开加密文件。`],
  194. not_found: ["该文件已不存在", "它可能已被移动或删除。"],
  195. file_too_large: ["文件太大,无法打开", ""],
  196. corrupt_file: ["该文件似乎已损坏", `${zhTitle}无法读取其内容。`],
  197. unsupported_type: ["不支持这种文件", `${zhTitle}可以打开 ${extensionList("zh-CN")} 文件。`],
  198. path_outside_workspace: ["该文件不在工作区内", ""],
  199. },
  200. sendFailed: "消息未发送。",
  201. contextFailed: "ZCode 未接受该引用。",
  202. where: {
  203. page: (n) => `第 ${n} 页`,
  204. pages: (a, b) => `第 ${a}–${b} 页`,
  205. pageLower: (n) => `第 ${n} 页`,
  206. slide: (n) => `第 ${n} 张幻灯片`,
  207. slides: (a, b) => `第 ${a}–${b} 张幻灯片`,
  208. slideLower: (n) => `第 ${n} 张幻灯片`,
  209. shape: (n) => `第 ${n} 张幻灯片 · 形状`,
  210. paragraph: (n) => `第 ${n} 段`,
  211. paragraphs: (a, b) => `第 ${a}–${b} 段`,
  212. paragraphLower: (n) => `第 ${n} 段`,
  213. table: (n) => `表格 ${n}`,
  214. cell: (n, row, col) => `表格 ${n} · R${row}C${col}`,
  215. },
  216. };
  217. export type Strings = typeof en;
  218. export const strings = (locale: string | undefined): Strings =>
  219. locale?.toLowerCase().startsWith("zh") ? zh : en;
  220. /** A locator as people say it: `page:3` → "Page 3", `Plan!C2:D2` → "Plan · C2:D2". */
  221. export function describeAt(at: string, t: Strings, lower = false): string {
  222. const w = t.where;
  223. let match: RegExpExecArray | null;
  224. if ((match = /^page:(\d+)(?:-(\d+))?$/.exec(at)))
  225. return match[2]
  226. ? w.pages(match[1], match[2])
  227. : lower
  228. ? w.pageLower(match[1])
  229. : w.page(match[1]);
  230. if ((match = /^slide:(\d+)\/shape:/.exec(at))) return w.shape(match[1]);
  231. if ((match = /^slide:(\d+)(?:-(\d+))?$/.exec(at)))
  232. return match[2]
  233. ? w.slides(match[1], match[2])
  234. : lower
  235. ? w.slideLower(match[1])
  236. : w.slide(match[1]);
  237. if ((match = /^p:(\d+)(?:-(\d+))?$/.exec(at)))
  238. return match[2]
  239. ? w.paragraphs(match[1], match[2])
  240. : lower
  241. ? w.paragraphLower(match[1])
  242. : w.paragraph(match[1]);
  243. if ((match = /^t:(\d+)\/r(\d+)c(\d+)$/.exec(at))) return w.cell(match[1], match[2], match[3]);
  244. if ((match = /^t:(\d+)$/.exec(at))) return w.table(match[1]);
  245. // Spreadsheet: Sheet!A1[:B2], with an optionally quoted sheet name.
  246. const bang = at.lastIndexOf("!");
  247. if (bang > 0)
  248. return `${at
  249. .slice(0, bang)
  250. .replace(/^'(.*)'$/, "$1")
  251. .replace(/''/g, "'")} · ${at.slice(bang + 1)}`;
  252. return at.replace(/^'(.*)'$/, "$1");
  253. }