@@ -4,16 +4,15 @@ import {Config, CredentialsConfig} from './config';
4
4
import { Logger } from './logger' ;
5
5
import BuildRequest = TranslationsModel . BuildRequest ;
6
6
import { baseName , downloadZipAndUnzip } from './utils' ;
7
- import { toPlatformPath } from '@actions/core ' ;
7
+ import { Writer } from './writer ' ;
8
8
9
9
export class Translator {
10
- private placeholderStart = '<!-- README-TRANSLATE-LANGUAGES-START -->' ;
11
- private placeholderEnd = '<!-- README-TRANSLATE-LANGUAGES-END -->' ;
12
-
13
10
private credentials : CredentialsConfig ;
14
11
private config : Config ;
15
12
private logger : Logger ;
13
+
16
14
private crowdin : crowdin ;
15
+ private writer : Writer ;
17
16
18
17
constructor ( credentials : CredentialsConfig , config : Config , logger : Logger ) {
19
18
this . credentials = credentials ;
@@ -24,21 +23,24 @@ export class Translator {
24
23
token : this . credentials . token ,
25
24
organization : this . credentials . organization
26
25
} ) ;
26
+
27
+ this . writer = new Writer ( credentials , config , logger ) ;
27
28
}
28
29
29
30
public async translate ( ) : Promise < void > {
30
31
this . logger . log ( 'info' , 'Start...' ) ;
31
32
32
- const switcher = await this . renderSwitcher ( ) ;
33
+ const project = await this . crowdin . projectsGroupsApi . getProject ( this . credentials . projectId ) ;
34
+ this . writer . setProjectLanguages ( project . data . targetLanguages ) ;
33
35
34
- await this . addLanguageSwitcher ( this . config . file , switcher ) ;
36
+ await this . writer . addLanguageSwitcher ( this . config . file ) ;
35
37
await this . uploadSources ( ) ;
36
38
37
39
const translationsUrl = await this . downloadTranslations ( ) ;
38
40
const translationFiles = await downloadZipAndUnzip ( translationsUrl , this . config . destination ) ;
39
41
40
42
translationFiles . map ( async ( file : string ) : Promise < void > => {
41
- await this . addLanguageSwitcher ( `${ this . config . destination } /${ file } ` , switcher ) ;
43
+ await this . writer . addLanguageSwitcher ( `${ this . config . destination } /${ file } ` ) ;
42
44
} ) ;
43
45
44
46
this . logger . log ( 'info' , 'Done!' ) ;
@@ -96,46 +98,6 @@ export class Translator {
96
98
}
97
99
}
98
100
99
- private async addLanguageSwitcher ( file : string , switcher : string ) : Promise < void > {
100
- if ( ! this . config . languageSwitcher ) {
101
- return ;
102
- }
103
-
104
- this . logger . log ( 'info' , `Adding language switcher to ${ file } ...` ) ;
105
-
106
- let fileContents = fs . readFileSync ( toPlatformPath ( file ) ) . toString ( ) ;
107
-
108
- if ( ! fileContents . includes ( this . placeholderStart ) || ! fileContents . includes ( this . placeholderEnd ) ) {
109
- this . logger . log (
110
- 'warning' ,
111
- `Skipped! Please add ${ this . placeholderStart } and ${ this . placeholderEnd } to your README.md`
112
- ) ;
113
-
114
- return ;
115
- }
116
-
117
- const sliceFrom = fileContents . indexOf ( this . placeholderStart ) + this . placeholderStart . length ;
118
- const sliceTo = fileContents . indexOf ( this . placeholderEnd ) ;
119
-
120
- fileContents = `${ fileContents . slice ( 0 , sliceFrom ) } \n${ switcher } \n${ fileContents . slice ( sliceTo ) } ` ;
121
-
122
- this . logger . log ( 'debug' , fileContents ) ;
123
-
124
- fs . writeFileSync ( toPlatformPath ( file ) , fileContents ) ;
125
- }
126
-
127
- private async renderSwitcher ( ) : Promise < string > {
128
- const project = await this . crowdin . projectsGroupsApi . getProject ( this . credentials . projectId ) ;
129
-
130
- let languages : string [ ] = [ ] ;
131
-
132
- project . data . targetLanguages . map ( language => {
133
- languages . push ( `[${ language . name } ](${ this . config . destination } /README.${ language . locale } .md)` ) ;
134
- } ) ;
135
-
136
- return languages . join ( ' | ' ) ;
137
- }
138
-
139
101
private async downloadTranslations ( ) : Promise < string > {
140
102
this . logger . log ( 'info' , 'Downloading translations...' ) ;
141
103
@@ -172,7 +134,7 @@ export class Translator {
172
134
private async getFile ( ) : Promise < SourceFilesModel . File | undefined > {
173
135
const files = await this . crowdin . sourceFilesApi . listProjectFiles ( this . credentials . projectId ) ;
174
136
175
- const sourceFile = files . data . find ( file => file . data . path === this . getFilePath ( ) ) ;
137
+ const sourceFile = files . data . find ( file => file . data . path === this . getCrowdinFilePath ( ) ) ;
176
138
177
139
return sourceFile ?. data ;
178
140
}
@@ -185,7 +147,7 @@ export class Translator {
185
147
return crowdinBranch ?. data ;
186
148
}
187
149
188
- private getFilePath ( ) : string {
150
+ private getCrowdinFilePath ( ) : string {
189
151
const fileName = baseName ( this . config . file ) ;
190
152
191
153
if ( this . config . branch ) {
0 commit comments