@antelopejs/dms-lang 0.0.7 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (245) hide show
  1. package/README.md +12 -6
  2. package/dist/pages/actions.d.ts +7 -0
  3. package/dist/pages/actions.js +15 -0
  4. package/dist/pages/actions.js.map +1 -0
  5. package/dist/pages/component-action.d.ts +2 -0
  6. package/dist/pages/component-action.js +10 -0
  7. package/dist/pages/component-action.js.map +1 -0
  8. package/dist/pages/header-actions.d.ts +9 -0
  9. package/dist/pages/header-actions.js +146 -0
  10. package/dist/pages/header-actions.js.map +1 -0
  11. package/dist/pages/index.d.ts +6 -0
  12. package/dist/pages/index.js +6 -0
  13. package/dist/pages/index.js.map +1 -1
  14. package/dist/pages/language.d.ts +10 -0
  15. package/dist/pages/language.js +89 -0
  16. package/dist/pages/language.js.map +1 -0
  17. package/dist/pages/languages-table.d.ts +2 -0
  18. package/dist/pages/languages-table.js +120 -0
  19. package/dist/pages/languages-table.js.map +1 -0
  20. package/dist/pages/module.d.ts +7 -1
  21. package/dist/pages/module.js +28 -2
  22. package/dist/pages/module.js.map +1 -1
  23. package/dist/pages/overview.d.ts +2 -1
  24. package/dist/pages/overview.js +83 -8
  25. package/dist/pages/overview.js.map +1 -1
  26. package/dist/pages/page-actions.d.ts +6 -0
  27. package/dist/pages/page-actions.js +10 -0
  28. package/dist/pages/page-actions.js.map +1 -0
  29. package/dist/pages/quick-actions.d.ts +1 -0
  30. package/dist/pages/quick-actions.js +56 -0
  31. package/dist/pages/quick-actions.js.map +1 -0
  32. package/dist/pages/translations.d.ts +7 -1
  33. package/dist/pages/translations.js +47 -10
  34. package/dist/pages/translations.js.map +1 -1
  35. package/dist/pages/workspace-guard.d.ts +1 -0
  36. package/dist/pages/workspace-guard.js +35 -0
  37. package/dist/pages/workspace-guard.js.map +1 -0
  38. package/dist/pages/workspace.d.ts +18 -0
  39. package/dist/pages/workspace.js +162 -0
  40. package/dist/pages/workspace.js.map +1 -0
  41. package/dist/pages/workspaces.d.ts +14 -0
  42. package/dist/pages/workspaces.js +88 -0
  43. package/dist/pages/workspaces.js.map +1 -0
  44. package/dist/routes/publicLocales.d.ts +3 -2
  45. package/dist/routes/publicLocales.js +39 -18
  46. package/dist/routes/publicLocales.js.map +1 -1
  47. package/dist/routes/translations.d.ts +59 -63
  48. package/dist/routes/translations.js +400 -119
  49. package/dist/routes/translations.js.map +1 -1
  50. package/dist/utils/announce.d.ts +9 -0
  51. package/dist/utils/announce.js +29 -0
  52. package/dist/utils/announce.js.map +1 -0
  53. package/dist/utils/blocks.d.ts +8 -0
  54. package/dist/utils/blocks.js +213 -0
  55. package/dist/utils/blocks.js.map +1 -0
  56. package/dist/utils/body.d.ts +1 -0
  57. package/dist/utils/body.js +21 -0
  58. package/dist/utils/body.js.map +1 -0
  59. package/dist/utils/cache.d.ts +3 -0
  60. package/dist/utils/cache.js +32 -0
  61. package/dist/utils/cache.js.map +1 -0
  62. package/dist/utils/catalog.d.ts +7 -0
  63. package/dist/utils/catalog.js +62 -0
  64. package/dist/utils/catalog.js.map +1 -0
  65. package/dist/utils/constants.d.ts +6 -0
  66. package/dist/utils/constants.js +10 -0
  67. package/dist/utils/constants.js.map +1 -0
  68. package/dist/utils/coverage.d.ts +2 -0
  69. package/dist/utils/coverage.js +11 -0
  70. package/dist/utils/coverage.js.map +1 -0
  71. package/dist/utils/dialogs.d.ts +7 -0
  72. package/dist/utils/dialogs.js +103 -0
  73. package/dist/utils/dialogs.js.map +1 -0
  74. package/dist/utils/errors.d.ts +40 -0
  75. package/dist/utils/errors.js +60 -0
  76. package/dist/utils/errors.js.map +1 -0
  77. package/dist/utils/export.d.ts +17 -0
  78. package/dist/utils/export.js +129 -0
  79. package/dist/utils/export.js.map +1 -0
  80. package/dist/utils/json.d.ts +10 -1
  81. package/dist/utils/json.js +127 -19
  82. package/dist/utils/json.js.map +1 -1
  83. package/dist/utils/key-stores.d.ts +3 -0
  84. package/dist/utils/key-stores.js +24 -0
  85. package/dist/utils/key-stores.js.map +1 -0
  86. package/dist/utils/keys.d.ts +23 -0
  87. package/dist/utils/keys.js +252 -0
  88. package/dist/utils/keys.js.map +1 -0
  89. package/dist/utils/languages.d.ts +27 -0
  90. package/dist/utils/languages.js +121 -0
  91. package/dist/utils/languages.js.map +1 -0
  92. package/dist/utils/locale-codes.d.ts +5 -0
  93. package/dist/utils/locale-codes.js +56 -0
  94. package/dist/utils/locale-codes.js.map +1 -0
  95. package/dist/utils/locale-discovery.d.ts +1 -0
  96. package/dist/utils/locale-discovery.js +19 -1
  97. package/dist/utils/locale-discovery.js.map +1 -1
  98. package/dist/utils/message-check.d.ts +1 -0
  99. package/dist/utils/message-check.js +93 -0
  100. package/dist/utils/message-check.js.map +1 -0
  101. package/dist/utils/message-syntax.d.ts +6 -0
  102. package/dist/utils/message-syntax.js +38 -0
  103. package/dist/utils/message-syntax.js.map +1 -0
  104. package/dist/utils/namespaces.d.ts +12 -0
  105. package/dist/utils/namespaces.js +52 -0
  106. package/dist/utils/namespaces.js.map +1 -0
  107. package/dist/utils/notices.d.ts +4 -0
  108. package/dist/utils/notices.js +60 -0
  109. package/dist/utils/notices.js.map +1 -0
  110. package/dist/utils/own.d.ts +2 -3
  111. package/dist/utils/own.js +17 -57
  112. package/dist/utils/own.js.map +1 -1
  113. package/dist/utils/public-url.d.ts +8 -0
  114. package/dist/utils/public-url.js +87 -0
  115. package/dist/utils/public-url.js.map +1 -0
  116. package/dist/utils/registry.d.ts +3 -1
  117. package/dist/utils/registry.js +26 -10
  118. package/dist/utils/registry.js.map +1 -1
  119. package/dist/utils/resolve.d.ts +18 -15
  120. package/dist/utils/resolve.js +98 -151
  121. package/dist/utils/resolve.js.map +1 -1
  122. package/dist/utils/review.d.ts +5 -0
  123. package/dist/utils/review.js +64 -0
  124. package/dist/utils/review.js.map +1 -0
  125. package/dist/utils/snippets.d.ts +7 -0
  126. package/dist/utils/snippets.js +129 -0
  127. package/dist/utils/snippets.js.map +1 -0
  128. package/dist/utils/summary.d.ts +23 -0
  129. package/dist/utils/summary.js +65 -0
  130. package/dist/utils/summary.js.map +1 -0
  131. package/dist/utils/tree.d.ts +2 -17
  132. package/dist/utils/tree.js +21 -126
  133. package/dist/utils/tree.js.map +1 -1
  134. package/dist/utils/workspaces.d.ts +45 -15
  135. package/dist/utils/workspaces.js +256 -164
  136. package/dist/utils/workspaces.js.map +1 -1
  137. package/dist/validation/key.schema.d.ts +71 -0
  138. package/dist/validation/key.schema.js +67 -0
  139. package/dist/validation/key.schema.js.map +1 -0
  140. package/dist/validation/query.schema.d.ts +17 -0
  141. package/dist/validation/query.schema.js +44 -0
  142. package/dist/validation/query.schema.js.map +1 -0
  143. package/dist/validation/validate.d.ts +2 -0
  144. package/dist/validation/validate.js +24 -0
  145. package/dist/validation/validate.js.map +1 -0
  146. package/dist/validation/workspace.schema.d.ts +145 -0
  147. package/dist/validation/workspace.schema.js +83 -0
  148. package/dist/validation/workspace.schema.js.map +1 -0
  149. package/frontend-vue/app/build/components/KeyAddModal.vue +340 -0
  150. package/frontend-vue/app/build/components/KeyEditorDrawer.vue +951 -0
  151. package/frontend-vue/app/build/components/KeyRenameModal.vue +172 -0
  152. package/frontend-vue/app/build/components/LanguageAddModal.vue +199 -0
  153. package/frontend-vue/app/build/components/LanguagePicker.vue +233 -0
  154. package/frontend-vue/app/build/components/LocaleTile.vue +52 -0
  155. package/frontend-vue/app/build/components/PlaceholderText.vue +42 -0
  156. package/frontend-vue/app/build/components/ShortcutKbds.vue +22 -0
  157. package/frontend-vue/app/build/components/WorkspaceCreateModal.vue +193 -0
  158. package/frontend-vue/app/build/components/WorkspaceRenameModal.vue +107 -0
  159. package/frontend-vue/app/build/components/form/FormFieldRow.vue +70 -0
  160. package/frontend-vue/app/build/components/form/FormRows.vue +25 -0
  161. package/frontend-vue/app/build/components/form/WorkspaceDetailsFields.vue +44 -0
  162. package/frontend-vue/app/build/components/matrix/MatrixCell.vue +488 -0
  163. package/frontend-vue/app/build/components/matrix/MatrixColumns.vue +177 -0
  164. package/frontend-vue/app/build/components/matrix/MatrixGroupHeader.vue +57 -0
  165. package/frontend-vue/app/build/components/matrix/MatrixKeyCell.vue +83 -0
  166. package/frontend-vue/app/build/components/matrix/MatrixToolbar.vue +167 -0
  167. package/frontend-vue/app/build/components/queue/QueueActions.vue +60 -0
  168. package/frontend-vue/app/build/components/queue/QueueDone.vue +185 -0
  169. package/frontend-vue/app/build/components/queue/QueueEditor.vue +374 -0
  170. package/frontend-vue/app/build/components/queue/QueueHeader.vue +162 -0
  171. package/frontend-vue/app/build/components/queue/QueueUpNext.vue +61 -0
  172. package/frontend-vue/app/build/composables/useCellEditor.ts +324 -0
  173. package/frontend-vue/app/build/composables/useCellMoves.ts +155 -0
  174. package/frontend-vue/app/build/composables/useCellSaves.ts +339 -0
  175. package/frontend-vue/app/build/composables/useKeyPanel.ts +284 -0
  176. package/frontend-vue/app/build/composables/useKeySaves.ts +280 -0
  177. package/frontend-vue/app/build/composables/useMatrixFilters.ts +213 -0
  178. package/frontend-vue/app/build/composables/useMatrixRows.ts +104 -0
  179. package/frontend-vue/app/build/composables/useMatrixView.ts +181 -0
  180. package/frontend-vue/app/build/composables/useQueueContext.ts +31 -0
  181. package/frontend-vue/app/build/composables/useQueueEditing.ts +53 -0
  182. package/frontend-vue/app/build/composables/useQueueOrphans.ts +54 -0
  183. package/frontend-vue/app/build/composables/useQueueSave.ts +290 -0
  184. package/frontend-vue/app/build/composables/useQueueShortcuts.ts +106 -0
  185. package/frontend-vue/app/build/composables/useQueueToasts.ts +99 -0
  186. package/frontend-vue/app/components/LanguageNamespaces.vue +77 -0
  187. package/frontend-vue/app/components/NamespaceCoverage.vue +222 -0
  188. package/frontend-vue/app/components/TranslationMatrix.vue +809 -0
  189. package/frontend-vue/app/components/TranslationQueue.vue +212 -0
  190. package/frontend-vue/app/components/WorkspaceDangerZone.vue +81 -0
  191. package/frontend-vue/app/components/WorkspaceGeneral.vue +232 -0
  192. package/frontend-vue/app/components/WorkspaceList.vue +454 -0
  193. package/frontend-vue/app/components/WorkspaceScope.vue +384 -0
  194. package/frontend-vue/app/composables/translation/useFirstRun.ts +8 -0
  195. package/frontend-vue/app/composables/translation/useI18nWorkspaces.ts +221 -133
  196. package/frontend-vue/app/composables/translation/useKeyActions.ts +224 -0
  197. package/frontend-vue/app/composables/translation/useLangFormat.ts +10 -0
  198. package/frontend-vue/app/composables/translation/useLangLinks.ts +80 -0
  199. package/frontend-vue/app/composables/translation/useLanguageActions.ts +159 -0
  200. package/frontend-vue/app/composables/translation/useLanguageQueue.ts +232 -0
  201. package/frontend-vue/app/composables/translation/useLocaleNames.ts +166 -0
  202. package/frontend-vue/app/composables/translation/useMutationToast.ts +51 -17
  203. package/frontend-vue/app/composables/translation/useQueueDrafts.ts +115 -0
  204. package/frontend-vue/app/composables/translation/useQueueSession.ts +182 -0
  205. package/frontend-vue/app/composables/translation/useServedConfirm.ts +152 -0
  206. package/frontend-vue/app/composables/translation/useWorkspaceActions.ts +259 -0
  207. package/frontend-vue/app/composables/translation/useWorkspaceContext.ts +46 -15
  208. package/frontend-vue/app/composables/translation/useWorkspaceFlat.ts +322 -70
  209. package/frontend-vue/app/composables/translation/useWorkspaceNameCheck.ts +55 -0
  210. package/frontend-vue/app/composables/translation/useWorkspaceRoute.ts +129 -0
  211. package/frontend-vue/app/config/shortcuts-registry.ts +72 -0
  212. package/frontend-vue/app/types/lang.ts +130 -0
  213. package/frontend-vue/app/utils/boundedCache.ts +22 -0
  214. package/frontend-vue/app/utils/download.ts +16 -0
  215. package/frontend-vue/app/utils/keySaveErrors.ts +61 -0
  216. package/frontend-vue/app/utils/keySaves.ts +108 -0
  217. package/frontend-vue/app/utils/languages.ts +272 -0
  218. package/frontend-vue/app/utils/matrixExport.ts +47 -0
  219. package/frontend-vue/app/utils/queueDrafts.ts +115 -0
  220. package/frontend-vue/app/utils/queuePass.ts +145 -0
  221. package/frontend-vue/app/utils/queueView.ts +22 -0
  222. package/frontend-vue/app/utils/shortcuts.ts +75 -0
  223. package/frontend-vue/app/utils/translations.ts +756 -0
  224. package/frontend-vue/app/utils/workspaceBackup.ts +119 -0
  225. package/frontend-vue/app/utils/workspaces.ts +45 -0
  226. package/frontend-vue/dms.frontend.build.ts +5 -0
  227. package/frontend-vue/dms.frontend.ts +9 -20
  228. package/frontend-vue/i18n/locales/lang-en-GB.json +792 -69
  229. package/frontend-vue/i18n/locales/lang-fr-FR.json +792 -69
  230. package/frontend-vue/package.json +8 -6
  231. package/package.json +11 -8
  232. package/dist/utils/mutations.d.ts +0 -3
  233. package/dist/utils/mutations.js +0 -35
  234. package/dist/utils/mutations.js.map +0 -1
  235. package/frontend-vue/app/build/components/pages/lang/TranslationMatrix.vue +0 -284
  236. package/frontend-vue/app/components/LanguageAddModal.vue +0 -78
  237. package/frontend-vue/app/components/WorkspaceEndpoint.vue +0 -29
  238. package/frontend-vue/app/components/WorkspaceSelector.vue +0 -180
  239. package/frontend-vue/app/components/WorkspaceStatus.vue +0 -24
  240. package/frontend-vue/app/composables/translation/useLanguageCatalog.ts +0 -90
  241. package/frontend-vue/app/composables/translation/useLocaleMeta.ts +0 -18
  242. package/frontend-vue/app/composables/translation/useTranslationProgress.ts +0 -73
  243. package/frontend-vue/app/custom-pages/lang/overview.vue +0 -397
  244. package/frontend-vue/app/custom-pages/lang/translations.vue +0 -498
  245. package/frontend-vue/app/plugins/workspace-sidebar-widget.ts +0 -12
@@ -0,0 +1,756 @@
1
+ import { baseCompile, CompileErrorCodes } from '@intlify/message-compiler'
2
+ import type {
3
+ FlatTranslationRow,
4
+ FlatTranslations,
5
+ LanguageCoverage,
6
+ NamespaceCoverage,
7
+ } from '../types/lang'
8
+
9
+ export type MessagePartKind =
10
+ 'text' | 'placeholder' | 'literal' | 'linked' | 'pipe'
11
+
12
+ /** A piece of a vue-i18n message; `name` is the variable, literal text or linked key. */
13
+ export interface MessagePart {
14
+ kind: MessagePartKind
15
+ raw: string
16
+ name: string
17
+ start: number
18
+ }
19
+
20
+ export type MessageSyntaxKind =
21
+ | 'bareAt'
22
+ | 'unclosedBrace'
23
+ | 'strayBrace'
24
+ | 'emptyPlaceholder'
25
+ | 'invalidPlaceholder'
26
+ | 'unclosedLiteral'
27
+ | 'invalidEscape'
28
+ | 'emptyPlural'
29
+ | 'invalidSyntax'
30
+
31
+ export type MessageIssueKind =
32
+ MessageSyntaxKind | 'missing' | 'extra' | 'plural' | 'spaces'
33
+
34
+ /** `error`: vue-i18n cannot compile the message; `warning`: it shows, but wrong. */
35
+ export type MessageIssueSeverity = 'error' | 'warning'
36
+
37
+ interface MessageSyntaxError {
38
+ kind: MessageSyntaxKind
39
+ raw: string
40
+ start: number
41
+ }
42
+
43
+ export interface MessageScan {
44
+ parts: MessagePart[]
45
+ errors: MessageSyntaxError[]
46
+ }
47
+
48
+ interface ScanStep {
49
+ part?: MessagePart
50
+ error?: MessageSyntaxError
51
+ end: number
52
+ }
53
+
54
+ /**
55
+ * One problem of a message. `tokens` holds variable names (`missing`,
56
+ * `extra`) or the offending characters (syntax); `count` the number of them,
57
+ * or of plural forms for `plural`, whose base has `expected` forms.
58
+ */
59
+ export interface MessageIssue {
60
+ kind: MessageIssueKind
61
+ severity: MessageIssueSeverity
62
+ tokens: string[]
63
+ count: number
64
+ expected?: number
65
+ index: number
66
+ }
67
+
68
+ export type KeyPathConflictKind = 'exists' | 'namespace' | 'leafParent'
69
+
70
+ export interface KeyPathConflict {
71
+ kind: KeyPathConflictKind
72
+ keys: string[]
73
+ }
74
+
75
+ /** The `t` of vue-i18n: key, named values and the count that picks the plural. */
76
+ export type MessageTranslate = (
77
+ key: string,
78
+ named: Record<string, unknown>,
79
+ plural: number,
80
+ ) => string
81
+
82
+ const FULL_COVERAGE = 100
83
+ export const NAMESPACE_SEPARATOR = '.'
84
+ const OPEN_BRACE = '{'
85
+ const CLOSE_BRACE = '}'
86
+ const QUOTE = "'"
87
+ const LINKED_MARK = '@'
88
+ export const PLURAL_SEPARATOR = '|'
89
+ /** What the scanner reads as a variable, to highlight and rename it. */
90
+ const PLACEHOLDER_NAME = /^(?:[A-Za-z_][\w$-]*|\d+)$/
91
+ const LITERAL_PATTERN = /\s*'((?:[^'\\\n]|\\.)*)'\s*\}/y
92
+ const LITERAL_ESCAPE = /\\u([0-9a-fA-F]{4})|\\(.)/g
93
+ const LINKED_PATTERN = /@(?:\.[A-Za-z]+)?:(?:\(([^()\s]+)\)|([\w.$-]+)|(?=\{))/y
94
+ const IMPLICIT_PLURAL_NAMES = ['count', 'n']
95
+ const LEADING_SPACE = /^\s/
96
+ const TRAILING_SPACE = /\s$/
97
+ const KEY_PATH_PATTERN = /^[\w-]+(\.[\w-]+)*$/
98
+ const MAX_KEY_SEGMENT_LENGTH = 128
99
+ const FORBIDDEN_KEY_SEGMENTS = new Set([
100
+ '__proto__',
101
+ 'prototype',
102
+ 'constructor',
103
+ ])
104
+
105
+ /** The i18n key of the sentence that explains each kind of issue. */
106
+ export const MESSAGE_ISSUE_LABELS: Record<MessageIssueKind, string> = {
107
+ missing: 'dms_lang.message_issue.missing',
108
+ extra: 'dms_lang.message_issue.extra',
109
+ plural: 'dms_lang.message_issue.plural',
110
+ spaces: 'dms_lang.message_issue.spaces',
111
+ bareAt: 'dms_lang.message_issue.bare_at',
112
+ unclosedBrace: 'dms_lang.message_issue.unclosed_brace',
113
+ strayBrace: 'dms_lang.message_issue.stray_brace',
114
+ emptyPlaceholder: 'dms_lang.message_issue.empty_placeholder',
115
+ invalidPlaceholder: 'dms_lang.message_issue.invalid_placeholder',
116
+ unclosedLiteral: 'dms_lang.message_issue.unclosed_literal',
117
+ invalidEscape: 'dms_lang.message_issue.invalid_escape',
118
+ emptyPlural: 'dms_lang.message_issue.empty_plural',
119
+ invalidSyntax: 'dms_lang.message_issue.invalid_syntax',
120
+ }
121
+
122
+ const SYNTAX_ESCAPES: Partial<Record<MessageIssueKind, string>> = {
123
+ bareAt: "{'@'}",
124
+ unclosedBrace: "{'{'}",
125
+ strayBrace: "{'}'}",
126
+ }
127
+
128
+ export const HIGH_COVERAGE = 90
129
+ export const MID_COVERAGE = 60
130
+
131
+ export const EMPTY_FLAT: FlatTranslations = {
132
+ defaultLocale: '',
133
+ locales: [],
134
+ rows: [],
135
+ localeProgress: {},
136
+ localeMissing: {},
137
+ totalKeys: 0,
138
+ baseMissing: 0,
139
+ incompleteKeys: 0,
140
+ }
141
+
142
+ /** No value, or a text made of spaces only: the server stores neither. */
143
+ export function isLocaleValueMissing(value: unknown): boolean {
144
+ if (typeof value === 'string') return value.trim() === ''
145
+ return value === undefined || value === null
146
+ }
147
+
148
+ /**
149
+ * Whether a key has no text in a language, counting the value an installed
150
+ * module gives it in the project files: that one shows on screen too.
151
+ */
152
+ export function isRowValueMissing(
153
+ row: FlatTranslationRow,
154
+ locale: string,
155
+ ): boolean {
156
+ return (
157
+ isLocaleValueMissing(row.values[locale]) &&
158
+ isLocaleValueMissing(row.inherited?.[locale])
159
+ )
160
+ }
161
+
162
+ export function stringValue(value: unknown): string {
163
+ return value === undefined || value === null ? '' : String(value)
164
+ }
165
+
166
+ export function namespaceOf(key: string): string {
167
+ const index = key.indexOf(NAMESPACE_SEPARATOR)
168
+ return index < 0 ? key : key.slice(0, index)
169
+ }
170
+
171
+ function textPart(raw: string, start: number): MessagePart {
172
+ return { kind: 'text', raw, name: raw, start }
173
+ }
174
+
175
+ function syntaxStep(
176
+ kind: MessageSyntaxKind,
177
+ raw: string,
178
+ start: number,
179
+ end = start + raw.length,
180
+ ): ScanStep {
181
+ return {
182
+ part: textPart(raw, start),
183
+ error: { kind, raw, start },
184
+ end,
185
+ }
186
+ }
187
+
188
+ function unescapeLiteral(content: string): string {
189
+ return content.replace(
190
+ LITERAL_ESCAPE,
191
+ (_match, code?: string, char?: string) =>
192
+ code ? String.fromCharCode(Number.parseInt(code, 16)) : (char ?? ''),
193
+ )
194
+ }
195
+
196
+ function readLiteral(text: string, start: number): ScanStep {
197
+ LITERAL_PATTERN.lastIndex = start + 1
198
+ const match = LITERAL_PATTERN.exec(text)
199
+ if (!match) {
200
+ const close = text.indexOf(CLOSE_BRACE, start)
201
+ const raw = text.slice(start, close < 0 ? text.length : close + 1)
202
+ return syntaxStep('unclosedLiteral', raw, start)
203
+ }
204
+ const end = LITERAL_PATTERN.lastIndex
205
+ const raw = text.slice(start, end)
206
+ return {
207
+ part: { kind: 'literal', raw, name: unescapeLiteral(match[1]), start },
208
+ end,
209
+ }
210
+ }
211
+
212
+ function readPlaceholder(text: string, start: number): ScanStep {
213
+ const close = text.indexOf(CLOSE_BRACE, start + 1)
214
+ const reopen = text.indexOf(OPEN_BRACE, start + 1)
215
+ if (close < 0 || (reopen >= 0 && reopen < close))
216
+ return syntaxStep('unclosedBrace', OPEN_BRACE, start)
217
+ const raw = text.slice(start, close + 1)
218
+ const name = raw.slice(1, -1).trim()
219
+ if (!name) return syntaxStep('emptyPlaceholder', raw, start)
220
+ if (!PLACEHOLDER_NAME.test(name))
221
+ return syntaxStep('invalidPlaceholder', raw, start)
222
+ return { part: { kind: 'placeholder', raw, name, start }, end: close + 1 }
223
+ }
224
+
225
+ function readBrace(text: string, start: number): ScanStep {
226
+ const isLiteral = text
227
+ .slice(start + 1)
228
+ .trimStart()
229
+ .startsWith(QUOTE)
230
+ return isLiteral ? readLiteral(text, start) : readPlaceholder(text, start)
231
+ }
232
+
233
+ function readLinked(text: string, start: number): ScanStep {
234
+ LINKED_PATTERN.lastIndex = start
235
+ const match = LINKED_PATTERN.exec(text)
236
+ if (!match) return syntaxStep('bareAt', LINKED_MARK, start)
237
+ const raw = match[0]
238
+ return {
239
+ part: { kind: 'linked', raw, name: match[1] ?? match[2] ?? '', start },
240
+ end: start + raw.length,
241
+ }
242
+ }
243
+
244
+ function readPipe(_text: string, start: number): ScanStep {
245
+ return {
246
+ part: { kind: 'pipe', raw: PLURAL_SEPARATOR, name: '', start },
247
+ end: start + 1,
248
+ }
249
+ }
250
+
251
+ function readStrayBrace(_text: string, start: number): ScanStep {
252
+ return syntaxStep('strayBrace', CLOSE_BRACE, start)
253
+ }
254
+
255
+ const SPECIAL_READERS: Record<
256
+ string,
257
+ (text: string, start: number) => ScanStep
258
+ > = {
259
+ [OPEN_BRACE]: readBrace,
260
+ [CLOSE_BRACE]: readStrayBrace,
261
+ [LINKED_MARK]: readLinked,
262
+ [PLURAL_SEPARATOR]: readPipe,
263
+ }
264
+
265
+ /**
266
+ * Splits a message written in the vue-i18n syntax into text, `{name}`
267
+ * variables, `{'@'}` literals, `@:key` links and `|` plural separators, and
268
+ * marks the bare characters {@link escapeMessage} writes as literals. Joining
269
+ * the parts' `raw` gives the text back. Whether a message compiles is the
270
+ * vue-i18n compiler's call (see {@link checkMessage}).
271
+ */
272
+ export function scanMessage(message: unknown): MessageScan {
273
+ const text = stringValue(message)
274
+ const scan: MessageScan = { parts: [], errors: [] }
275
+ let textStart = 0
276
+ let index = 0
277
+ while (index < text.length) {
278
+ const reader = SPECIAL_READERS[text[index]]
279
+ if (!reader) {
280
+ index += 1
281
+ continue
282
+ }
283
+ if (index > textStart)
284
+ scan.parts.push(textPart(text.slice(textStart, index), textStart))
285
+ const step = reader(text, index)
286
+ if (step.part) scan.parts.push(step.part)
287
+ if (step.error) scan.errors.push(step.error)
288
+ index = step.end
289
+ textStart = index
290
+ }
291
+ if (text.length > textStart)
292
+ scan.parts.push(textPart(text.slice(textStart), textStart))
293
+ return scan
294
+ }
295
+
296
+ function namesOf(scan: MessageScan): string[] {
297
+ const names = scan.parts
298
+ .filter((part) => part.kind === 'placeholder')
299
+ .map((part) => part.name)
300
+ return [...new Set(names)]
301
+ }
302
+
303
+ function formCount(scan: MessageScan): number {
304
+ return scan.parts.filter((part) => part.kind === 'pipe').length + 1
305
+ }
306
+
307
+ /** The rule the server applies to a new key path: `checkout.coupon.invalid`. */
308
+ export function isValidKeyPath(path: string): boolean {
309
+ if (!KEY_PATH_PATTERN.test(path)) return false
310
+ return path
311
+ .split(NAMESPACE_SEPARATOR)
312
+ .every(
313
+ (segment) =>
314
+ segment.length <= MAX_KEY_SEGMENT_LENGTH &&
315
+ !FORBIDDEN_KEY_SEGMENTS.has(segment),
316
+ )
317
+ }
318
+
319
+ function parentPaths(path: string): string[] {
320
+ const segments = path.split(NAMESPACE_SEPARATOR)
321
+ return segments
322
+ .slice(1)
323
+ .map((_segment, index) =>
324
+ segments.slice(0, index + 1).join(NAMESPACE_SEPARATOR),
325
+ )
326
+ }
327
+
328
+ /**
329
+ * Why a new key path cannot be written next to existing keys: it is one of
330
+ * them (`exists`), it holds some of them (`namespace`, a key cannot also be a
331
+ * namespace) or one of its parents is a key (`leafParent`). `keys` lists the
332
+ * keys in the way.
333
+ */
334
+ export function keyPathConflict(
335
+ path: string,
336
+ existingKeys: string[],
337
+ ): KeyPathConflict | null {
338
+ if (existingKeys.includes(path)) return { kind: 'exists', keys: [path] }
339
+ const children = existingKeys.filter((key) =>
340
+ key.startsWith(`${path}${NAMESPACE_SEPARATOR}`),
341
+ )
342
+ if (children.length) return { kind: 'namespace', keys: children }
343
+ const parents = new Set(parentPaths(path))
344
+ const leaves = existingKeys.filter((key) => parents.has(key))
345
+ if (leaves.length) return { kind: 'leafParent', keys: leaves }
346
+ return null
347
+ }
348
+
349
+ /**
350
+ * The path of what follows a key in its namespace, in the order of the keys:
351
+ * `cart.badge` after `cart.title`, `cart.items` before `cart.items.one`.
352
+ * Undefined when the key comes last: a key put back there is appended.
353
+ */
354
+ export function nextSiblingPath(
355
+ keys: string[],
356
+ path: string,
357
+ ): string | undefined {
358
+ const next = keys[keys.indexOf(path) + 1]
359
+ if (!next || !keys.includes(path)) return undefined
360
+ const segments = path.split(NAMESPACE_SEPARATOR)
361
+ const parent = segments.slice(0, -1)
362
+ const nextSegments = next.split(NAMESPACE_SEPARATOR)
363
+ const isSibling =
364
+ nextSegments.length >= segments.length &&
365
+ parent.every((segment, index) => nextSegments[index] === segment)
366
+ if (!isSibling) return undefined
367
+ return nextSegments.slice(0, segments.length).join(NAMESPACE_SEPARATOR)
368
+ }
369
+
370
+ export function placeholdersOf(text: unknown): string[] {
371
+ return namesOf(scanMessage(text))
372
+ }
373
+
374
+ type CompileErrorName = keyof typeof CompileErrorCodes
375
+
376
+ /** What each error of the vue-i18n compiler means to a translator; any other is `invalidSyntax`. */
377
+ const COMPILE_ERROR_KINDS: Partial<
378
+ Record<CompileErrorName, MessageSyntaxKind>
379
+ > = {
380
+ INVALID_TOKEN_IN_PLACEHOLDER: 'invalidPlaceholder',
381
+ UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER: 'unclosedLiteral',
382
+ UNKNOWN_ESCAPE_SEQUENCE: 'invalidEscape',
383
+ INVALID_UNICODE_ESCAPE_SEQUENCE: 'invalidEscape',
384
+ UNBALANCED_CLOSING_BRACE: 'strayBrace',
385
+ UNTERMINATED_CLOSING_BRACE: 'unclosedBrace',
386
+ NOT_ALLOW_NEST_PLACEHOLDER: 'unclosedBrace',
387
+ EMPTY_PLACEHOLDER: 'emptyPlaceholder',
388
+ INVALID_LINKED_FORMAT: 'bareAt',
389
+ UNEXPECTED_EMPTY_LINKED_KEY: 'bareAt',
390
+ UNEXPECTED_EMPTY_LINKED_MODIFIER: 'bareAt',
391
+ MUST_HAVE_MESSAGES_IN_PLURAL: 'emptyPlural',
392
+ }
393
+
394
+ const COMPILE_ERROR_NAMES = new Map<number, CompileErrorName>(
395
+ Object.entries(CompileErrorCodes).map(([name, code]) => [
396
+ code,
397
+ name as CompileErrorName,
398
+ ]),
399
+ )
400
+
401
+ const FIXED_TOKENS: Partial<Record<MessageSyntaxKind, string>> = {
402
+ bareAt: LINKED_MARK,
403
+ unclosedBrace: OPEN_BRACE,
404
+ strayBrace: CLOSE_BRACE,
405
+ emptyPlural: PLURAL_SEPARATOR,
406
+ }
407
+
408
+ interface CompileIssue {
409
+ kind: MessageSyntaxKind
410
+ offset: number
411
+ }
412
+
413
+ function compileErrors(text: string): CompileIssue[] {
414
+ const errors: CompileIssue[] = []
415
+ baseCompile(text, {
416
+ onError: (error) => {
417
+ const name = COMPILE_ERROR_NAMES.get(error.code)
418
+ errors.push({
419
+ kind: (name && COMPILE_ERROR_KINDS[name]) || 'invalidSyntax',
420
+ offset: error.location?.start.offset ?? 0,
421
+ })
422
+ },
423
+ })
424
+ return errors
425
+ }
426
+
427
+ /** The `{…}` around an offset, up to its `}` (or the end of the text). */
428
+ function placeholderAt(text: string, offset: number): string {
429
+ const start = Math.max(text.lastIndexOf(OPEN_BRACE, offset), 0)
430
+ const close = text.indexOf(CLOSE_BRACE, offset)
431
+ return text.slice(start, close < 0 ? text.length : close + 1)
432
+ }
433
+
434
+ /** The `}` closing the placeholder an unclosed brace opens, when one does before the next `{`. */
435
+ function closingBraceOf(text: string, offset: number): number {
436
+ const close = text.indexOf(CLOSE_BRACE, offset)
437
+ const reopen = text.indexOf(OPEN_BRACE, offset)
438
+ return reopen >= 0 && reopen < close ? -1 : close
439
+ }
440
+
441
+ /**
442
+ * vue-i18n reads `{a b}` as a brace never closed then a stray one: to a
443
+ * translator it is one variable whose name is not valid.
444
+ */
445
+ function mergeBrokenPlaceholders(
446
+ text: string,
447
+ errors: CompileIssue[],
448
+ ): CompileIssue[] {
449
+ const closings = new Set<number>()
450
+ const merged = errors.map((error) => {
451
+ if (error.kind !== 'unclosedBrace') return error
452
+ const close = closingBraceOf(text, error.offset)
453
+ if (close < 0) return error
454
+ closings.add(close)
455
+ return { ...error, kind: 'invalidPlaceholder' as const }
456
+ })
457
+ return merged.filter(
458
+ (error) => error.kind !== 'strayBrace' || !closings.has(error.offset),
459
+ )
460
+ }
461
+
462
+ const PLACEHOLDER_KINDS = new Set<MessageSyntaxKind>([
463
+ 'invalidPlaceholder',
464
+ 'emptyPlaceholder',
465
+ 'unclosedLiteral',
466
+ 'invalidEscape',
467
+ ])
468
+
469
+ function tokenOf(text: string, issue: CompileIssue): string {
470
+ const fixed = FIXED_TOKENS[issue.kind]
471
+ if (fixed) return fixed
472
+ return PLACEHOLDER_KINDS.has(issue.kind)
473
+ ? placeholderAt(text, issue.offset)
474
+ : text.charAt(issue.offset)
475
+ }
476
+
477
+ /** A generic compile error only counts when nothing more precise explains the text. */
478
+ function withoutGeneric(errors: CompileIssue[]): CompileIssue[] {
479
+ const precise = errors.filter((error) => error.kind !== 'invalidSyntax')
480
+ return precise.length ? precise : errors
481
+ }
482
+
483
+ /** The errors the vue-i18n compiler finds in a message, one issue per kind. */
484
+ function syntaxIssues(text: string): MessageIssue[] {
485
+ const errors = withoutGeneric(
486
+ mergeBrokenPlaceholders(text, compileErrors(text)),
487
+ ).sort((a, b) => a.offset - b.offset)
488
+ const byKind = new Map<MessageSyntaxKind, CompileIssue[]>()
489
+ for (const error of errors)
490
+ byKind.set(error.kind, [...(byKind.get(error.kind) ?? []), error])
491
+ return [...byKind].map(([kind, found]) => ({
492
+ kind,
493
+ severity: 'error',
494
+ tokens: [...new Set(found.map((error) => tokenOf(text, error)))],
495
+ count: found.length,
496
+ index: found[0].offset,
497
+ }))
498
+ }
499
+
500
+ function tokenIssue(kind: MessageIssueKind, tokens: string[]): MessageIssue[] {
501
+ if (!tokens.length) return []
502
+ return [
503
+ { kind, severity: 'warning', tokens, count: tokens.length, index: -1 },
504
+ ]
505
+ }
506
+
507
+ function pluralIssue(base: MessageScan, value: MessageScan): MessageIssue[] {
508
+ const expected = formCount(base)
509
+ const actual = formCount(value)
510
+ if (expected === actual) return []
511
+ return [
512
+ {
513
+ kind: 'plural',
514
+ severity: 'warning',
515
+ tokens: [],
516
+ count: actual,
517
+ expected,
518
+ index: -1,
519
+ },
520
+ ]
521
+ }
522
+
523
+ function edgeSpaces(text: string): [boolean, boolean] {
524
+ return [LEADING_SPACE.test(text), TRAILING_SPACE.test(text)]
525
+ }
526
+
527
+ /**
528
+ * Spaces or line breaks at the start or the end of a text the base does not
529
+ * have there (any of them when there is no base): they show in the app.
530
+ */
531
+ function spacesIssue(base: unknown, value: unknown): MessageIssue[] {
532
+ const [leading, trailing] = edgeSpaces(stringValue(value))
533
+ const [baseLeading, baseTrailing] = edgeSpaces(stringValue(base))
534
+ const count =
535
+ Number(leading && !baseLeading) + Number(trailing && !baseTrailing)
536
+ if (!count) return []
537
+ return [{ kind: 'spaces', severity: 'warning', tokens: [], count, index: -1 }]
538
+ }
539
+
540
+ function comparisonIssues(
541
+ base: MessageScan,
542
+ value: MessageScan,
543
+ ): MessageIssue[] {
544
+ const baseNames = namesOf(base)
545
+ const valueNames = namesOf(value)
546
+ const implicit = formCount(value) > 1 ? IMPLICIT_PLURAL_NAMES : []
547
+ const known = new Set([...baseNames, ...implicit])
548
+ return [
549
+ ...tokenIssue(
550
+ 'missing',
551
+ baseNames.filter((name) => !valueNames.includes(name)),
552
+ ),
553
+ ...tokenIssue(
554
+ 'extra',
555
+ valueNames.filter((name) => !known.has(name)),
556
+ ),
557
+ ...pluralIssue(base, value),
558
+ ]
559
+ }
560
+
561
+ /**
562
+ * What is wrong with a translation, read as a vue-i18n message: syntax that
563
+ * does not compile (a bare `@`, an unclosed brace…), base variables it leaves
564
+ * out, variables the base does not have, a different number of plural forms
565
+ * and spaces at its ends the base does not have. Pass an empty base to check
566
+ * the base text itself (syntax and spaces at its ends only). An empty value
567
+ * has no issue: it is missing.
568
+ */
569
+ export function checkMessage(base: unknown, value: unknown): MessageIssue[] {
570
+ if (isLocaleValueMissing(value)) return []
571
+ const scan = scanMessage(value)
572
+ const issues = [
573
+ ...syntaxIssues(stringValue(value)),
574
+ ...spacesIssue(base, value),
575
+ ]
576
+ if (isLocaleValueMissing(base)) return issues
577
+ return [...issues, ...comparisonIssues(scanMessage(base), scan)]
578
+ }
579
+
580
+ export function hasBlockingIssue(issues: MessageIssue[]): boolean {
581
+ return issues.some((issue) => issue.severity === 'error')
582
+ }
583
+
584
+ /**
585
+ * Whether the text of a language needs another look, as the server tells:
586
+ * it has an issue (the rule of {@link checkMessage}), or it was translated
587
+ * from an older base text. The rule of every "issues" count.
588
+ */
589
+ export function needsReview(row: FlatTranslationRow, locale: string): boolean {
590
+ return (
591
+ (row.issues?.includes(locale) ?? false) ||
592
+ (row.outdated?.includes(locale) ?? false)
593
+ )
594
+ }
595
+
596
+ /** The sentence that explains an issue, in the interface language. */
597
+ export function messageIssueText(
598
+ issue: MessageIssue,
599
+ t: MessageTranslate,
600
+ ): string {
601
+ const isNamed = issue.kind === 'missing' || issue.kind === 'extra'
602
+ return t(
603
+ MESSAGE_ISSUE_LABELS[issue.kind],
604
+ {
605
+ tokens: isNamed ? formatTokens(issue.tokens) : issue.tokens.join(' '),
606
+ count: issue.count,
607
+ expected: issue.expected ?? 0,
608
+ fix: SYNTAX_ESCAPES[issue.kind] ?? '',
609
+ },
610
+ issue.count,
611
+ )
612
+ }
613
+
614
+ /** Writes bare `@` and lone braces as vue-i18n literals: `{'@'}`. */
615
+ export function escapeMessage(message: unknown): string {
616
+ const scan = scanMessage(message)
617
+ const errorKinds = new Map(
618
+ scan.errors.map((error) => [error.start, error.kind]),
619
+ )
620
+ return scan.parts
621
+ .map((part) => {
622
+ const kind = part.kind === 'text' ? errorKinds.get(part.start) : undefined
623
+ return (kind && SYNTAX_ESCAPES[kind]) || part.raw
624
+ })
625
+ .join('')
626
+ }
627
+
628
+ /** Renames a variable everywhere it appears in a message. */
629
+ export function replacePlaceholder(
630
+ message: unknown,
631
+ from: string,
632
+ to: string,
633
+ ): string {
634
+ return scanMessage(message)
635
+ .parts.map((part) =>
636
+ part.kind === 'placeholder' && part.name === from ? `{${to}}` : part.raw,
637
+ )
638
+ .join('')
639
+ }
640
+
641
+ /**
642
+ * The share of `total` that is translated, in whole percent rounded down, so
643
+ * that one missing text keeps it under 100 (200 of 201 reads 99). Nothing to
644
+ * translate reads 100. The rule the server rounds every coverage with.
645
+ */
646
+ export function coveragePercent(translated: number, total: number): number {
647
+ if (total <= 0) return FULL_COVERAGE
648
+ return Math.floor((Math.max(translated, 0) * FULL_COVERAGE) / total)
649
+ }
650
+
651
+ export function coverageTone(
652
+ coverage: number,
653
+ ): 'success' | 'warning' | 'error' {
654
+ if (coverage >= HIGH_COVERAGE) return 'success'
655
+ if (coverage >= MID_COVERAGE) return 'warning'
656
+ return 'error'
657
+ }
658
+
659
+ export function translatedCount(
660
+ rows: FlatTranslationRow[],
661
+ locale: string,
662
+ ): number {
663
+ return rows.filter((row) => !isRowValueMissing(row, locale)).length
664
+ }
665
+
666
+ /** The coverage of each language, from the figures the server keeps. */
667
+ export function languageCoverage(
668
+ flat: FlatTranslations,
669
+ nameOf: (code: string) => string,
670
+ ): LanguageCoverage[] {
671
+ return flat.locales.map((code) => {
672
+ const missing = flat.localeMissing[code] ?? 0
673
+ const translated = flat.totalKeys - missing
674
+ return {
675
+ code,
676
+ name: nameOf(code),
677
+ isBase: code === flat.defaultLocale,
678
+ coverage: coveragePercent(translated, flat.totalKeys),
679
+ translated,
680
+ missing,
681
+ }
682
+ })
683
+ }
684
+
685
+ function groupByNamespace(
686
+ rows: FlatTranslationRow[],
687
+ ): Map<string, FlatTranslationRow[]> {
688
+ const groups = new Map<string, FlatTranslationRow[]>()
689
+ for (const row of rows) {
690
+ const namespace = namespaceOf(row.key)
691
+ const group = groups.get(namespace)
692
+ if (group) group.push(row)
693
+ else groups.set(namespace, [row])
694
+ }
695
+ return groups
696
+ }
697
+
698
+ export function namespaceCoverage(
699
+ rows: FlatTranslationRow[],
700
+ locales: string[],
701
+ ): NamespaceCoverage[] {
702
+ return [...groupByNamespace(rows)].map(([namespace, group]) => {
703
+ const coverage: Record<string, number> = {}
704
+ const missing: Record<string, number> = {}
705
+ for (const locale of locales) {
706
+ const translated = translatedCount(group, locale)
707
+ coverage[locale] = coveragePercent(translated, group.length)
708
+ missing[locale] = group.length - translated
709
+ }
710
+ return { namespace, keys: group.length, coverage, missing }
711
+ })
712
+ }
713
+
714
+ /** Where a key put back before `before` (a sibling key or namespace) goes: the end when none is there. */
715
+ function insertionIndex(rows: FlatTranslationRow[], before?: string): number {
716
+ if (before === undefined) return rows.length
717
+ const index = rows.findIndex(
718
+ (entry) =>
719
+ entry.key === before ||
720
+ entry.key.startsWith(`${before}${NAMESPACE_SEPARATOR}`),
721
+ )
722
+ return index < 0 ? rows.length : index
723
+ }
724
+
725
+ /**
726
+ * The translations with one row put in place: replaced, added (before the
727
+ * `before` sibling, else at the end), or taken out when there is none (the
728
+ * key is gone). The other rows are kept as they are.
729
+ */
730
+ export function withRow(
731
+ flat: FlatTranslations,
732
+ key: string,
733
+ row: FlatTranslationRow | undefined,
734
+ before?: string,
735
+ ): FlatTranslations {
736
+ const index = flat.rows.findIndex((entry) => entry.key === key)
737
+ const rows = [...flat.rows]
738
+ if (row && index >= 0) rows[index] = row
739
+ else if (row) rows.splice(insertionIndex(rows, before), 0, row)
740
+ else if (index >= 0) rows.splice(index, 1)
741
+ return { ...flat, rows }
742
+ }
743
+
744
+ export const COVERAGE_TEXT_CLASSES: Record<
745
+ 'success' | 'warning' | 'error',
746
+ string
747
+ > = {
748
+ success: 'text-success',
749
+ warning: 'text-warning',
750
+ error: 'text-error',
751
+ }
752
+
753
+ /** Placeholder names written as they appear in a text: `{name} {count}`. */
754
+ export function formatTokens(names: string[]): string {
755
+ return names.map((name) => `{${name}}`).join(' ')
756
+ }