@things-factory/i18n-base 7.0.66 → 7.0.68

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@things-factory/i18n-base",
3
- "version": "7.0.66",
3
+ "version": "7.0.68",
4
4
  "main": "dist-server/index.js",
5
5
  "browser": "dist-client/index.js",
6
6
  "things-factory": true,
@@ -28,8 +28,8 @@
28
28
  },
29
29
  "dependencies": {
30
30
  "@operato/i18n": "^7.0.0",
31
- "@things-factory/auth-base": "^7.0.66",
32
- "@things-factory/shell": "^7.0.66"
31
+ "@things-factory/auth-base": "^7.0.68",
32
+ "@things-factory/shell": "^7.0.68"
33
33
  },
34
- "gitHead": "17363b9643b8e126c4f55bc5dc9f6dfbba2cedc3"
34
+ "gitHead": "a9328d1a0f4e0c89cc793839e9b6154c2790bd15"
35
35
  }
@@ -0,0 +1,87 @@
1
+ import Router from 'koa-router'
2
+ import { ILike } from 'typeorm'
3
+
4
+ import { getRepository } from '@things-factory/shell'
5
+ import { Terminology } from '../service/terminology/terminology'
6
+ import { config } from '@things-factory/env'
7
+
8
+ const path = require('path')
9
+ const cacheFS = require('fs')
10
+ var fs = require('fs')
11
+
12
+ const disableCustomTerminologyFeature = config.get('i18n/disableCustomTerminologyFeature')
13
+
14
+ if (!disableCustomTerminologyFeature) {
15
+ process.on('client-rebuilt' as any, (app, compilerFileSystem) => {
16
+ /* change filesystem whenever webpacking assets done. only for development mode. */
17
+ fs = compilerFileSystem
18
+
19
+ const cacheFileFolder = path.join('cache', 'translations')
20
+
21
+ if (cacheFS.existsSync(cacheFileFolder)) {
22
+ cacheFS.rmSync(cacheFileFolder, { recursive: true })
23
+ }
24
+ })
25
+ }
26
+
27
+ export const translationsRouter = new Router()
28
+
29
+ translationsRouter.get('/translations/:filename', async (context, next) => {
30
+ const { subdomain, filename } = context.params
31
+ const { domain } = context.state
32
+
33
+ const locale = (filename || 'en.json').split('.')[0]
34
+
35
+ // context.set('Cache-Control', 'public, max-age=31536000') // 캐시 기간 설정 : 1년
36
+ // context.set('Expires', new Date(Date.now() + 31536000000).toUTCString()) // 캐시 만료일 설정
37
+
38
+ if (disableCustomTerminologyFeature) {
39
+ var file = fs.readFileSync(path.resolve(path.join('dist-app', 'translations', filename)))
40
+
41
+ context.type = 'application/json'
42
+ context.body = file
43
+ return
44
+ }
45
+
46
+ const cacheFileFolder = path.join('cache', 'translations', subdomain)
47
+ const cacheFilePath = path.join(cacheFileFolder, filename)
48
+
49
+ if (cacheFS.existsSync(cacheFilePath)) {
50
+ var file = cacheFS.readFileSync(cacheFilePath)
51
+ } else {
52
+ var file = fs.readFileSync(path.resolve(path.join('dist-app', 'translations', filename)))
53
+
54
+ try {
55
+ const terminologies = await getRepository(Terminology).find({
56
+ where: { domain: { id: domain.id }, locale: ILike(locale + '%') }
57
+ })
58
+
59
+ const json = JSON.parse(file)
60
+
61
+ terminologies.forEach(term => {
62
+ const { name, category, display } = term
63
+ json[`${category}.${name}`] = display
64
+ })
65
+
66
+ try {
67
+ if (!cacheFS.existsSync(cacheFileFolder)) {
68
+ cacheFS.mkdirSync(cacheFileFolder, { recursive: true })
69
+ }
70
+
71
+ cacheFS.writeFileSync(cacheFilePath, JSON.stringify(json))
72
+ } catch (err) {
73
+ console.error(err)
74
+ }
75
+
76
+ context.type = 'application/json'
77
+ context.body = json
78
+
79
+ return
80
+ } catch (err) {
81
+ console.error(err)
82
+ }
83
+ }
84
+
85
+ context.type = 'application/json'
86
+ context.body = file
87
+ })
package/server/routes.ts CHANGED
@@ -1,27 +1,10 @@
1
1
  import i18next from 'i18next'
2
- import { ILike } from 'typeorm'
3
-
4
- import { getRepository } from '@things-factory/shell'
5
2
  import { config } from '@things-factory/env'
6
3
 
7
4
  const disableCustomTerminologyFeature = config.get('i18n/disableCustomTerminologyFeature')
5
+ const isPathBaseDomain = !config.get('subdomain') && !config.get('useVirtualHostBasedDomain')
8
6
 
9
- import { Terminology } from './service/terminology/terminology'
10
-
11
- const path = require('path')
12
- const cacheFS = require('fs')
13
- var fs = require('fs')
14
-
15
- process.on('client-rebuilt' as any, (app, compilerFileSystem) => {
16
- /* change filesystem whenever webpacking assets done. only for development mode. */
17
- fs = compilerFileSystem
18
-
19
- const cacheFileFolder = path.join('cache', 'translations')
20
-
21
- if (cacheFS.existsSync(cacheFileFolder)) {
22
- cacheFS.rmSync(cacheFileFolder, { recursive: true })
23
- }
24
- })
7
+ import { translationsRouter } from './routers/translations-router'
25
8
 
26
9
  process.on('bootstrap-module-global-public-route' as any, (app, globalPublicRouter) => {
27
10
  globalPublicRouter.get('/translations/translate/:lng/:key', async (context, next) => {
@@ -40,63 +23,9 @@ process.on('bootstrap-module-global-public-route' as any, (app, globalPublicRout
40
23
  })
41
24
 
42
25
  process.on('bootstrap-module-domain-public-route' as any, (app, domainPublicRouter) => {
43
- domainPublicRouter.get('/domain/:subdomain/translations/:filename', async (context, next) => {
44
- const { subdomain, filename } = context.params
45
- const { domain } = context.state
46
-
47
- const locale = (filename || 'en.json').split('.')[0]
48
-
49
- // context.set('Cache-Control', 'public, max-age=31536000') // 캐시 기간 설정 : 1년
50
- // context.set('Expires', new Date(Date.now() + 31536000000).toUTCString()) // 캐시 만료일 설정
51
-
52
- if (disableCustomTerminologyFeature) {
53
- var file = fs.readFileSync(path.resolve(path.join('dist-app', 'translations', filename)))
54
-
55
- context.type = 'application/json'
56
- context.body = file
57
- return
58
- }
59
-
60
- const cacheFileFolder = path.join('cache', 'translations', subdomain)
61
- const cacheFilePath = path.join(cacheFileFolder, filename)
62
-
63
- if (cacheFS.existsSync(cacheFilePath)) {
64
- var file = cacheFS.readFileSync(cacheFilePath)
65
- } else {
66
- var file = fs.readFileSync(path.resolve(path.join('dist-app', 'translations', filename)))
67
-
68
- try {
69
- const terminologies = await getRepository(Terminology).find({
70
- where: { domain: { id: domain.id }, locale: ILike(locale + '%') }
71
- })
72
-
73
- const json = JSON.parse(file)
74
-
75
- terminologies.forEach(term => {
76
- const { name, category, display } = term
77
- json[`${category}.${name}`] = display
78
- })
79
-
80
- try {
81
- if (!cacheFS.existsSync(cacheFileFolder)) {
82
- cacheFS.mkdirSync(cacheFileFolder, { recursive: true })
83
- }
84
-
85
- cacheFS.writeFileSync(cacheFilePath, JSON.stringify(json))
86
- } catch (err) {
87
- console.error(err)
88
- }
89
-
90
- context.type = 'application/json'
91
- context.body = json
92
-
93
- return
94
- } catch (err) {
95
- console.error(err)
96
- }
97
- }
98
-
99
- context.type = 'application/json'
100
- context.body = file
101
- })
26
+ if (isPathBaseDomain) {
27
+ domainPublicRouter.use('/domain/:subdomain', translationsRouter.routes(), translationsRouter.allowedMethods())
28
+ } else if (!disableCustomTerminologyFeature) {
29
+ domainPublicRouter.use('', translationsRouter.routes(), translationsRouter.allowedMethods())
30
+ }
102
31
  })