@things-factory/i18n-base 7.0.1-alpha.5 → 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.5",
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.5"
28
+ "@things-factory/shell": "^7.0.1-alpha.55"
29
29
  },
30
- "gitHead": "3a1ceb942521a89f14c4b16a323b506ff676d349"
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,4 +1,21 @@
1
1
  import i18next from 'i18next'
2
+ import { ILike } from 'typeorm'
3
+
4
+ import { getRepository } from '@things-factory/shell'
5
+ import { config } from '@things-factory/env'
6
+
7
+ const disableCustomTerminologyFeature = config.get('i18n/disableCustomTerminologyFeature')
8
+
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
+ })
2
19
 
3
20
  process.on('bootstrap-module-global-public-route' as any, (app, globalPublicRouter) => {
4
21
  globalPublicRouter.get('/translations/translate/:lng/:key', async (context, next) => {
@@ -9,10 +26,71 @@ process.on('bootstrap-module-global-public-route' as any, (app, globalPublicRout
9
26
  })
10
27
 
11
28
  globalPublicRouter.get('/translations/translate/:key', async (context, next) => {
12
- const { lng } = context.state
13
29
  const { key } = context.params
14
30
  const translated = context.t(key)
15
31
 
16
32
  context.body = translated
17
33
  })
18
34
  })
35
+
36
+ process.on('bootstrap-module-domain-public-route' as any, (app, domainPublicRouter) => {
37
+ domainPublicRouter.get('/domain/:subdomain/translations/:filename', async (context, next) => {
38
+ const { subdomain, filename } = context.params
39
+ const { domain } = context.state
40
+
41
+ const locale = (filename || 'en.json').split('.')[0]
42
+
43
+ // context.set('Cache-Control', 'public, max-age=31536000') // 캐시 기간 설정 : 1년
44
+ // context.set('Expires', new Date(Date.now() + 31536000000).toUTCString()) // 캐시 만료일 설정
45
+
46
+ if (disableCustomTerminologyFeature) {
47
+ var file = fs.readFileSync(path.resolve(path.join('dist-app', 'translations', filename)))
48
+
49
+ context.type = 'application/json'
50
+ context.body = file
51
+ return
52
+ }
53
+
54
+ const cacheFileFolder = path.join('cache', 'translations', subdomain)
55
+ const cacheFilePath = path.join(cacheFileFolder, filename)
56
+
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)))
61
+
62
+ try {
63
+ const terminologies = await getRepository(Terminology).find({
64
+ where: { domain: { id: domain.id }, locale: ILike(locale + '%') }
65
+ })
66
+
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
+ }
91
+ }
92
+
93
+ context.type = 'application/json'
94
+ context.body = file
95
+ })
96
+ })
@@ -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(