@things-factory/i18n-base 7.0.1-alpha.47 → 7.0.1-alpha.55

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.1-alpha.47",
3
+ "version": "7.0.1-alpha.55",
4
4
  "main": "dist-server/index.js",
5
5
  "browser": "client/index.js",
6
6
  "things-factory": true,
@@ -25,7 +25,7 @@
25
25
  },
26
26
  "dependencies": {
27
27
  "@operato/i18n": "^2.0.0-alpha.0",
28
- "@things-factory/shell": "^7.0.1-alpha.47"
28
+ "@things-factory/shell": "^7.0.1-alpha.55"
29
29
  },
30
- "gitHead": "7ede9691f8dbc3588094858550d9e995c975ac04"
30
+ "gitHead": "aeab58a019a928a4063057446538fc3d77cf3d77"
31
31
  }
@@ -0,0 +1,18 @@
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.rmdirSync(cacheFileFolder, { recursive: true })
17
+ }
18
+ }
package/server/routes.ts CHANGED
@@ -1,10 +1,15 @@
1
1
  import i18next from 'i18next'
2
2
  import { ILike } from 'typeorm'
3
3
 
4
- import { Domain, getRepository } from '@things-factory/shell'
4
+ import { getRepository } from '@things-factory/shell'
5
+ import { config } from '@things-factory/env'
6
+
7
+ const disableCustomTerminologyFeature = config.get('i18n/disableCustomTerminologyFeature')
8
+
5
9
  import { Terminology } from './service/terminology/terminology'
6
10
 
7
11
  const path = require('path')
12
+ const cacheFS = require('fs')
8
13
  var fs = require('fs')
9
14
 
10
15
  process.on('client-rebuilt' as any, (app, compilerFileSystem) => {
@@ -29,34 +34,60 @@ process.on('bootstrap-module-global-public-route' as any, (app, globalPublicRout
29
34
  })
30
35
 
31
36
  process.on('bootstrap-module-domain-public-route' as any, (app, domainPublicRouter) => {
32
- domainPublicRouter.get('/domain/:domain/translations/:filename', async (context, next) => {
33
- const { filename } = context.params
37
+ domainPublicRouter.get('/domain/:subdomain/translations/:filename', async (context, next) => {
38
+ const { subdomain, filename } = context.params
34
39
  const { domain } = context.state
35
40
 
41
+ const locale = (filename || 'en.json').split('.')[0]
42
+
36
43
  // context.set('Cache-Control', 'public, max-age=31536000') // 캐시 기간 설정 : 1년
37
44
  // context.set('Expires', new Date(Date.now() + 31536000000).toUTCString()) // 캐시 만료일 설정
38
45
 
39
- const locale = (filename || 'en.json').split('.')[0]
40
- const file = fs.readFileSync(path.resolve(path.join('dist-app', 'translations', filename)))
46
+ if (disableCustomTerminologyFeature) {
47
+ var file = fs.readFileSync(path.resolve(path.join('dist-app', 'translations', filename)))
41
48
 
42
- const terminologies = await getRepository(Terminology).find({
43
- where: { domain: { id: domain.id }, locale: ILike(locale + '%') }
44
- })
49
+ context.type = 'application/json'
50
+ context.body = file
51
+ return
52
+ }
45
53
 
46
- try {
47
- const json = JSON.parse(file)
54
+ const cacheFileFolder = path.join('cache', 'translations', subdomain)
55
+ const cacheFilePath = path.join(cacheFileFolder, filename)
48
56
 
49
- terminologies.forEach(term => {
50
- const { name, category, display } = term
51
- json[`${category}.${name}`] = display
52
- })
57
+ if (cacheFS.existsSync(cacheFilePath)) {
58
+ var file = cacheFS.readFileSync(cacheFilePath)
59
+ } else {
60
+ var file = fs.readFileSync(path.resolve(path.join('dist-app', 'translations', filename)))
53
61
 
54
- context.type = 'application/json'
55
- context.body = json
62
+ try {
63
+ const terminologies = await getRepository(Terminology).find({
64
+ where: { domain: { id: domain.id }, locale: ILike(locale + '%') }
65
+ })
56
66
 
57
- return
58
- } catch (err) {
59
- console.error(err)
67
+ const json = JSON.parse(file)
68
+
69
+ terminologies.forEach(term => {
70
+ const { name, category, display } = term
71
+ json[`${category}.${name}`] = display
72
+ })
73
+
74
+ try {
75
+ if (!cacheFS.existsSync(cacheFileFolder)) {
76
+ cacheFS.mkdirSync(cacheFileFolder, { recursive: true })
77
+ }
78
+
79
+ cacheFS.writeFileSync(cacheFilePath, JSON.stringify(json))
80
+ } catch (err) {
81
+ console.error(err)
82
+ }
83
+
84
+ context.type = 'application/json'
85
+ context.body = json
86
+
87
+ return
88
+ } catch (err) {
89
+ console.error(err)
90
+ }
60
91
  }
61
92
 
62
93
  context.type = 'application/json'
@@ -0,0 +1,32 @@
1
+ import { EventSubscriber, EntitySubscriberInterface, InsertEvent, UpdateEvent } from 'typeorm'
2
+
3
+ import { Terminology } from './terminology'
4
+
5
+ import { clearTranslationCacheForCertainDomain } from '../../controllers/clear-cache-for-translation'
6
+
7
+ function clearCache(subdomain: string) {
8
+ try {
9
+ clearTranslationCacheForCertainDomain(subdomain)
10
+ } catch (err) {
11
+ console.error('Failed to delete the translations cache directory:', err)
12
+ }
13
+ }
14
+
15
+ @EventSubscriber()
16
+ export class TerminologySubscriber implements EntitySubscriberInterface<Terminology> {
17
+ listenTo() {
18
+ return Terminology
19
+ }
20
+
21
+ async afterInsert(event: InsertEvent<Terminology>): Promise<any | void> {
22
+ const { entity: terminology, manager: tx } = event
23
+
24
+ clearCache(terminology.domain.subdomain)
25
+ }
26
+
27
+ async afterUpdate(event: UpdateEvent<Terminology>): Promise<any | void> {
28
+ const { entity: terminology, manager: tx } = event
29
+
30
+ clearCache(terminology.domain.subdomain)
31
+ }
32
+ }
@@ -1,6 +1,8 @@
1
1
  import { Terminology } from './terminology'
2
2
  import { TerminologyQuery } from './terminology-query'
3
3
  import { TerminologyMutation } from './terminology-mutation'
4
+ import { TerminologySubscriber } from './event-subscriber'
4
5
 
5
6
  export const entities = [Terminology]
6
7
  export const resolvers = [TerminologyQuery, TerminologyMutation]
8
+ export const subscribers = [TerminologySubscriber]
@@ -4,6 +4,8 @@ import { In } from 'typeorm'
4
4
  import { Terminology } from './terminology'
5
5
  import { NewTerminology, TerminologyPatch } from './terminology-type'
6
6
 
7
+ import { clearTranslationCacheForCertainDomain } from '../../controllers/clear-cache-for-translation'
8
+
7
9
  @Resolver(Terminology)
8
10
  export class TerminologyMutation {
9
11
  @Directive('@transaction')
@@ -21,11 +23,7 @@ export class TerminologyMutation {
21
23
 
22
24
  @Directive('@transaction')
23
25
  @Mutation(returns => Terminology, { description: 'To modify Terminology information' })
24
- async updateTerminology(
25
- @Arg('id') id: string,
26
- @Arg('patch') patch: TerminologyPatch,
27
- @Ctx() context: any
28
- ): Promise<Terminology> {
26
+ async updateTerminology(@Arg('id') id: string, @Arg('patch') patch: TerminologyPatch, @Ctx() context: any): Promise<Terminology> {
29
27
  const { domain, user, tx } = context.state
30
28
 
31
29
  const repository = tx.getRepository(Terminology)
@@ -42,10 +40,7 @@ export class TerminologyMutation {
42
40
 
43
41
  @Directive('@transaction')
44
42
  @Mutation(returns => [Terminology], { description: "To modify multiple Terminologies' information" })
45
- async updateMultipleTerminologies(
46
- @Arg('patches', type => [TerminologyPatch]) patches: TerminologyPatch[],
47
- @Ctx() context: any
48
- ): Promise<Terminology[]> {
43
+ async updateMultipleTerminologies(@Arg('patches', type => [TerminologyPatch]) patches: TerminologyPatch[], @Ctx() context: any): Promise<Terminology[]> {
49
44
  const { domain, user, tx } = context.state
50
45
 
51
46
  let results = []
@@ -86,6 +81,16 @@ export class TerminologyMutation {
86
81
  return results
87
82
  }
88
83
 
84
+ @Directive('@transaction')
85
+ @Mutation(returns => Boolean, { description: 'To clear translations cache' })
86
+ async clearTranslationsCache(@Ctx() context: any): Promise<boolean> {
87
+ const { domain, tx } = context.state
88
+
89
+ clearTranslationCacheForCertainDomain(domain.subdomain)
90
+
91
+ return true
92
+ }
93
+
89
94
  @Directive('@transaction')
90
95
  @Mutation(returns => Boolean, { description: 'To delete Terminology' })
91
96
  async deleteTerminology(@Arg('id') id: string, @Ctx() context: any): Promise<boolean> {
@@ -110,10 +115,7 @@ export class TerminologyMutation {
110
115
 
111
116
  @Directive('@transaction')
112
117
  @Mutation(returns => Boolean, { description: 'To import multiple Terminologies' })
113
- async importTerminologies(
114
- @Arg('terminologies', type => [TerminologyPatch]) terminologies: TerminologyPatch[],
115
- @Ctx() context: any
116
- ): Promise<boolean> {
118
+ async importTerminologies(@Arg('terminologies', type => [TerminologyPatch]) terminologies: TerminologyPatch[], @Ctx() context: any): Promise<boolean> {
117
119
  const { domain, tx } = context.state
118
120
 
119
121
  await Promise.all(