11import { existsSync , mkdirSync , readFileSync , readdirSync , writeFileSync } from 'fs' ;
2- const fetch = require ( 'node-fetch' ) ;
3- import { commands } from '../data/cli.json' ;
4-
5- const getTranslateType = async ( ) => {
6- const response = await fetch ( 'https://unpkg.com/@ionic/docs/core.json' ) ;
7- const { components } = await response . json ( ) ;
8-
9- return [
10- {
11- type : 'api' ,
12- contents : components ,
13- key : 'tag' ,
14- contentsKey : 'components' ,
15- markdown : 'docs' ,
16- resource : response ,
17- } ,
18- {
19- type : 'cli' ,
20- contents : commands ,
21- key : 'name' ,
22- contentsKey : 'commands' ,
23- markdown : 'description' ,
24- resource : require ( '../data/cli.json' ) ,
25- } ,
26- {
27- type : 'native' ,
28- contents : require ( '../data/native.json' ) ,
29- key : 'packageName' ,
30- contentsKey : '' ,
31- markdown : 'description' ,
32- resource : require ( '../data/native.json' ) ,
33- }
34- ] ;
35- }
2+ import { getTranslateType } from './translate-type' ;
3+ const translate = require ( 'deepl' ) ;
4+ import DeeplConfig from './deepl.config.json' ;
5+ import TranslatedCache from '../data/translated-cache.json' ;
366
377const apply = async ( ) => {
388 const translateTypes = await getTranslateType ( ) ;
399
10+ const cacheTranslated = TranslatedCache . cache as { [ key : string ] : string } ;
11+ const translatedNow = { } as { [ key : string ] : string } ;
12+
4013 for ( const translateType of translateTypes ) {
4114 const directory = process . cwd ( ) + '/src/translate/' + translateType . type ;
4215 if ( ! existsSync ( directory ) ) {
@@ -58,6 +31,39 @@ const apply = async () => {
5831 if ( existsSync ( readmePath ) ) {
5932 componentObject [ translateType . markdown ] = readFileSync ( readmePath , { encoding : 'utf8' } ) ;
6033 }
34+ await Promise . all ( translateType . translateTarget . map ( async ( target : string ) => {
35+ if ( componentObject [ target ] ) {
36+ await Promise . all ( componentObject [ target ] . map ( async ( ob : any ) => {
37+ if ( ob [ translateType . translateTargetKey ] ) {
38+ const translateText = ob [ translateType . translateTargetKey ] . replace ( / \n / g, ' ' ) ;
39+
40+ // キャッシュデータにあるか確認
41+ if ( cacheTranslated . hasOwnProperty ( translateText ) ) {
42+ ob [ translateType . translateTargetKey ] = ob [ translateType . translateTargetKey ] + `\n\n自動翻訳: ${ cacheTranslated [ translateText ] } ` ;
43+ return ;
44+ }
45+
46+ // 今回翻訳データにあるか確認
47+ if ( translatedNow . hasOwnProperty ( ob [ translateType . translateTargetKey ] ) ) {
48+ ob [ translateType . translateTargetKey ] = ob [ translateType . translateTargetKey ] + `\n\n自動翻訳: ${ translatedNow [ translateText ] } ` ;
49+ return ;
50+ }
51+
52+ const response = await translate ( {
53+ free_api : true ,
54+ text : translateText ,
55+ source_lang : DeeplConfig . fromLanguage ,
56+ target_lang : DeeplConfig . toLanguage ,
57+ auth_key : process . env . DEEPLAUTHKEY ,
58+ } ) ;
59+ const translated = response . data . translations [ 0 ] . text ;
60+ translatedNow [ translateText ] = translated ;
61+
62+ ob [ translateType . translateTargetKey ] = ob [ translateType . translateTargetKey ] + `\n\n自動翻訳: ${ translated } ` ;
63+ }
64+ } ) ) ;
65+ }
66+ } ) ) ;
6167 componentsObject . push ( componentObject ) ;
6268 }
6369 let resource = translateType . resource ;
@@ -70,6 +76,12 @@ const apply = async () => {
7076
7177 writeFileSync ( process . cwd ( ) + '/scripts/data/translated-' + translateType . type + '.json' , JSON . stringify ( resource , null , 2 ) , { encoding : 'utf8' } ) ;
7278 }
79+
80+ // 翻訳データの結合
81+ const writeTranslateCache = {
82+ cache : Object . assign ( cacheTranslated , translatedNow )
83+ } ;
84+ writeFileSync ( process . cwd ( ) + '/scripts/data/translated-cache.json' , JSON . stringify ( writeTranslateCache , null , 2 ) , { encoding : 'utf8' } ) ;
7385} ;
7486
7587const create = async ( ) => {
0 commit comments