-
Notifications
You must be signed in to change notification settings - Fork 2
/
Config.go
403 lines (337 loc) · 15.1 KB
/
Config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package main
import (
"encoding/csv"
"errors"
"flag"
"fmt"
"os"
"sort"
"strings"
"time"
)
/*
Config.go handles creating the config struct that works to configure how the strings are generated
CONFIG OPTIONS ARE:
*/
const LANGUAGE_ID_NONE = "ROOT"
const LANGUAGE_ID_NONE_NAME = "en"
//Base translation files
const CONFIG_ARG_SPREAD_SHEET_PATH = "s"
const MESSAGE_SPREAD_SHEET_PATH = "REQUIRED (if no -a)\n" +
" If `csvLangs` is set, path to your CSV file.\n" +
" Otherwise, path to your spread sheet folder.\n" +
" Ex: /Users/me/workspace/project/translation/spreadsheets/\n" +
" Ex: /Users/me/workspace/project/translation/spreadsheets/base.csv\n"
const CONFIG_ARG_SPREAD_SHEET_NUMBER_OF_LANGUAGES = "csvLangNum"
const DEFAULT_VALUE_SPREAD_SHEET_NUMBER_OF_LANGUAGES = -1
const MESSAGE_SPREAD_SHEET_NUMBER_OF_LANGUAGES = "Optional\n" +
" Set this if translated text exists in one CSV rather than a separate CSV for each language\n" +
" Set this to the number of languages in your CSV file\n" +
" Value must be higher than 0\n" +
" Ex: Your CSV includes `en`, `fr`, and `es`, the value should be 3\n"
//Android related
const CONFIG_ARG_ANDROID_RES = "a" //REQUIRED
const MESSAGE_ANDROID_RES = "REQUIRED (if no -s)\n" +
" Path to your Android res folder.\n" +
" Ex: /Users/me/workspace/project/android/project_name/app/src/main/res\n"
const CONFIG_ARG_NAME_OF_STRING_XML_FILE = "s_xml" //Optional
const MESSAGE_NAME_OF_STRING_XML_FILE = "Optional\n" +
" Name of your xml file containing strings.\n"
const DEFAULT_VALUE_NAME_OF_STRING_XML_FILE = "strings" //.xml
const CONFIG_ARG_ROOT_LANGUAGE = "lang" //Optional
const MESSAGE_ROOT_LANGUAGE = "Optional\n" +
" Id of language to use as the base string xml file.\n" +
" For example en if you want the file 'values-en/strings.xml' to be used as the root language.\n" +
" If left blank the default folder will be values/strings.xml\n"
const DEFAULT_VALUE_ROOT_LANGUAGE = LANGUAGE_ID_NONE
const CONFIG_ARG_ROOT_LANGUAGE_ID = "langId" //Optional
const MESSAGE_ROOT_LANGUAGE_ID = "Optional\n" +
" Id of language to use as the base id for -lang if none is specified\n" +
" This value will be overwritten by -lang\n" +
" For right now this is only used with Dart\n"
const DEFAULT_VALUE_ROOT_LANGUAGE_ID = LANGUAGE_ID_NONE_NAME
//const CONFIG_ARG_ROOT_LANGUAGE_ID = "langId" //Optional
//const MESSAGE_ROOT_LANGUAGE_ID = "Optional\n" +
// "Id of root language. This will override -lang's value when outputting to "
//const DEFAULT_VALUE_ROOT_LANGUAGE_ID = LANGUAGE_ID_NONE_NAME
//iOS related
const CONFIG_ARG_XCODE_PROJECT = "i" //Optional REQUIRED FOR IOS
const MESSAGE_XCODE_PROJECT = "Optional, REQUIRED to output to iOS\n" +
" Root folder of your iOS project. This is where the .strings files will be created.\n"
const CONFIG_ARG_NAME_OF_XCODE_DOT_STRING_FILE = "dotName" //Optional
const MESSAGE_NAME_OF_XCODE_DOT_STRING_FILE = "Optional\n" +
" Name of the .strings file.\n"
const DEFAULT_VALUE_NAME_OF_XCODE_DOT_STRING_FILE = "String"
const CONFIG_ARG_SHOULD_CREATE_SWIFT_KEYS = "swift" //Optional
const MESSAGE_SHOULD_CREATE_SWIFT_KEYS = "Optional\n" +
" If a swift key file should be generated.\n"
const DEFAULT_VALUE_SHOULD_CREATE_SWIFT_KEYS = true //this is relative to XCODE_PROJECT
const CONFIG_ARG_PATH_TO_SWIFT_FILE = "swiftLoc" //Optional
const MESSAGE_PATH_TO_SWIFT_FILE = "Optional\n" +
" Relative path inside the XCode project where the Swift StringCheese key class will be generated.\n" +
" If empty, this will generate it at the root of the project.\n"
const DEFAULT_VALUE_PATH_TO_SWIFT_FILE = "" //this is relative to XCODE_PROJECT
//Kotlin Map related
const CONFIG_ARG_PATH_TO_KOTLIN_PROJECT = "kt" //Optional
const MESSAGE_PATH_TO_KOTLIN_PROJECT = "Optional, REQUIRED to output to a Kotlin Class \n" +
" Where the kotlin class will be generated\n" +
" For now it will always be apart of the org.stringcheese package\n"
//Kotlin Map related
const CONFIG_ARG_PATH_TO_KOTLIN_MAP_PROJECT = "km" //Optional
const MESSAGE_PATH_TO_KOTLIN_MAP_PROJECT = "Optional, REQUIRED to output to a Kotlin Map<String, String> \n" +
" Where the kotlin map class will be generated\n" +
" For now it will always be apart of the org.stringcheese package\n"
//Dart related
const CONFIG_ARG_PATH_TO_DART_PROJECT = "dart" //Optional
const MESSAGE_PATH_TO_DART_PROJECT = "Optional, REQUIRED to output to Dart\n" +
" Root folder of your Dart project. This is where the Dart StringCheese classes will be generated\n"
//Javascript related
const CONFIG_ARG_PATH_TO_JS_PROJECT = "js" //Optional
const MESSAGE_PATH_TO_JS_PROJECT = "Optional, REQUIRED to output to JavaScript\n" +
" Where the JavaScript files will be generated.\n" +
" Generates ES6 compatible JS.\n"
//general
const CONFIG_ARG_LOG_MISSING_STRINGS = "logMissing" //Optional
const MESSAGE_LOG_MISSING_STRINGS = "Optional\n" +
" Logs to the console if there are any missing strings in your strings.xml files\n"
const DEFAULT_VALUE_LOG_MISSING_STRINGS = true
const CONFIG_ARG_REDUCE_KEYS = "reduce" //Optional
const MESSAGE_REDUCE_KEYS = "Optional\n" +
" Reduces keys in outputted key classes\n"
const DEFAULT_VALUE_REDUCE_KEYS = true
const CONFIG_ARG_KEY_HAS_ARGUMENTS = "useArgs" //Optional
const MESSAGE_KEY_HAS_ARGUMENTS = "Optional\n" +
" If the key class generated generates a special argument method for strings with format points.\n"
const DEFAULT_VALUE_KEY_HAS_ARGUMENTS = true
const CONFIG_ARG_CLASS_IS_STATIC = "static" //Optional
const MESSAGE_CLASS_IS_STATIC = "Optional\n" +
" If the generated key file is static\n"
const DEFAULT_VALUE_CLASS_IS_STATIC = false
const CONFIG_ARG_SKIP_NON_VALID_LANGUAGE_ID = "useValidLangId" //Optional
const MESSAGE_SKIP_NON_VALID_LANGUAGE_ID = "Optional\n" +
" Skips a values-LANGUAGE_ID folder if the id is not valid\n"
const DEFAULT_VALUE_SKIP_NON_VALID_LANGUAGE_ID = false
const CONFIG_ARG_CLASS_NAME = "className" //Optional
const MESSAGE_CLASS_NAME = "Optional\n" +
" The name of the Class that will be generated\n"
const DEFAULT_VALUE_CLASS_NAME = "StringCheese"
const CONFIG_ARG_OBJ_C_SUPPORT = "objCSupport" //Optional
const MESSAGE_OBJ_C_SUPPORT = "Optional\n" +
" Enables the Swift key class and methods to be compatible with ObjC\n"
const DEFAULT_VALUE_OBJ_C_SUPPORT = false
const NO_VALUE_FROM_FLAG = "NONE"
/*
Processes CLI arguments
*/
func parseAndGetConfig() (*StringCheeseConfig, error) {
//android related
pathSpreadSheet := flag.String(CONFIG_ARG_SPREAD_SHEET_PATH, NO_VALUE_FROM_FLAG, MESSAGE_SPREAD_SHEET_PATH)
spreadSheetNumberOfLanguages := flag.Int(CONFIG_ARG_SPREAD_SHEET_NUMBER_OF_LANGUAGES, DEFAULT_VALUE_SPREAD_SHEET_NUMBER_OF_LANGUAGES, MESSAGE_SPREAD_SHEET_NUMBER_OF_LANGUAGES)
pathToAndroidRes := flag.String(CONFIG_ARG_ANDROID_RES, NO_VALUE_FROM_FLAG, MESSAGE_ANDROID_RES)
nameOfXMLFile := flag.String(CONFIG_ARG_NAME_OF_STRING_XML_FILE, DEFAULT_VALUE_NAME_OF_STRING_XML_FILE, MESSAGE_NAME_OF_STRING_XML_FILE)
//ios
iOSProjectRoot := flag.String(CONFIG_ARG_XCODE_PROJECT, NO_VALUE_FROM_FLAG, MESSAGE_XCODE_PROJECT)
nameOfDotStrings := flag.String(CONFIG_ARG_NAME_OF_XCODE_DOT_STRING_FILE, DEFAULT_VALUE_NAME_OF_XCODE_DOT_STRING_FILE, MESSAGE_NAME_OF_XCODE_DOT_STRING_FILE)
createSwiftKey := flag.Bool(CONFIG_ARG_SHOULD_CREATE_SWIFT_KEYS, DEFAULT_VALUE_SHOULD_CREATE_SWIFT_KEYS, MESSAGE_SHOULD_CREATE_SWIFT_KEYS)
pathToSwift := flag.String(CONFIG_ARG_PATH_TO_SWIFT_FILE, DEFAULT_VALUE_PATH_TO_SWIFT_FILE, MESSAGE_PATH_TO_SWIFT_FILE)
className := flag.String(CONFIG_ARG_CLASS_NAME, DEFAULT_VALUE_CLASS_NAME, MESSAGE_CLASS_NAME)
objCSupport := flag.Bool(CONFIG_ARG_OBJ_C_SUPPORT, DEFAULT_VALUE_OBJ_C_SUPPORT, MESSAGE_OBJ_C_SUPPORT)
//kotlin
kotlinProject := flag.String(CONFIG_ARG_PATH_TO_KOTLIN_PROJECT, NO_VALUE_FROM_FLAG, CONFIG_ARG_PATH_TO_KOTLIN_PROJECT)
//kotlin Map
kotlinMapProject := flag.String(CONFIG_ARG_PATH_TO_KOTLIN_MAP_PROJECT, NO_VALUE_FROM_FLAG, CONFIG_ARG_PATH_TO_KOTLIN_MAP_PROJECT)
//dart
dartProject := flag.String(CONFIG_ARG_PATH_TO_DART_PROJECT, NO_VALUE_FROM_FLAG, MESSAGE_PATH_TO_DART_PROJECT)
//javascript
javaScriptProject := flag.String(CONFIG_ARG_PATH_TO_JS_PROJECT, NO_VALUE_FROM_FLAG, MESSAGE_PATH_TO_JS_PROJECT)
//general
rootLanguage := flag.String(CONFIG_ARG_ROOT_LANGUAGE, DEFAULT_VALUE_ROOT_LANGUAGE, MESSAGE_ROOT_LANGUAGE)
rootLanguageIfIfNone := flag.String(CONFIG_ARG_ROOT_LANGUAGE_ID, DEFAULT_VALUE_ROOT_LANGUAGE_ID, MESSAGE_ROOT_LANGUAGE_ID)
logMissingStrings := flag.Bool(CONFIG_ARG_LOG_MISSING_STRINGS, DEFAULT_VALUE_LOG_MISSING_STRINGS, MESSAGE_LOG_MISSING_STRINGS)
reduceKeys := flag.Bool(CONFIG_ARG_REDUCE_KEYS, DEFAULT_VALUE_REDUCE_KEYS, MESSAGE_REDUCE_KEYS)
keyClassesHaveArgs := flag.Bool(CONFIG_ARG_KEY_HAS_ARGUMENTS, DEFAULT_VALUE_KEY_HAS_ARGUMENTS, MESSAGE_KEY_HAS_ARGUMENTS)
staticClassKey := flag.Bool(CONFIG_ARG_CLASS_IS_STATIC, DEFAULT_VALUE_CLASS_IS_STATIC, MESSAGE_CLASS_IS_STATIC)
skipNonValidLanguageIds := flag.Bool(CONFIG_ARG_SKIP_NON_VALID_LANGUAGE_ID, DEFAULT_VALUE_SKIP_NON_VALID_LANGUAGE_ID, MESSAGE_SKIP_NON_VALID_LANGUAGE_ID)
flag.Parse()
if *pathToAndroidRes == NO_VALUE_FROM_FLAG && *pathSpreadSheet == NO_VALUE_FROM_FLAG {
return nil, errors.New("Did not include path to either your android res folder or folder of spread sheets.\n" +
"Ex: ./StringCheese -a /Users/me/workspace/androidApp/app/src/main/res")
}
if *iOSProjectRoot == NO_VALUE_FROM_FLAG &&
*dartProject == NO_VALUE_FROM_FLAG &&
*kotlinProject == NO_VALUE_FROM_FLAG &&
*kotlinMapProject == NO_VALUE_FROM_FLAG &&
*javaScriptProject == NO_VALUE_FROM_FLAG {
return nil, errors.New("Did not include path to an iOS, Kotlin map, JS, or Dart project folder.\n" +
"Ex: ./StringCheese -a /Users/me/workspace/iOSAPP/iOSAPP")
}
var usingRootLanguageId = rootLanguageIfIfNone
//if (*rootLanguage == LANGUAGE_ID_NONE) {
// usingRootLanguageId = rootLanguage
//}
//if *defaultLang == NO_VALUE_FROM_FLAG {
// *defaultLang = DEFAULT_LANGUAGE_ID
//}
timeStamp := "// Last generated at: " + time.Now().UTC().String() + "\n"
config := StringCheeseConfig{
timeStampString: timeStamp,
rootLanguageId: *rootLanguage,
rootLanguageIdToUse: *usingRootLanguageId,
//spreadsheet
pathToSpreadSheetFolder: *pathSpreadSheet,
shouldUseSpreadSheetForStrings: *pathSpreadSheet != NO_VALUE_FROM_FLAG,
spreadSheetValuesAllInOneSheet: *spreadSheetNumberOfLanguages >= 1,
spreadSheetNumberOfLanguages: *spreadSheetNumberOfLanguages,
//android
pathToAndroidRes: *pathToAndroidRes,
nameOfXMLStringFile: *nameOfXMLFile,
//ios
translatingToIOS: *iOSProjectRoot != NO_VALUE_FROM_FLAG,
pathToIOSProject: *iOSProjectRoot,
nameOfDotStringFile: *nameOfDotStrings,
shouldCreateSwiftKey: *createSwiftKey,
pathToSwiftKey: *pathToSwift,
objCSupport: *objCSupport,
//kotlin
translatingToKotlin: *kotlinProject != NO_VALUE_FROM_FLAG,
pathToKotlinFolder: *kotlinProject,
//kotlin
translatingToKotlinMap: *kotlinMapProject != NO_VALUE_FROM_FLAG,
pathToKotlinFolderMap: *kotlinMapProject,
//dart
translatingToDart: *dartProject != NO_VALUE_FROM_FLAG,
pathToDartFile: *dartProject,
//JS
translatingToJS: *javaScriptProject != NO_VALUE_FROM_FLAG,
pathToJSFolder: *javaScriptProject,
//general
className: *className,
logMissingStrings: *logMissingStrings,
reduceKeys: *reduceKeys,
shouldCreateArguments: *keyClassesHaveArgs,
createStaticKeyClass: *staticClassKey,
skipNonValidLanguageIds: *skipNonValidLanguageIds,
}
return &config, nil
}
//Gets the path to the where the language's .strings file should be located
func (config *StringCheeseConfig) dotStringFileWithLanguageId(languageId string) string {
if languageId == LANGUAGE_ID_NONE {
return config.pathToIOSProject + "/Base.lproj/" + config.nameOfDotStringFile + ".strings"
}
return config.pathToIOSProject + "/" + strings.Title(languageId) + ".lproj/" + config.nameOfDotStringFile + ".strings"
}
func GetLanguageIdsFromCSVHeader(config *StringCheeseConfig) ([]string, error) {
fileReader, err := os.Open(config.pathToSpreadSheetFolder)
if err != nil {
return nil, err
}
var reader = csv.NewReader(fileReader)
line, err := reader.Read()
if err != nil {
return nil, err
} else if len(line) <= config.spreadSheetNumberOfLanguages {
return nil, errors.New("CSV is smaller than the expected")
}
languageIds := []string{}
for i := CSV_POSITION_VALUE; i < config.spreadSheetNumberOfLanguages+1; i++ {
langId := strings.ToLower(line[i])
if CheckIfValidLanguageCode(langId) || !config.skipNonValidLanguageIds {
languageIds = append(languageIds, langId)
}
}
return languageIds, nil
}
//gets all the language IDs for a translation
func (config *StringCheeseConfig) getAllValueFoldersLanguageIds() ([]string, error) {
var languageIds []string
if config.spreadSheetValuesAllInOneSheet {
idsFromCSV, err := GetLanguageIdsFromCSVHeader(config)
if err != nil {
return nil, err
}
languageIds = idsFromCSV
} else if config.shouldUseSpreadSheetForStrings {
languageIds = []string{}
res, err := os.Open(config.pathToSpreadSheetFolder)
if err != nil {
return nil, err
}
fileNames, err := res.Readdirnames(0)
for _, name := range fileNames {
if strings.Contains(name, "_sheet") {
s := strings.Split(name, "_")
if len(s) > 0 {
langId := s[0]
if CheckIfValidLanguageCode(langId) || !config.skipNonValidLanguageIds {
languageIds = append(languageIds, s[0])
}
}
}
}
} else {
languageIds = []string{}
res, err := os.Open(config.pathToAndroidRes)
if err != nil {
return nil, err
}
fileNames, err := res.Readdirnames(0)
for _, name := range fileNames {
if strings.Contains(name, "values") {
s := strings.Split(name, "-")
if len(s) > 1 {
langId := s[1]
if CheckIfValidLanguageCode(langId) || !config.skipNonValidLanguageIds {
languageIds = append(languageIds, s[1])
}
}
}
}
}
sort.Strings(languageIds)
fmt.Println("Exporting: ", strings.Join(languageIds, ", "))
return languageIds, nil
}
/*
Config Struct
*/
type StringCheeseConfig struct {
//base
timeStampString string
rootLanguageId string
rootLanguageIdToUse string
//spreadsheets
pathToSpreadSheetFolder string
shouldUseSpreadSheetForStrings bool
spreadSheetValuesAllInOneSheet bool
spreadSheetNumberOfLanguages int
//android
pathToAndroidRes string
nameOfXMLStringFile string
//ios
translatingToIOS bool
pathToIOSProject string
nameOfDotStringFile string
shouldCreateSwiftKey bool
pathToSwiftKey string
objCSupport bool
//kotlin
translatingToKotlin bool
pathToKotlinFolder string
//kotlin
translatingToKotlinMap bool
pathToKotlinFolderMap string
//dart
translatingToDart bool
pathToDartFile string
//dart
translatingToJS bool
pathToJSFolder string
//general
className string
shouldCreateArguments bool
createStaticKeyClass bool
reduceKeys bool
logMissingStrings bool
skipNonValidLanguageIds bool
}