@antelopejs/dms-lang 0.0.7 → 0.1.0

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 +8 -6
  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,130 @@
1
+ export type TranslationValue = string | number | boolean | null | undefined
2
+
3
+ export type WorkspaceKind = 'default' | 'modules' | 'added'
4
+
5
+ export type WorkspaceType = 'dms' | 'website' | 'mobile' | 'other'
6
+
7
+ export interface WorkspaceLocale {
8
+ code: string
9
+ name?: string
10
+ flag?: string
11
+ }
12
+
13
+ export interface WorkspaceSummary {
14
+ id: string
15
+ kind: WorkspaceKind
16
+ editable: boolean
17
+ defaultLocale: string
18
+ locales: WorkspaceLocale[]
19
+ description?: string
20
+ type?: WorkspaceType
21
+ /** Public URL of a language file, with a `{locale}` placeholder; added workspaces only. */
22
+ publicUrl?: string
23
+ }
24
+
25
+ /** A workspace as /workspaces lists it: its number of keys, of keys without base text and of texts. */
26
+ export interface ScopedWorkspace extends WorkspaceSummary {
27
+ totalKeys: number
28
+ baseMissing: number
29
+ translatedStrings: number
30
+ }
31
+
32
+ export interface WorkspaceDetails {
33
+ description?: string
34
+ type?: WorkspaceType
35
+ }
36
+
37
+ /** DMS permission ids guarding each kind of write, as /workspaces lists them. */
38
+ export interface WorkspacePermissionIds {
39
+ editTranslations: string
40
+ manageKeys: string
41
+ manageLanguages: string
42
+ createWorkspace: string
43
+ renameWorkspace: string
44
+ deleteWorkspace: string
45
+ editWorkspace: string
46
+ }
47
+
48
+ export type WorkspacePermission = keyof WorkspacePermissionIds
49
+
50
+ export interface WorkspaceStats extends WorkspaceSummary {
51
+ baseLocale: string
52
+ totalKeys: number
53
+ translatedStrings: number
54
+ missing: number
55
+ coverage: number
56
+ localeProgress: Record<string, number>
57
+ localeMissing: Record<string, number>
58
+ overrides: number
59
+ baseMissing: number
60
+ incompleteKeys: number
61
+ targets: number
62
+ }
63
+
64
+ export interface FlatTranslationRow {
65
+ key: string
66
+ values: Record<string, TranslationValue>
67
+ /** Value the installed modules give the key, in the project files workspace. */
68
+ inherited?: Record<string, TranslationValue>
69
+ /** The project files redefine this module key. */
70
+ overridden?: boolean
71
+ /** In the modules workspace, the values the project files redefine. */
72
+ overrides?: Record<string, string>
73
+ /** Languages translated from an older base text. */
74
+ outdated?: string[]
75
+ /** Languages whose text has an issue, as the server checks it. */
76
+ issues?: string[]
77
+ }
78
+
79
+ /** The figures of a workspace a write changes. */
80
+ export interface FlatCounts {
81
+ localeProgress: Record<string, number>
82
+ localeMissing: Record<string, number>
83
+ totalKeys: number
84
+ baseMissing: number
85
+ incompleteKeys: number
86
+ }
87
+
88
+ export interface FlatTranslations extends FlatCounts {
89
+ defaultLocale: string
90
+ locales: string[]
91
+ rows: FlatTranslationRow[]
92
+ }
93
+
94
+ /** What a write to one key answers: the row as the server now stores it (none once the key is gone) and the figures it changed. */
95
+ export interface KeyRowResponse {
96
+ row?: FlatTranslationRow
97
+ counts: FlatCounts
98
+ }
99
+
100
+ export interface LanguageCoverage {
101
+ code: string
102
+ name: string
103
+ isBase: boolean
104
+ coverage: number
105
+ translated: number
106
+ missing: number
107
+ }
108
+
109
+ export interface NamespaceCoverage {
110
+ namespace: string
111
+ keys: number
112
+ coverage: Record<string, number>
113
+ missing: Record<string, number>
114
+ }
115
+
116
+ /** One namespace of the overview heatmap: its keys and how many each target language lacks. */
117
+ export interface NamespaceRow {
118
+ namespace: string
119
+ keys: number
120
+ missing: Record<string, number>
121
+ }
122
+
123
+ /** The overview heatmap as /namespaces answers it: the targets and the namespaces, neediest first. */
124
+ export interface NamespaceMatrix {
125
+ locales: string[]
126
+ localeMissing: Record<string, number>
127
+ namespaces: NamespaceRow[]
128
+ }
129
+
130
+ export type TranslationFilter = 'all' | 'missing' | 'complete' | 'issues'
@@ -0,0 +1,22 @@
1
+ /** A cache keeping at most `limit` entries, the oldest one dropped first. */
2
+ export interface BoundedCache<T> {
3
+ get: (key: string) => T | undefined
4
+ has: (key: string) => boolean
5
+ set: (key: string, value: T) => T
6
+ }
7
+
8
+ /** A cache keeping at most `limit` entries: every code typed in a search adds one. */
9
+ export function boundedCache<T>(limit: number): BoundedCache<T> {
10
+ const entries = new Map<string, T>()
11
+ return {
12
+ get: (key) => entries.get(key),
13
+ has: (key) => entries.has(key),
14
+ set(key, value) {
15
+ entries.delete(key)
16
+ entries.set(key, value)
17
+ const oldest = entries.keys().next().value
18
+ if (entries.size > limit && oldest !== undefined) entries.delete(oldest)
19
+ return value
20
+ },
21
+ }
22
+ }
@@ -0,0 +1,16 @@
1
+ const JSON_INDENT = 2
2
+ const JSON_TYPE = 'application/json'
3
+ /** Long enough for the browser to start the download before the URL goes. */
4
+ const REVOKE_DELAY_MS = 1000
5
+
6
+ /** Downloads a text as a file, through the DMS `downloadFile`. */
7
+ export function downloadText(name: string, content: string, type: string) {
8
+ const url = URL.createObjectURL(new Blob([content], { type }))
9
+ downloadFile(url, name)
10
+ setTimeout(() => URL.revokeObjectURL(url), REVOKE_DELAY_MS)
11
+ }
12
+
13
+ /** Downloads data as an indented JSON file. */
14
+ export function downloadJson(name: string, data: unknown) {
15
+ downloadText(name, JSON.stringify(data, null, JSON_INDENT), JSON_TYPE)
16
+ }
@@ -0,0 +1,61 @@
1
+ import type { TranslationValue } from '../types/lang'
2
+
3
+ /** Why a key write was refused: changed elsewhere, deleted, a message that does not compile, or anything else. */
4
+ export type KeySaveFailureKind = 'conflict' | 'deleted' | 'invalid' | 'other'
5
+
6
+ export interface KeySaveFailure {
7
+ kind: KeySaveFailureKind
8
+ /** On a conflict, each language's text as the server now stores it. */
9
+ current: Record<string, TranslationValue>
10
+ /** On a refused message, one sentence per problem. */
11
+ details: string[]
12
+ }
13
+
14
+ interface KeySaveErrorBody {
15
+ current?: Record<string, TranslationValue>
16
+ details?: string[]
17
+ }
18
+
19
+ interface KeySaveError {
20
+ data?: KeySaveErrorBody | string | null
21
+ }
22
+
23
+ /** Translates an i18n key with its parameters, as `t` does. */
24
+ export type ApiMessageTranslate = (
25
+ key: string,
26
+ params: Record<string, unknown>,
27
+ ) => string
28
+
29
+ const FAILURE_KINDS: Record<string, KeySaveFailureKind> = {
30
+ conflict: 'conflict',
31
+ key_not_found: 'deleted',
32
+ invalid_message: 'invalid',
33
+ }
34
+
35
+ function bodyOf(error: unknown): KeySaveErrorBody {
36
+ const data = (error as KeySaveError | undefined)?.data
37
+ return typeof data === 'object' && data !== null ? data : {}
38
+ }
39
+
40
+ function kindOf(code: string): KeySaveFailureKind {
41
+ return Object.hasOwn(FAILURE_KINDS, code) ? FAILURE_KINDS[code] : 'other'
42
+ }
43
+
44
+ /**
45
+ * Reads a refused key write: the one place the module reads the server's
46
+ * error body for a key. Its details (`$dms_lang.errors.message_syntax.*`
47
+ * keys) come back translated.
48
+ */
49
+ export function keySaveFailure(
50
+ error: unknown,
51
+ translate: ApiMessageTranslate,
52
+ ): KeySaveFailure {
53
+ const body = bodyOf(error)
54
+ return {
55
+ kind: kindOf(serverErrorCode(error)),
56
+ current: body.current ?? {},
57
+ details: (body.details ?? []).map((detail) =>
58
+ resolveApiMessage(translate, detail),
59
+ ),
60
+ }
61
+ }
@@ -0,0 +1,108 @@
1
+ import type { TranslationValue } from '../types/lang'
2
+
3
+ /** The text a save expects to replace (`null`: no text); `undefined` sends no check. */
4
+ export type ExpectedText = string | null | undefined
5
+
6
+ /** Where a text being saved stands, as `DmsSaveStatus` shows it. */
7
+ export type SaveState = 'idle' | 'saving' | 'saved' | 'error'
8
+
9
+ /** What a write left behind for the next write of the same text. */
10
+ export interface WriteLink {
11
+ /** The text it sent (`null`: no text). */
12
+ sent: string | null
13
+ /** The text it expected to replace. */
14
+ expected: ExpectedText
15
+ /** Whether the server stored it. */
16
+ isLanded: boolean
17
+ }
18
+
19
+ const SAVE_ID_SEPARATOR = '\u0000'
20
+
21
+ /** One language of one key of one workspace: a text saved on its own. */
22
+ export function saveIdOf(workspace: string, key: string, code: string): string {
23
+ return [workspace, key, code].join(SAVE_ID_SEPARATOR)
24
+ }
25
+
26
+ /** The prefix every save id of a key starts with. */
27
+ export function saveIdPrefix(workspace: string, key: string): string {
28
+ return saveIdOf(workspace, key, '')
29
+ }
30
+
31
+ /** A stored value as the server compares it: a text, else no text (`null`). */
32
+ export function savedText(value: TranslationValue): string | null {
33
+ return typeof value === 'string' && !isLocaleValueMissing(value)
34
+ ? value
35
+ : null
36
+ }
37
+
38
+ /** What a typed text stores: a text made of spaces only is no text. */
39
+ export function storedText(text: string): string | null {
40
+ return text.trim() === '' ? null : text
41
+ }
42
+
43
+ /**
44
+ * What a write expects once the write before it did not land: a text the
45
+ * server never stored cannot be expected, the one that write expected can.
46
+ */
47
+ export function carriedExpected(
48
+ expected: ExpectedText,
49
+ previous: WriteLink | undefined,
50
+ ): ExpectedText {
51
+ if (!previous || previous.isLanded || expected !== previous.sent)
52
+ return expected
53
+ return previous.expected
54
+ }
55
+
56
+ /** A text queued behind the earlier writes of the same text. */
57
+ export interface QueuedText {
58
+ id: string
59
+ text: string
60
+ expected: ExpectedText
61
+ }
62
+
63
+ interface SentTexts<T> {
64
+ outcome: T
65
+ expected: ExpectedText[]
66
+ }
67
+
68
+ /**
69
+ * Runs a write once every earlier write of its texts settled, each text
70
+ * expecting what the write before it carried over (`carriedExpected`), and
71
+ * leaves the link the next write of each text starts from.
72
+ */
73
+ export function queueTexts<T>(
74
+ links: Map<string, Promise<WriteLink>>,
75
+ texts: QueuedText[],
76
+ send: (expected: ExpectedText[]) => Promise<T>,
77
+ isLanded: (outcome: T) => boolean,
78
+ ): Promise<T> {
79
+ const run = Promise.all(texts.map((entry) => links.get(entry.id))).then(
80
+ async (earlier): Promise<SentTexts<T>> => {
81
+ const expected = texts.map((entry, index) =>
82
+ carriedExpected(entry.expected, earlier[index]),
83
+ )
84
+ return { expected, outcome: await send(expected) }
85
+ },
86
+ )
87
+ texts.forEach((entry, index) =>
88
+ links.set(
89
+ entry.id,
90
+ run.then((sent) => ({
91
+ sent: storedText(entry.text),
92
+ expected: sent.expected[index],
93
+ isLanded: isLanded(sent.outcome),
94
+ })),
95
+ ),
96
+ )
97
+ return run.then((sent) => sent.outcome)
98
+ }
99
+
100
+ /** A record with one entry set, or removed when `value` is `undefined`. */
101
+ export function withEntry<T>(
102
+ record: Record<string, T>,
103
+ id: string,
104
+ value: T | undefined,
105
+ ): Record<string, T> {
106
+ const { [id]: _previous, ...rest } = record
107
+ return value === undefined ? rest : { ...rest, [id]: value }
108
+ }
@@ -0,0 +1,272 @@
1
+ /** Languages offered first when adding one; any other BCP 47 code can be typed. */
2
+ export const SUGGESTED_LANGUAGE_CODES = [
3
+ 'en',
4
+ 'en-GB',
5
+ 'en-US',
6
+ 'fr',
7
+ 'fr-FR',
8
+ 'fr-CA',
9
+ 'de',
10
+ 'de-DE',
11
+ 'es',
12
+ 'es-ES',
13
+ 'es-MX',
14
+ 'it',
15
+ 'it-IT',
16
+ 'pt',
17
+ 'pt-PT',
18
+ 'pt-BR',
19
+ 'nl',
20
+ 'nl-NL',
21
+ 'pl',
22
+ 'sv',
23
+ 'da',
24
+ 'fi',
25
+ 'nb',
26
+ 'cs',
27
+ 'sk',
28
+ 'hu',
29
+ 'ro',
30
+ 'bg',
31
+ 'el',
32
+ 'hr',
33
+ 'sl',
34
+ 'et',
35
+ 'lv',
36
+ 'lt',
37
+ 'ru',
38
+ 'uk',
39
+ 'tr',
40
+ 'ar',
41
+ 'he',
42
+ 'fa',
43
+ 'hi',
44
+ 'bn',
45
+ 'ja',
46
+ 'ko',
47
+ 'zh-CN',
48
+ 'zh-TW',
49
+ 'th',
50
+ 'vi',
51
+ 'id',
52
+ 'ms',
53
+ ]
54
+
55
+ /**
56
+ * Regional variants and further languages a search finds ("suisse" →
57
+ * fr-CH, de-CH, it-CH) without lengthening the list shown before typing.
58
+ */
59
+ export const SEARCHABLE_LANGUAGE_CODES = [
60
+ 'en-AU',
61
+ 'en-CA',
62
+ 'en-IE',
63
+ 'en-IN',
64
+ 'en-NZ',
65
+ 'en-ZA',
66
+ 'fr-BE',
67
+ 'fr-CH',
68
+ 'fr-LU',
69
+ 'de-AT',
70
+ 'de-CH',
71
+ 'it-CH',
72
+ 'nl-BE',
73
+ 'es-AR',
74
+ 'es-CO',
75
+ 'es-US',
76
+ 'es-419',
77
+ 'zh-HK',
78
+ 'ca',
79
+ 'eu',
80
+ 'gl',
81
+ 'ga',
82
+ 'is',
83
+ 'sq',
84
+ 'sr',
85
+ 'bs',
86
+ 'mk',
87
+ 'ka',
88
+ 'hy',
89
+ 'az',
90
+ 'kk',
91
+ 'ur',
92
+ 'ta',
93
+ 'te',
94
+ 'mr',
95
+ 'sw',
96
+ 'af',
97
+ 'fil',
98
+ ]
99
+
100
+ const RTL_LANGUAGES = new Set([
101
+ 'ar',
102
+ 'he',
103
+ 'fa',
104
+ 'ur',
105
+ 'ps',
106
+ 'sd',
107
+ 'ug',
108
+ 'yi',
109
+ 'dv',
110
+ ])
111
+
112
+ const SUBTAG_SEPARATOR = /[-_]/
113
+ const LANGUAGE_SUBTAG_RE = /^[a-z]{2,3}$/i
114
+ const WORD_SEPARATOR = /[\s()(),,、·'’-]+/
115
+ const DIACRITICS = /\p{Diacritic}/gu
116
+
117
+ /** How well a language answers a search, best first; absent when it does not. */
118
+ const MATCH_RANKS = {
119
+ exactCode: 0,
120
+ codePrefix: 1,
121
+ namePrefix: 2,
122
+ wordPrefix: 3,
123
+ } as const
124
+
125
+ /** A language a picker offers, with every name it can be searched by. */
126
+ export interface LanguageCandidate {
127
+ code: string
128
+ names: string[]
129
+ }
130
+
131
+ /** A language a picker shows: its name with the region, in its own language and in the interface's. */
132
+ export interface LanguageOption extends LanguageCandidate {
133
+ name: string
134
+ localName: string
135
+ /** Not known to the browser: offered because the search spells a valid code. */
136
+ isUnknown: boolean
137
+ }
138
+
139
+ export function languageOf(code: string): string {
140
+ return code.split(SUBTAG_SEPARATOR)[0]
141
+ }
142
+
143
+ export function lastSubtag(code: string): string {
144
+ return code.split(SUBTAG_SEPARATOR).at(-1) ?? code
145
+ }
146
+
147
+ /**
148
+ * The canonical BCP 47 form of a code (`fr_FR` → `fr-FR`, `zh-hant-tw` →
149
+ * `zh-Hant-TW`), or null when it is not one.
150
+ */
151
+ export function canonicalLocale(code: string): string | null {
152
+ try {
153
+ return Intl.getCanonicalLocales(code.trim().replaceAll('_', '-'))[0] ?? null
154
+ } catch {
155
+ return null
156
+ }
157
+ }
158
+
159
+ /**
160
+ * The code a search spells, when it is a valid BCP 47 code with a real
161
+ * language subtag (two or three letters): `french` is a valid tag, not a code.
162
+ */
163
+ export function typedLocaleCode(search: string): string | null {
164
+ const code = canonicalLocale(search)
165
+ if (!code || !LANGUAGE_SUBTAG_RE.test(languageOf(code))) return null
166
+ return code
167
+ }
168
+
169
+ /** A text as a search compares it: lower case, without accents ("Français" → "francais"). */
170
+ function folded(text: string): string {
171
+ return text.normalize('NFD').replace(DIACRITICS, '').toLocaleLowerCase()
172
+ }
173
+
174
+ function wordsOf(text: string): string[] {
175
+ return text.split(WORD_SEPARATOR).filter(Boolean)
176
+ }
177
+
178
+ /** Whether each word of the search starts a word of the name ("united kingdom", "royaume-uni"). */
179
+ function startsWords(name: string, needleWords: string[]): boolean {
180
+ const words = wordsOf(name)
181
+ return needleWords.every((needle) =>
182
+ words.some((word) => word.startsWith(needle)),
183
+ )
184
+ }
185
+
186
+ /** Whether a search is written as a code with subtags (`pt-BR`, `zh_Hant`), valid or not. */
187
+ export function hasSubtags(search: string): boolean {
188
+ return SUBTAG_SEPARATOR.test(search)
189
+ }
190
+
191
+ function isSpelledCode(search: string): boolean {
192
+ return hasSubtags(search) && typedLocaleCode(search) !== null
193
+ }
194
+
195
+ /**
196
+ * How well a language answers one needle. A code (`fr-fr`) is never read as
197
+ * words: "fr" would start "Français".
198
+ */
199
+ function rankFor(candidate: LanguageCandidate, needle: string) {
200
+ const code = candidate.code.toLowerCase()
201
+ if (code === needle) return MATCH_RANKS.exactCode
202
+ if (code.startsWith(needle)) return MATCH_RANKS.codePrefix
203
+ const search = folded(needle)
204
+ const names = candidate.names.map(folded)
205
+ if (names.some((name) => name.startsWith(search)))
206
+ return MATCH_RANKS.namePrefix
207
+ if (isSpelledCode(needle)) return undefined
208
+ const needleWords = wordsOf(search)
209
+ if (!needleWords.length) return undefined
210
+ if (names.some((name) => startsWords(name, needleWords)))
211
+ return MATCH_RANKS.wordPrefix
212
+ return undefined
213
+ }
214
+
215
+ function matchRank(candidate: LanguageCandidate, needles: string[]) {
216
+ const ranks = needles
217
+ .map((needle) => rankFor(candidate, needle))
218
+ .filter((rank) => rank !== undefined)
219
+ return ranks.length ? Math.min(...ranks) : undefined
220
+ }
221
+
222
+ /** What a search is matched with: as typed, and as the code it spells (`fra` → `fr`). */
223
+ function needlesOf(search: string): string[] {
224
+ const typed = search.trim().toLocaleLowerCase()
225
+ if (!typed) return []
226
+ const code = typedLocaleCode(typed)?.toLowerCase()
227
+ return code && code !== typed ? [typed, code] : [typed]
228
+ }
229
+
230
+ /**
231
+ * The languages a search finds, best first: the exact code, codes starting
232
+ * with it, then names (native, local, English) starting with it, then names
233
+ * where each word of the search starts a word ("united kingdom", "brasil").
234
+ * Case and accents are ignored; a search inside a word ("it" in "British")
235
+ * finds nothing. Ties keep the order given.
236
+ */
237
+ export function searchLanguages<T extends LanguageCandidate>(
238
+ candidates: T[],
239
+ search: string,
240
+ ): T[] {
241
+ const needles = needlesOf(search)
242
+ if (!needles.length) return candidates
243
+ return candidates
244
+ .map((candidate, index) => ({
245
+ candidate,
246
+ index,
247
+ rank: matchRank(candidate, needles),
248
+ }))
249
+ .filter((entry) => entry.rank !== undefined)
250
+ .sort((a, b) => (a.rank ?? 0) - (b.rank ?? 0) || a.index - b.index)
251
+ .map((entry) => entry.candidate)
252
+ }
253
+
254
+ /** Whether another code of the list names the same language (`pt-BR`, `pt-PT`). */
255
+ export function sharesLanguage(code: string, codes: string[]): boolean {
256
+ const language = languageOf(code).toLowerCase()
257
+ return codes.some(
258
+ (other) => other !== code && languageOf(other).toLowerCase() === language,
259
+ )
260
+ }
261
+
262
+ /** The direction a language is written in, as the `dir` attribute takes it. */
263
+ export type TextDirection = 'rtl' | 'ltr'
264
+
265
+ export function isRightToLeft(code: string): boolean {
266
+ return RTL_LANGUAGES.has(languageOf(code).toLowerCase())
267
+ }
268
+
269
+ /** The `dir` of a text in a language: right to left for Arabic, Hebrew, Persian… */
270
+ export function textDirection(code: string): TextDirection {
271
+ return isRightToLeft(code) ? 'rtl' : 'ltr'
272
+ }
@@ -0,0 +1,47 @@
1
+ import type { FlatTranslationRow } from '../types/lang'
2
+
3
+ const BYTE_ORDER_MARK = ''
4
+ const LINE_BREAK = '\r\n'
5
+ const SEPARATOR = ','
6
+ /** Spreadsheets run a cell starting with one of these as a formula. */
7
+ const FORMULA_START = /^[=+\-@\t\r]/
8
+ const FORMULA_GUARD = "'"
9
+
10
+ function csvCell(value: unknown): string {
11
+ const text = stringValue(value)
12
+ const safe = FORMULA_START.test(text) ? `${FORMULA_GUARD}${text}` : text
13
+ return `"${safe.replaceAll('"', '""')}"`
14
+ }
15
+
16
+ /**
17
+ * A CSV of translations that spreadsheets open as written: UTF-8 with a byte
18
+ * order mark (accents survive Excel), CRLF lines, the base language first and
19
+ * cells that look like formulas kept as text.
20
+ */
21
+ export function translationsCsv(
22
+ rows: FlatTranslationRow[],
23
+ locales: string[],
24
+ ): string {
25
+ const lines = [
26
+ ['key', ...locales].map(csvCell).join(SEPARATOR),
27
+ ...rows.map((row) =>
28
+ [row.key, ...locales.map((code) => row.values[code])]
29
+ .map(csvCell)
30
+ .join(SEPARATOR),
31
+ ),
32
+ ]
33
+ return `${BYTE_ORDER_MARK}${lines.join(LINE_BREAK)}${LINE_BREAK}`
34
+ }
35
+
36
+ /**
37
+ * The file an export downloads: the whole workspace and a filtered view never
38
+ * share a name (`site-all.csv`, `site-view-en_de.csv`).
39
+ */
40
+ export function exportFileName(
41
+ workspace: string,
42
+ viewLocales?: string[],
43
+ ): string {
44
+ return viewLocales
45
+ ? `${workspace}-view-${viewLocales.join('_')}.csv`
46
+ : `${workspace}-all.csv`
47
+ }