@things-factory/i18n-base 8.0.0 → 9.0.0-beta.3
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.
- package/dist-client/tsconfig.tsbuildinfo +1 -1
- package/dist-server/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -5
- package/client/bootstrap.ts +0 -16
- package/client/index.ts +0 -6
- package/client/page/terminology-page.ts +0 -408
- package/client/route.ts +0 -7
- package/server/apis/index.ts +0 -1
- package/server/apis/t.ts +0 -36
- package/server/controllers/clear-cache-for-translation.ts +0 -18
- package/server/index.ts +0 -6
- package/server/middlewares/index.ts +0 -36
- package/server/middlewares/koa-i18next/detect.ts +0 -108
- package/server/middlewares/koa-i18next/index.ts +0 -142
- package/server/middlewares/translations.ts +0 -27
- package/server/routers/translations-router.ts +0 -87
- package/server/routes.ts +0 -31
- package/server/service/index.ts +0 -27
- package/server/service/terminology/index.ts +0 -7
- package/server/service/terminology/terminology-mutation.ts +0 -171
- package/server/service/terminology/terminology-query.ts +0 -50
- package/server/service/terminology/terminology-type.ts +0 -54
- package/server/service/terminology/terminology.ts +0 -75
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@things-factory/i18n-base",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.0.0-beta.3",
|
|
4
4
|
"main": "dist-server/index.js",
|
|
5
5
|
"browser": "dist-client/index.js",
|
|
6
6
|
"things-factory": true,
|
|
@@ -27,9 +27,9 @@
|
|
|
27
27
|
"migration:create": "node ../../node_modules/typeorm/cli.js migration:create ./server/migrations/migration"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@operato/i18n": "^
|
|
31
|
-
"@things-factory/auth-base": "^
|
|
32
|
-
"@things-factory/shell": "^
|
|
30
|
+
"@operato/i18n": "^9.0.0-beta",
|
|
31
|
+
"@things-factory/auth-base": "^9.0.0-beta.3",
|
|
32
|
+
"@things-factory/shell": "^9.0.0-beta.0"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "1d7e0dd4c88f3c3f3bd311c00e4b1d1542d53634"
|
|
35
35
|
}
|
package/client/bootstrap.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { PageView } from '@operato/shell'
|
|
2
|
-
|
|
3
|
-
declare module '@operato/shell' {
|
|
4
|
-
interface PageView {
|
|
5
|
-
languageUpdated(i18next: any): void
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export default function bootstrap() {
|
|
10
|
-
/*
|
|
11
|
-
* PageView의 lifecycle에 languageUpdate를 추가한다.
|
|
12
|
-
*/
|
|
13
|
-
PageView.prototype.languageUpdated = function (i18next) {
|
|
14
|
-
this.pageReset()
|
|
15
|
-
}
|
|
16
|
-
}
|
package/client/index.ts
DELETED
|
@@ -1,408 +0,0 @@
|
|
|
1
|
-
import '@material/web/icon/icon.js'
|
|
2
|
-
|
|
3
|
-
import '@operato/data-grist'
|
|
4
|
-
import '@operato/context/ox-context-page-toolbar.js'
|
|
5
|
-
import '@operato/input/ox-input-select-buttons.js'
|
|
6
|
-
|
|
7
|
-
import { css, html } from 'lit'
|
|
8
|
-
import { customElement, query, state } from 'lit/decorators.js'
|
|
9
|
-
import { connect } from 'pwa-helpers/connect-mixin.js'
|
|
10
|
-
import gql from 'graphql-tag'
|
|
11
|
-
|
|
12
|
-
import { store, CustomAlert, PageView } from '@operato/shell'
|
|
13
|
-
import { client } from '@operato/graphql'
|
|
14
|
-
import { DataGrist, FetchOption, GristRecord } from '@operato/data-grist'
|
|
15
|
-
import { i18next, localize } from '@operato/i18n'
|
|
16
|
-
import { CommonHeaderStyles, ScrollbarStyles } from '@operato/styles'
|
|
17
|
-
import { isMobileDevice } from '@operato/utils'
|
|
18
|
-
import { getLanguages } from '@things-factory/auth-base/dist-client'
|
|
19
|
-
|
|
20
|
-
@customElement('terminology-page')
|
|
21
|
-
export class TerminologyPage extends connect(store)(localize(i18next)(PageView)) {
|
|
22
|
-
static styles = [
|
|
23
|
-
ScrollbarStyles,
|
|
24
|
-
CommonHeaderStyles,
|
|
25
|
-
css`
|
|
26
|
-
:host {
|
|
27
|
-
display: flex;
|
|
28
|
-
flex-direction: column;
|
|
29
|
-
|
|
30
|
-
overflow: hidden;
|
|
31
|
-
|
|
32
|
-
--grid-header-padding: 2px 0 2px 9px;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
ox-grist {
|
|
36
|
-
overflow-y: auto;
|
|
37
|
-
flex: 1;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
.header {
|
|
41
|
-
grid-template-areas: 'filters actions';
|
|
42
|
-
}
|
|
43
|
-
`
|
|
44
|
-
]
|
|
45
|
-
|
|
46
|
-
@state() private gristConfig: any
|
|
47
|
-
@state() private mode: 'CARD' | 'GRID' | 'LIST' = isMobileDevice() ? 'CARD' : 'GRID'
|
|
48
|
-
@state() private state?: string[]
|
|
49
|
-
|
|
50
|
-
@query('ox-grist') private grist!: DataGrist
|
|
51
|
-
|
|
52
|
-
get context() {
|
|
53
|
-
return {
|
|
54
|
-
title: i18next.t('title.terminology'),
|
|
55
|
-
search: {
|
|
56
|
-
handler: (search: string) => {
|
|
57
|
-
this.grist.searchText = search
|
|
58
|
-
},
|
|
59
|
-
value: this.grist?.searchText || ''
|
|
60
|
-
},
|
|
61
|
-
filter: {
|
|
62
|
-
handler: () => {
|
|
63
|
-
this.grist.toggleHeadroom()
|
|
64
|
-
}
|
|
65
|
-
},
|
|
66
|
-
help: 'mfg-warehouse/term-page',
|
|
67
|
-
actions: [
|
|
68
|
-
{
|
|
69
|
-
title: i18next.t('button.clear-cache'),
|
|
70
|
-
action: this.clearCache.bind(this),
|
|
71
|
-
icon: 'ink_eraser'
|
|
72
|
-
},
|
|
73
|
-
{
|
|
74
|
-
title: i18next.t('button.save'),
|
|
75
|
-
action: this.save.bind(this),
|
|
76
|
-
icon: 'save'
|
|
77
|
-
},
|
|
78
|
-
{
|
|
79
|
-
title: i18next.t('button.delete'),
|
|
80
|
-
action: this.delete.bind(this),
|
|
81
|
-
icon: 'delete',
|
|
82
|
-
emphasis: {
|
|
83
|
-
danger: true
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
],
|
|
87
|
-
exportable: {
|
|
88
|
-
name: i18next.t('button.export'),
|
|
89
|
-
data: this.exportHandler.bind(this)
|
|
90
|
-
},
|
|
91
|
-
toolbar: false
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
async getFilterStatus() {
|
|
96
|
-
return (await getLanguages()).map(({ code, display }) => {
|
|
97
|
-
return { value: code, display }
|
|
98
|
-
})
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
render() {
|
|
102
|
-
const mode = this.mode || (isMobileDevice() ? 'LIST' : 'GRID')
|
|
103
|
-
|
|
104
|
-
return html`
|
|
105
|
-
<ox-grist .mode=${mode} .config=${this.gristConfig} .fetchHandler=${this.fetchHandler.bind(this)}>
|
|
106
|
-
<div slot="headroom" class="header">
|
|
107
|
-
<div class="filters">
|
|
108
|
-
<ox-filters-form autofocus without-search></ox-filters-form>
|
|
109
|
-
</div>
|
|
110
|
-
|
|
111
|
-
<ox-context-page-toolbar class="actions" .context=${this.context}></ox-context-page-toolbar>
|
|
112
|
-
</div>
|
|
113
|
-
</ox-grist>
|
|
114
|
-
`
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
async pageInitialized(lifecycle) {
|
|
118
|
-
this.gristConfig = {
|
|
119
|
-
list: {
|
|
120
|
-
fields: ['name', 'description'],
|
|
121
|
-
details: ['active', 'updatedAt']
|
|
122
|
-
},
|
|
123
|
-
columns: [
|
|
124
|
-
{ type: 'gutter', gutterName: 'sequence' },
|
|
125
|
-
{ type: 'gutter', gutterName: 'row-selector', multiple: true },
|
|
126
|
-
{
|
|
127
|
-
type: 'string',
|
|
128
|
-
name: 'id',
|
|
129
|
-
header: i18next.t('field.id'),
|
|
130
|
-
hidden: true,
|
|
131
|
-
width: 0
|
|
132
|
-
},
|
|
133
|
-
{
|
|
134
|
-
type: 'string',
|
|
135
|
-
name: 'name',
|
|
136
|
-
header: i18next.t('field.name'),
|
|
137
|
-
record: {
|
|
138
|
-
editable: true
|
|
139
|
-
},
|
|
140
|
-
filter: 'search',
|
|
141
|
-
sortable: true,
|
|
142
|
-
width: 250
|
|
143
|
-
},
|
|
144
|
-
{
|
|
145
|
-
type: 'select',
|
|
146
|
-
name: 'locale',
|
|
147
|
-
header: i18next.t('field.locale'),
|
|
148
|
-
record: {
|
|
149
|
-
editable: true,
|
|
150
|
-
options: [{ display: '', value: '' }, ...(await this.getFilterStatus())]
|
|
151
|
-
},
|
|
152
|
-
width: 90,
|
|
153
|
-
filter: {
|
|
154
|
-
type: 'select-buttons',
|
|
155
|
-
operator: 'in'
|
|
156
|
-
}
|
|
157
|
-
},
|
|
158
|
-
{
|
|
159
|
-
type: 'select',
|
|
160
|
-
name: 'category',
|
|
161
|
-
label: true,
|
|
162
|
-
header: i18next.t('field.category'),
|
|
163
|
-
record: {
|
|
164
|
-
editable: true,
|
|
165
|
-
options: ['', 'button', 'field', 'label', 'menu', 'text', 'title']
|
|
166
|
-
},
|
|
167
|
-
width: 90,
|
|
168
|
-
filter: 'in',
|
|
169
|
-
imex: true
|
|
170
|
-
},
|
|
171
|
-
{
|
|
172
|
-
type: 'string',
|
|
173
|
-
name: 'display',
|
|
174
|
-
label: true,
|
|
175
|
-
header: i18next.t('field.display'),
|
|
176
|
-
record: {
|
|
177
|
-
editable: true
|
|
178
|
-
},
|
|
179
|
-
filter: 'search',
|
|
180
|
-
sortable: true,
|
|
181
|
-
width: 380
|
|
182
|
-
},
|
|
183
|
-
{
|
|
184
|
-
type: 'resource-object',
|
|
185
|
-
name: 'updater',
|
|
186
|
-
header: i18next.t('field.updater'),
|
|
187
|
-
record: {
|
|
188
|
-
editable: false
|
|
189
|
-
},
|
|
190
|
-
sortable: true,
|
|
191
|
-
width: 120
|
|
192
|
-
},
|
|
193
|
-
{
|
|
194
|
-
type: 'datetime',
|
|
195
|
-
name: 'updatedAt',
|
|
196
|
-
header: i18next.t('field.updated_at'),
|
|
197
|
-
record: {
|
|
198
|
-
editable: false
|
|
199
|
-
},
|
|
200
|
-
sortable: true,
|
|
201
|
-
width: 180
|
|
202
|
-
}
|
|
203
|
-
],
|
|
204
|
-
rows: {
|
|
205
|
-
appendable: true,
|
|
206
|
-
selectable: {
|
|
207
|
-
multiple: true
|
|
208
|
-
}
|
|
209
|
-
/*handlers: {
|
|
210
|
-
click: (columns, data, column, record, rowIndex) => {
|
|
211
|
-
// TODO Implement the desired sub-view(like item-view) and use it in the popup content after importing
|
|
212
|
-
const popup = openPopup(html`
|
|
213
|
-
<item-view item-id=${record.id} style="background-color: white;">
|
|
214
|
-
</item-view>
|
|
215
|
-
`, {
|
|
216
|
-
backdrop: true,
|
|
217
|
-
size: 'large',
|
|
218
|
-
title: i18next.t('text.item view')
|
|
219
|
-
})
|
|
220
|
-
popup.onclosed = () => {
|
|
221
|
-
this.grist.fetch()
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
}*/
|
|
225
|
-
},
|
|
226
|
-
sorters: [
|
|
227
|
-
{
|
|
228
|
-
name: 'name',
|
|
229
|
-
desc: true
|
|
230
|
-
}
|
|
231
|
-
]
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
async pageUpdated(changes: any, lifecycle: any) {
|
|
236
|
-
if (this.active) {
|
|
237
|
-
// do something here when this page just became as active
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
async fetchHandler({ page = 1, limit = 100, sortings = [], filters = [] }: FetchOption) {
|
|
242
|
-
const response = await client.query({
|
|
243
|
-
query: gql`
|
|
244
|
-
query ($filters: [Filter!], $pagination: Pagination, $sortings: [Sorting!]) {
|
|
245
|
-
terminologies(filters: $filters, pagination: $pagination, sortings: $sortings) {
|
|
246
|
-
items {
|
|
247
|
-
id
|
|
248
|
-
name
|
|
249
|
-
locale
|
|
250
|
-
category
|
|
251
|
-
display
|
|
252
|
-
creator {
|
|
253
|
-
id
|
|
254
|
-
name
|
|
255
|
-
}
|
|
256
|
-
createdAt
|
|
257
|
-
updater {
|
|
258
|
-
id
|
|
259
|
-
name
|
|
260
|
-
}
|
|
261
|
-
updatedAt
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
total
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
`,
|
|
268
|
-
variables: {
|
|
269
|
-
filters,
|
|
270
|
-
pagination: { page, limit },
|
|
271
|
-
sortings
|
|
272
|
-
}
|
|
273
|
-
})
|
|
274
|
-
|
|
275
|
-
return {
|
|
276
|
-
total: response.data.terminologies.total || 0,
|
|
277
|
-
records: response.data.terminologies.items || []
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
exportHandler() {
|
|
282
|
-
let records = [] as GristRecord
|
|
283
|
-
if (this.grist.selected && this.grist.selected.length > 0) {
|
|
284
|
-
records = this.grist.selected
|
|
285
|
-
} else {
|
|
286
|
-
records = this.grist.data.records
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
var headerSetting = this.grist.compiledConfig.columns
|
|
290
|
-
.filter(column => column.type !== 'gutter' && column.record !== undefined && column.imex !== undefined)
|
|
291
|
-
.map(column => {
|
|
292
|
-
return column.imex === true
|
|
293
|
-
? {
|
|
294
|
-
header: column.header.renderer(column),
|
|
295
|
-
key: column.name,
|
|
296
|
-
width: column.width,
|
|
297
|
-
type: column.type
|
|
298
|
-
}
|
|
299
|
-
: column.imex
|
|
300
|
-
})
|
|
301
|
-
|
|
302
|
-
var data = records.map(item => {
|
|
303
|
-
return {
|
|
304
|
-
id: item.id,
|
|
305
|
-
...this.gristConfig.columns
|
|
306
|
-
.filter(column => column.type !== 'gutter' && column.record !== undefined && column.imex !== undefined)
|
|
307
|
-
.reduce((record, column) => {
|
|
308
|
-
const key = column.imex === true ? column.name : column.imex.key
|
|
309
|
-
record[key] = key
|
|
310
|
-
.split('.')
|
|
311
|
-
.reduce((obj, key) => (obj && obj[key] !== 'undefined' ? obj[key] : undefined), item)
|
|
312
|
-
return record
|
|
313
|
-
}, {})
|
|
314
|
-
}
|
|
315
|
-
})
|
|
316
|
-
|
|
317
|
-
return { header: headerSetting, data: data }
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
async delete() {
|
|
321
|
-
const selectedRecords = this.grist.selected
|
|
322
|
-
|
|
323
|
-
if (!selectedRecords || selectedRecords.length == 0) {
|
|
324
|
-
this.showToast(i18next.t('text.NOTHING_SELECTED'))
|
|
325
|
-
} else {
|
|
326
|
-
const answer = await CustomAlert({
|
|
327
|
-
type: 'question',
|
|
328
|
-
title: i18next.t('button.delete'),
|
|
329
|
-
text: i18next.t('text.are_you_sure'),
|
|
330
|
-
confirmButton: { text: i18next.t('button.delete') },
|
|
331
|
-
cancelButton: { text: i18next.t('button.cancel') }
|
|
332
|
-
})
|
|
333
|
-
|
|
334
|
-
if (!answer) {
|
|
335
|
-
return
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
let ids = selectedRecords.map(record => record.id)
|
|
339
|
-
|
|
340
|
-
const response = await client.mutate({
|
|
341
|
-
mutation: gql`
|
|
342
|
-
mutation ($ids: [String!]!) {
|
|
343
|
-
deleteTerminologies(ids: $ids)
|
|
344
|
-
}
|
|
345
|
-
`,
|
|
346
|
-
variables: {
|
|
347
|
-
ids
|
|
348
|
-
}
|
|
349
|
-
})
|
|
350
|
-
|
|
351
|
-
this.grist.fetch()
|
|
352
|
-
|
|
353
|
-
return response.data.deleteTerminologies
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
async clearCache() {
|
|
358
|
-
const response = await client.mutate({
|
|
359
|
-
mutation: gql`
|
|
360
|
-
mutation {
|
|
361
|
-
clearTranslationsCache
|
|
362
|
-
}
|
|
363
|
-
`
|
|
364
|
-
})
|
|
365
|
-
|
|
366
|
-
if (!response.errors) {
|
|
367
|
-
this.showToast(i18next.t('text.info_x_successfully', { x: i18next.t('text.clearing-cache') }))
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
async save() {
|
|
372
|
-
let changedRecords = this.grist.dirtyRecords
|
|
373
|
-
|
|
374
|
-
if (!changedRecords || changedRecords.length == 0) {
|
|
375
|
-
this.showToast(i18next.t('text.nothing_changed'))
|
|
376
|
-
} else {
|
|
377
|
-
const patches = changedRecords.map(patch => {
|
|
378
|
-
const patchField: any = patch.id ? { id: patch.id } : {}
|
|
379
|
-
const dirtyFields = patch.__dirtyfields__
|
|
380
|
-
for (let key in dirtyFields) {
|
|
381
|
-
patchField[key] = dirtyFields[key].after
|
|
382
|
-
}
|
|
383
|
-
patchField.cuFlag = patch.__dirty__
|
|
384
|
-
|
|
385
|
-
return patchField
|
|
386
|
-
})
|
|
387
|
-
|
|
388
|
-
const response = await client.mutate({
|
|
389
|
-
mutation: gql`
|
|
390
|
-
mutation ($patches: [TerminologyPatch!]!) {
|
|
391
|
-
updateMultipleTerminologies(patches: $patches) {
|
|
392
|
-
id
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
`,
|
|
396
|
-
variables: {
|
|
397
|
-
patches
|
|
398
|
-
}
|
|
399
|
-
})
|
|
400
|
-
|
|
401
|
-
this.grist.fetch()
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
showToast(message: string) {
|
|
406
|
-
document.dispatchEvent(new CustomEvent('notify', { detail: { message } }))
|
|
407
|
-
}
|
|
408
|
-
}
|
package/client/route.ts
DELETED
package/server/apis/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './t'
|
package/server/apis/t.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { getRepository } from '@things-factory/shell'
|
|
2
|
-
|
|
3
|
-
import { Terminology } from '../service/terminology/terminology'
|
|
4
|
-
|
|
5
|
-
export type T_Arugments = {
|
|
6
|
-
key: String
|
|
7
|
-
locale: String
|
|
8
|
-
options?: Object
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export async function t({ key, locale = 'en-US', options }: T_Arugments) {
|
|
12
|
-
let value = await getTerminology({
|
|
13
|
-
key,
|
|
14
|
-
locale
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
return value
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
async function getTerminology({ key, locale }) {
|
|
21
|
-
const repo = getRepository(Terminology)
|
|
22
|
-
var value
|
|
23
|
-
try {
|
|
24
|
-
let term = await repo.findOne({
|
|
25
|
-
where: {
|
|
26
|
-
name: key,
|
|
27
|
-
locale
|
|
28
|
-
}
|
|
29
|
-
})
|
|
30
|
-
value = term.display
|
|
31
|
-
} catch (e) {
|
|
32
|
-
value = key
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return value
|
|
36
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { config } from '@things-factory/env'
|
|
2
|
-
|
|
3
|
-
const disableCustomTerminologyFeature = config.get('i18n/disableCustomTerminologyFeature')
|
|
4
|
-
|
|
5
|
-
const path = require('path')
|
|
6
|
-
var fs = require('fs')
|
|
7
|
-
|
|
8
|
-
export function clearTranslationCacheForCertainDomain(subdomain: string) {
|
|
9
|
-
if (disableCustomTerminologyFeature) {
|
|
10
|
-
return
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const cacheFileFolder = path.join('cache', 'translations', subdomain)
|
|
14
|
-
|
|
15
|
-
if (fs.existsSync(cacheFileFolder)) {
|
|
16
|
-
fs.rmSync(cacheFileFolder, { recursive: true })
|
|
17
|
-
}
|
|
18
|
-
}
|
package/server/index.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import i18next from 'i18next'
|
|
3
|
-
|
|
4
|
-
import koaI18next from './koa-i18next'
|
|
5
|
-
import { build } from './translations'
|
|
6
|
-
|
|
7
|
-
i18next.init({
|
|
8
|
-
debug: false,
|
|
9
|
-
ns: ['translations'],
|
|
10
|
-
defaultNS: 'translations',
|
|
11
|
-
preload: ['ko', 'en', 'jp', 'zh', 'ms'],
|
|
12
|
-
fallbackLng: 'en',
|
|
13
|
-
interpolation: {
|
|
14
|
-
prefix: '{',
|
|
15
|
-
suffix: '}'
|
|
16
|
-
},
|
|
17
|
-
load: 'languageOnly',
|
|
18
|
-
keySeparator: false
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
build(fs)
|
|
22
|
-
|
|
23
|
-
process.on('client-rebuilt' as any, (app, fs) => {
|
|
24
|
-
/* rebuild once done webpacking assets. only for development mode. */
|
|
25
|
-
build(fs)
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
export function initMiddlewares(app: any) {
|
|
29
|
-
app.use(
|
|
30
|
-
koaI18next(i18next, {
|
|
31
|
-
lookupCookie: 'i18next',
|
|
32
|
-
order: ['cookie', 'header'],
|
|
33
|
-
next: true // if koa is version 2
|
|
34
|
-
})
|
|
35
|
-
)
|
|
36
|
-
}
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
const DEFAULT_ORDER = ['querystring', 'cookie', 'header']
|
|
2
|
-
|
|
3
|
-
const detectors = {
|
|
4
|
-
cookie: function (context, options) {
|
|
5
|
-
let cookie = options.lookupCookie || 'i18next'
|
|
6
|
-
return context.cookies.get(cookie)
|
|
7
|
-
},
|
|
8
|
-
|
|
9
|
-
// fork from i18next-express-middleware
|
|
10
|
-
header: function (context) {
|
|
11
|
-
let acceptLanguage = context.get('accept-language')
|
|
12
|
-
let found
|
|
13
|
-
let locales = []
|
|
14
|
-
if (acceptLanguage) {
|
|
15
|
-
let lngs = []
|
|
16
|
-
|
|
17
|
-
// associate language tags by their 'q' value (between 1 and 0)
|
|
18
|
-
acceptLanguage.split(',').forEach(function (l) {
|
|
19
|
-
let parts = l.split(';') // 'en-GB;q=0.8' -> ['en-GB', 'q=0.8']
|
|
20
|
-
|
|
21
|
-
// get the language tag qvalue: 'q=0.8' -> 0.8
|
|
22
|
-
let qvalue = 1 // default qvalue
|
|
23
|
-
|
|
24
|
-
for (let i = 0; i < parts.length; i++) {
|
|
25
|
-
let part = parts[i].split('=')
|
|
26
|
-
if (part[0] === 'q' && !isNaN(part[1])) {
|
|
27
|
-
qvalue = Number(part[1])
|
|
28
|
-
break
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
// add the tag and primary subtag to the qvalue associations
|
|
32
|
-
lngs.push({
|
|
33
|
-
lng: parts[0],
|
|
34
|
-
q: qvalue
|
|
35
|
-
})
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
lngs.sort(function (a, b) {
|
|
39
|
-
return b.q - a.q
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
for (let i = 0; i < lngs.length; i++) {
|
|
43
|
-
locales.push(lngs[i].lng)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (locales.length) found = locales
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return found
|
|
50
|
-
},
|
|
51
|
-
path: function (context, options) {
|
|
52
|
-
let found
|
|
53
|
-
|
|
54
|
-
if (options.lookupPath !== undefined && context.params) {
|
|
55
|
-
found = context.params[options.lookupPath]
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (!found && options.lookupFromPathIndex !== undefined) {
|
|
59
|
-
let parts = context.path.split('/')
|
|
60
|
-
if (parts[0] === '') {
|
|
61
|
-
// Handle paths that start with a slash, i.e., '/foo' -> ['', 'foo']
|
|
62
|
-
parts.shift()
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (parts.length > options.lookupFromPathIndex) {
|
|
66
|
-
found = parts[options.lookupFromPathIndex]
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
return found
|
|
70
|
-
},
|
|
71
|
-
querystring: function (context, options) {
|
|
72
|
-
let name = options.lookupQuerystring || 'lng'
|
|
73
|
-
return context.query[name]
|
|
74
|
-
},
|
|
75
|
-
|
|
76
|
-
session: function (context, options) {
|
|
77
|
-
let name = options.lookupSession || 'lng'
|
|
78
|
-
return context.session && context.session[name]
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export default function (context, options = {}) {
|
|
83
|
-
let { order, fallback } = options as any
|
|
84
|
-
order = order && Array.isArray(order) ? order : DEFAULT_ORDER
|
|
85
|
-
|
|
86
|
-
let lngs = []
|
|
87
|
-
|
|
88
|
-
for (let i = 0, len = order.length; i < len; i++) {
|
|
89
|
-
let detector = detectors[order[i]]
|
|
90
|
-
let lng
|
|
91
|
-
if (detector) {
|
|
92
|
-
lng = detector(context, options)
|
|
93
|
-
}
|
|
94
|
-
if (lng && typeof lng === 'string') {
|
|
95
|
-
lngs.push(lng)
|
|
96
|
-
} else {
|
|
97
|
-
lngs = lngs.concat(lng)
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
let found
|
|
101
|
-
for (let i = 0, len = lngs.length; i < len; i++) {
|
|
102
|
-
let cleanedLng = context.i18next.services.languageUtils.formatLanguageCode(lngs[i])
|
|
103
|
-
if (context.i18next.services.languageUtils.isSupportedCode(cleanedLng)) found = cleanedLng
|
|
104
|
-
if (found) break
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return found || fallback
|
|
108
|
-
}
|