-
Notifications
You must be signed in to change notification settings - Fork 2
/
JSWriter.go
88 lines (77 loc) · 2.47 KB
/
JSWriter.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
package main
import (
"os"
"sort"
)
/*
Writes the JS StringCheese api for a given StringKey struct
*/
func writeJSFiles(rootValue *StringKeys, values []*StringKeys, config *StringCheeseConfig) error {
pathToJSFile := config.pathToJSFolder + getJSFileName(config)
_ = os.Remove(pathToJSFile) //ignore error
//if err != nil {
// return err
//}
file, err := os.Create(pathToJSFile)
if err != nil {
return err
}
writeJSFileHeader(file, rootValue, values, config)
for _, value := range values {
writeJSClass(file,value,config)
}
file.Close()
return nil
}
func writeJSFileHeader(file *os.File, rootValue *StringKeys, values []*StringKeys, config *StringCheeseConfig) {
file.WriteString(config.timeStampString +
"\n" +
"//Generated String Cheese JS file" +
"\n" +
"/**\n" +
" * Returns a localized set of strings.\n" +
" * @param {string} languageShortName - A short language id (ex: 'en').\n" +
" * @returns {")
for _, value := range values {
file.WriteString(getJSClassName(value,config)+"|")
}
file.WriteString("}\n"+
" * @default returns {"+getJSClassName(rootValue,config)+"}\n" +
" */\n" +
"function CreateStringCheeseWithLanguageShort(languageShortName) {\n" +
" switch(languageShortName) {\n")
for _, value := range values {
file.WriteString(" case '" + value.languageId + "': return new " + getJSClassName(value, config)+ "();\n")
}
file.WriteString(" default: return new " + getJSClassName(rootValue, config)+ "();\n" +
" }\n"+
"}\n")
}
func writeJSClass(file *os.File, value *StringKeys, config *StringCheeseConfig) {
file.WriteString("\n" +
"//Generating " + value.languageId + " String Cheese class\n" +
"class " + getJSClassName(value, config) +" { \n")
valueMap := value.strings
useArgs := config.shouldCreateArguments
keys := make([]string,0)
for key, _ := range valueMap {
keys = append(keys,key)
}
sort.Strings(keys)
for _, key := range keys {
value := valueMap[key]
if useArgs && value.numberOfArguments > 0 {
file.WriteString(" get raw_"+ jsTransformKeyToJSVarName(value.originalKey) +"() {" +
" return '" + value.formatString + "';" +
" }\n" )
file.WriteString(" format_"+ jsTransformKeyToJSVarName(value.originalKey) +"("+value.argumentString+") {" +
" return `" + value.formatString + "`;" +
" }\n" )
} else {
file.WriteString(" get "+ jsTransformKeyToJSVarName(value.originalKey) +"() {" +
" return '" + value.value + "';" +
" }\n" )
}
}
file.WriteString("}")
}