@@ -42,14 +42,53 @@ function toJS(txt) {
42
42
return js ;
43
43
}
44
44
45
+ // Translate any strings in the app that are prefixed with /*LANG*/
46
+ // see https://github.com/espruino/BangleApps/issues/136
47
+ function translateJS ( options , app , code ) {
48
+ var lex = Espruino . Core . Utils . getLexer ( code ) ;
49
+ var outjs = "" ;
50
+ var lastIdx = 0 ;
51
+ var tok = lex . next ( ) ;
52
+ while ( tok !== undefined ) {
53
+ var previousString = code . substring ( lastIdx , tok . startIdx ) ;
54
+ var tokenString = code . substring ( tok . startIdx , tok . endIdx ) ;
55
+ if ( tok . type == "STRING" && previousString . includes ( "/*LANG*/" ) ) {
56
+ previousString = previousString . replace ( "/*LANG*/" , "" ) ;
57
+ var language = options . language ;
58
+ if ( language [ app . id ] && language [ app . id ] [ tok . value ] ) {
59
+ tokenString = JSON . stringify ( language . GLOBAL [ tok . value ] ) ;
60
+ } else if ( language . GLOBAL [ tok . value ] ) {
61
+ tokenString = JSON . stringify ( language . GLOBAL [ tok . value ] ) ;
62
+ } else {
63
+ // Unhandled translation...
64
+ //console.log("Untranslated ",tokenString);
65
+ }
66
+ }
67
+ outjs += previousString + tokenString ;
68
+ lastIdx = tok . endIdx ;
69
+ tok = lex . next ( ) ;
70
+ }
71
+
72
+ /*console.log("==================== IN");
73
+ console.log(code);
74
+ console.log("==================== OUT");
75
+ console.log(outjs);*/
76
+ return outjs ;
77
+ }
78
+
45
79
// Run JS through EspruinoTools to pull in modules/etc
46
- function parseJS ( storageFile , options ) {
80
+ function parseJS ( storageFile , options , app ) {
47
81
if ( storageFile . url && storageFile . url . endsWith ( ".js" ) && ! storageFile . url . endsWith ( ".min.js" ) ) {
48
82
// if original file ends in '.js'...
83
+ var js = storageFile . content ;
84
+ // check for language translations
85
+ if ( options . language )
86
+ js = translateJS ( options , app , js ) ;
87
+ // handle modules
49
88
let localModulesURL = "modules" ;
50
89
if ( typeof window !== "undefined" )
51
90
localModulesURL = window . location . origin + window . location . pathname . replace ( / [ ^ / ] * $ / , "" ) + "modules" ;
52
- return Espruino . transform ( storageFile . content , {
91
+ return Espruino . transform ( js , {
53
92
SET_TIME_ON_WRITE : false ,
54
93
PRETOKENISE : options . settings . pretokenise ,
55
94
MODULE_URL : localModulesURL + "|https://www.espruino.com/modules" ,
@@ -69,6 +108,7 @@ var AppInfo = {
69
108
fileGetter : callback for getting URL,
70
109
settings : global settings object
71
110
device : { id : ..., version : ... } info about the currently connected device
111
+ language : object of translations, eg 'lang/de_DE.json'
72
112
}
73
113
*/
74
114
getFiles : ( app , options ) => {
@@ -91,7 +131,7 @@ var AppInfo = {
91
131
92
132
Promise . all ( appFiles . map ( storageFile => {
93
133
if ( storageFile . content !== undefined )
94
- return Promise . resolve ( storageFile ) . then ( storageFile => parseJS ( storageFile , options ) ) ;
134
+ return Promise . resolve ( storageFile ) . then ( storageFile => parseJS ( storageFile , options , app ) ) ;
95
135
else if ( storageFile . url )
96
136
return options . fileGetter ( `apps/${ app . id } /${ storageFile . url } ` ) . then ( content => {
97
137
return {
@@ -100,7 +140,7 @@ var AppInfo = {
100
140
content : content ,
101
141
evaluate : storageFile . evaluate ,
102
142
noOverwrite : storageFile . noOverwrite
103
- } } ) . then ( storageFile => parseJS ( storageFile , options ) ) ;
143
+ } } ) . then ( storageFile => parseJS ( storageFile , options , app ) ) ;
104
144
else return Promise . resolve ( ) ;
105
145
} ) ) . then ( fileContents => { // now we just have a list of files + contents...
106
146
// filter out empty files
0 commit comments