11import { existsSync , mkdirSync , readFileSync , readdirSync , writeFileSync } from 'fs' ;
22import { getTranslateType } from './translate-type' ;
3- const translate = require ( 'deepl' ) ;
4- import DeeplConfig from './deepl.config.json' ;
5- import TranslatedCache from '../data/translated-cache.json' ;
63
74const apply = async ( ) => {
85 const translateTypes = await getTranslateType ( ) ;
96
10- const cacheTranslated = TranslatedCache . cache as { [ key : string ] : string } ;
11- const translatedNow = { } as { [ key : string ] : string } ;
12-
137 for ( const translateType of translateTypes ) {
8+ const cachePath = process . cwd ( ) + '/scripts/data/translated-cache-' + translateType . type + '.json' ;
9+ const cache = JSON . parse ( readFileSync ( cachePath , { encoding : 'utf8' } ) ) ;
10+
11+ const translatedNow = { } as { [ key : string ] : string } ;
1412 const directory = process . cwd ( ) + '/src/translate/' + translateType . type ;
1513 if ( ! existsSync ( directory ) ) {
1614 continue ;
@@ -40,32 +38,13 @@ const apply = async () => {
4038 const translateText = ob [ translateType . translateTargetKey ] . replace ( / \n / g, ' ' ) ;
4139
4240 // キャッシュデータにあるか確認
43- if ( cacheTranslated . hasOwnProperty ( translateText ) ) {
44- ob [ translateType . translateTargetKey ] = cacheTranslated [ translateText ] ;
41+ if ( cache . hasOwnProperty ( translateText ) ) {
42+ ob [ translateType . translateTargetKey ] = cache [ translateText ] ;
43+ translatedNow [ translateText ] = cache [ translateText ] ;
4544 return ;
46- }
47-
48- // 今回翻訳データにあるか確認
49- // if (translatedNow.hasOwnProperty(ob[translateType.translateTargetKey])) {
50- // ob[translateType.translateTargetKey] = ob[translateType.translateTargetKey] + `\n\n自動翻訳: ${translatedNow[translateText]}`;
51- // return;
52- // }
53-
54- try {
55- const response = await translate ( {
56- free_api : true ,
57- text : translateText ,
58- source_lang : DeeplConfig . fromLanguage ,
59- target_lang : DeeplConfig . toLanguage ,
60- auth_key : process . env . DEEPLAUTHKEY ,
61- } ) ;
62- const translated = response . data . translations [ 0 ] . text ;
63- translatedNow [ translateText ] = translated ;
64- } catch ( e ) {
45+ } else {
6546 translatedNow [ translateText ] = translateText ;
6647 }
67-
68- // ob[translateType.translateTargetKey] = ob[translateType.translateTargetKey] + `\n\n自動翻訳: ${translated}`;
6948 }
7049 } )
7150 ) ;
@@ -87,15 +66,10 @@ const apply = async () => {
8766 JSON . stringify ( resource , null , 2 ) ,
8867 { encoding : 'utf8' }
8968 ) ;
69+ writeFileSync ( cachePath , JSON . stringify ( translatedNow , null , 2 ) , {
70+ encoding : 'utf8' ,
71+ } ) ;
9072 }
91-
92- // 翻訳データの結合
93- const writeTranslateCache = {
94- cache : Object . assign ( cacheTranslated , translatedNow ) ,
95- } ;
96- writeFileSync ( process . cwd ( ) + '/scripts/data/translated-cache.json' , JSON . stringify ( writeTranslateCache , null , 2 ) , {
97- encoding : 'utf8' ,
98- } ) ;
9973} ;
10074
10175const create = async ( ) => {
0 commit comments