5
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
72
|
3 |
All Rights Reserved
|
5
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
0
|
12 |
|
|
13 |
Dictionary subclass:#ResourcePack
|
72
|
14 |
instanceVariableNames:'elements dependents packsClassName'
|
|
15 |
classVariableNames:'Packs'
|
|
16 |
poolDictionaries:''
|
118
|
17 |
category:'Interface-Support'
|
0
|
18 |
!
|
|
19 |
|
5
|
20 |
ResourcePack comment:'
|
46
|
21 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
72
|
22 |
All Rights Reserved
|
54
|
23 |
|
145
|
24 |
$Header: /cvs/stx/stx/libview/ResourcePack.st,v 1.16 1995-05-17 12:22:31 claus Exp $
|
46
|
25 |
'!
|
5
|
26 |
|
46
|
27 |
!ResourcePack class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
72
|
32 |
All Rights Reserved
|
5
|
33 |
|
46
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
5
|
42 |
|
46
|
43 |
version
|
|
44 |
"
|
145
|
45 |
$Header: /cvs/stx/stx/libview/ResourcePack.st,v 1.16 1995-05-17 12:22:31 claus Exp $
|
46
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
|
51 |
to allow easy customization of smalltalk code (i.e. internationalization)
|
50
|
52 |
(replaces the obsolete Resource class).
|
145
|
53 |
ResourcePacks are class specific, meaning that every subclass of View
|
|
54 |
and ApplicationModel has an instance of ResourcePack (called resources)
|
|
55 |
which is created when the first instance of the view is created.
|
54
|
56 |
|
50
|
57 |
The resourcePack consists of a mapping from strings to values, which are
|
|
58 |
then used in labels, buttons, menus etc.
|
|
59 |
The resourcePack initializes itself from a file found in 'resources/<className>.rs',
|
|
60 |
where 'className' is built by the usual abbreviation mechanism (see abbrev-files).
|
|
61 |
Conditional mappings are possible, by including lines as:
|
72
|
62 |
#if <expression>
|
|
63 |
#endif
|
50
|
64 |
in the resourcefile. Example:
|
|
65 |
file 'foo.rs':
|
72
|
66 |
#if Language == #german
|
|
67 |
'abort' 'Abbruch'
|
|
68 |
#endif
|
|
69 |
#if Language == #french
|
|
70 |
'abort' 'canceller'
|
|
71 |
#endif
|
145
|
72 |
|
|
73 |
the corresponding resource-strings are accessed (from methods within the class)
|
|
74 |
using:
|
|
75 |
|
72
|
76 |
resource string:'abort'
|
145
|
77 |
|
54
|
78 |
returning the mapped string (i.e. 'Abbruch' if the global Language is set
|
|
79 |
to #german)..
|
145
|
80 |
|
54
|
81 |
If no corresponding entry is found in the resources, the key is returned.
|
|
82 |
Translations can also include arguments, such as:
|
72
|
83 |
resources string:'really delete %1' with:fileName
|
145
|
84 |
|
|
85 |
This scheme has the advantage, that you can write your programs using your
|
|
86 |
native language strings. Later, when new languages are to be supported,
|
|
87 |
simply create a resource file for the class and add translations for
|
|
88 |
all those strings. (find the keys by looking at users of resource or senders
|
|
89 |
of 'string:*'.
|
|
90 |
|
|
91 |
More languages can be added later without any change in the code,
|
|
92 |
recompilation or the like.
|
|
93 |
Even by people without access to the source code i.e. which only have the
|
|
94 |
applications binary.
|
|
95 |
|
|
96 |
Also, unsupported languages' strings are simply left unchanged - if you
|
|
97 |
write your application in (say) english, and only plan to use it in english,
|
|
98 |
no additional work is required (i.e you dont need a resource file then).
|
|
99 |
Strings for unknown languages will come in english
|
|
100 |
(which is better than nothing or empty button labels ;-)
|
|
101 |
|
|
102 |
Summary:
|
|
103 |
in subclasses of View and ApplicationModel,
|
|
104 |
instead of writing:
|
|
105 |
|
|
106 |
...
|
|
107 |
b := Button label:'press me'
|
|
108 |
...
|
|
109 |
|
|
110 |
always write:
|
|
111 |
|
|
112 |
...
|
|
113 |
b := Button label:(resources string:'press me')
|
|
114 |
...
|
|
115 |
|
|
116 |
if your class is not a subclass of one of the above, AND you need
|
|
117 |
resource translations, you wont inherit the resources variable
|
|
118 |
(which is automatically initialized).
|
|
119 |
In this case, you have to ask the ResourcePack class explicitely for
|
|
120 |
a corresponding package:
|
|
121 |
|
|
122 |
ResourcePack for:aClassName
|
|
123 |
|
|
124 |
as an example, see how the Date class gets the national names of
|
|
125 |
week & monthnames.
|
|
126 |
"
|
|
127 |
!
|
|
128 |
|
|
129 |
examples
|
|
130 |
"
|
|
131 |
normally, resources are found in files named after their classes sourcefile
|
|
132 |
For example, the FileBrowsers resources are found in 'FBrowser.rs'.
|
|
133 |
For the examples below, we process resources from a constant string;
|
|
134 |
this is NOT representative.
|
|
135 |
|
|
136 |
|stream res|
|
|
137 |
|
|
138 |
stream := ReadStream on:'
|
|
139 |
foo ''the translation for foo''
|
|
140 |
#if Language == #german
|
|
141 |
bar ''die deutsche uebersetzung von bar''
|
|
142 |
baz ''baz hat den Wert %1''
|
|
143 |
#endif
|
|
144 |
#if Language == #french
|
|
145 |
bar ''bar en francaise''
|
|
146 |
baz ''%1, c''''est baz''
|
|
147 |
#endif
|
|
148 |
|
|
149 |
'.
|
|
150 |
|
|
151 |
res := ResourcePack new readFromResourceStream:stream in:nil.
|
|
152 |
|
|
153 |
Transcript showCr:'baz is translated to: ' , (res string:'baz' with:'1234').
|
|
154 |
Transcript showCr:'bar is translated to: ' , (res string:'bar').
|
|
155 |
Transcript showCr:'foo is translated to: ' , (res string:'foo').
|
|
156 |
Transcript showCr:'fooBar is translated to: ' , (res string:'fooBar').
|
|
157 |
|
|
158 |
set the Language to french:
|
|
159 |
|
|
160 |
Language := #french
|
|
161 |
|
|
162 |
and repeat the above.
|
|
163 |
|
|
164 |
Language := #english
|
|
165 |
Language := #german
|
46
|
166 |
"
|
|
167 |
! !
|
5
|
168 |
|
0
|
169 |
!ResourcePack class methodsFor:'initialization'!
|
|
170 |
|
|
171 |
initialize
|
24
|
172 |
Packs isNil ifTrue:[
|
72
|
173 |
Packs := WeakArray new:30
|
0
|
174 |
].
|
|
175 |
|
|
176 |
"ResourcePack initialize"
|
24
|
177 |
!
|
|
178 |
|
72
|
179 |
flushCachedResourcePacks
|
24
|
180 |
"forget all cached resources - needed after a style change"
|
|
181 |
|
|
182 |
Packs := nil.
|
|
183 |
self initialize
|
|
184 |
|
72
|
185 |
"ResourcePack flushCachedResourcePacks"
|
0
|
186 |
! !
|
|
187 |
|
54
|
188 |
!ResourcePack class methodsFor:'private'!
|
|
189 |
|
|
190 |
addToCache:aPack
|
|
191 |
|idx|
|
0
|
192 |
|
54
|
193 |
idx := Packs identityIndexOf:nil.
|
|
194 |
idx == 0 ifTrue:[
|
72
|
195 |
"
|
|
196 |
throw away oldest
|
|
197 |
"
|
|
198 |
idx := Packs size.
|
|
199 |
Packs replaceFrom:1 to:idx-1 with:Packs startingAt:2.
|
54
|
200 |
].
|
|
201 |
Packs at:idx put:aPack
|
|
202 |
!
|
0
|
203 |
|
54
|
204 |
searchCacheFor:aClassname
|
|
205 |
|sz|
|
0
|
206 |
|
24
|
207 |
Packs isNil ifTrue:[
|
72
|
208 |
self initialize.
|
|
209 |
^ nil
|
0
|
210 |
].
|
54
|
211 |
|
|
212 |
sz := Packs size.
|
|
213 |
1 to:sz do:[:idx |
|
72
|
214 |
|aPack|
|
54
|
215 |
|
72
|
216 |
aPack := Packs at:idx.
|
|
217 |
aPack notNil ifTrue:[
|
|
218 |
aClassname = aPack packsClassName ifTrue:[
|
|
219 |
"
|
|
220 |
bring to end for LRU
|
|
221 |
"
|
|
222 |
Packs replaceFrom:idx to:sz-1 with:Packs startingAt:idx+1.
|
|
223 |
Packs at:sz put:aPack.
|
|
224 |
^ aPack
|
|
225 |
]
|
|
226 |
]
|
0
|
227 |
].
|
54
|
228 |
^ nil
|
123
|
229 |
|
|
230 |
"
|
|
231 |
ResourcePack searchCacheFor:'TextView'
|
|
232 |
"
|
54
|
233 |
! !
|
|
234 |
|
|
235 |
!ResourcePack class methodsFor:'instance creation'!
|
|
236 |
|
|
237 |
fromFile:aFileName directory:dirName
|
|
238 |
"get the resource definitions from a file in a directory.
|
|
239 |
Uncached low-level entry."
|
|
240 |
|
72
|
241 |
|newPack|
|
0
|
242 |
|
2
|
243 |
newPack := self new.
|
72
|
244 |
newPack readFromFile:aFileName directory:dirName.
|
0
|
245 |
^ newPack
|
50
|
246 |
!
|
0
|
247 |
|
50
|
248 |
fromFile:aFileName
|
54
|
249 |
"get the resource definitions from a file in the default directory.
|
|
250 |
Uncached low-level entry."
|
50
|
251 |
|
|
252 |
^ self fromFile:aFileName directory:'resources'
|
|
253 |
|
|
254 |
"
|
|
255 |
ResourcePack fromFile:'SBrowser.rs'
|
|
256 |
ResourcePack fromFile:'FBrowser.rs'
|
|
257 |
ResourcePack fromFile:'Smalltalk.rs'
|
|
258 |
"
|
0
|
259 |
!
|
|
260 |
|
|
261 |
for:aClass
|
54
|
262 |
"get the full resource definitions for aClass (i.e. with super packs).
|
|
263 |
Also leave the resulting pack in the cache for faster access next time."
|
0
|
264 |
|
54
|
265 |
|nm pack superPack|
|
|
266 |
|
|
267 |
nm := aClass name.
|
|
268 |
pack := self searchCacheFor:nm.
|
|
269 |
pack notNil ifTrue:[^ pack].
|
0
|
270 |
|
54
|
271 |
pack := self fromFile:(Smalltalk fileNameForClass:nm) , '.rs'.
|
|
272 |
aClass == Object ifFalse:[
|
72
|
273 |
superPack := self for:(aClass superclass).
|
|
274 |
pack := pack merge:superPack
|
0
|
275 |
].
|
54
|
276 |
pack packsClassName:nm.
|
|
277 |
self addToCache:pack.
|
|
278 |
^ pack
|
123
|
279 |
|
|
280 |
"
|
|
281 |
ResourcePack for:TextView
|
|
282 |
ResourcePack for:CodeView
|
|
283 |
ResourcePack for:Workspace
|
|
284 |
Workspace classResources
|
|
285 |
"
|
0
|
286 |
! !
|
|
287 |
|
|
288 |
!ResourcePack methodsFor:'merging'!
|
|
289 |
|
|
290 |
merge:anotherPack
|
54
|
291 |
anotherPack keysAndValuesDo:[:key :value |
|
72
|
292 |
(self includesKey:key) ifFalse:[
|
|
293 |
self at:key put:value
|
|
294 |
]
|
0
|
295 |
]
|
|
296 |
! !
|
|
297 |
|
|
298 |
!ResourcePack methodsFor:'accessing'!
|
|
299 |
|
|
300 |
dependents:aCollection
|
|
301 |
dependents := aCollection
|
|
302 |
!
|
|
303 |
|
2
|
304 |
dependents
|
|
305 |
^ dependents
|
0
|
306 |
!
|
|
307 |
|
54
|
308 |
packsClassName
|
|
309 |
^ packsClassName
|
0
|
310 |
!
|
|
311 |
|
54
|
312 |
packsClassName:aString
|
|
313 |
packsClassName := aString
|
0
|
314 |
!
|
|
315 |
|
2
|
316 |
at:aKey
|
123
|
317 |
"translate a string"
|
|
318 |
|
72
|
319 |
^ self at:aKey ifAbsent:aKey
|
0
|
320 |
!
|
|
321 |
|
|
322 |
at:aKey default:default
|
123
|
323 |
"translate a string"
|
|
324 |
|
72
|
325 |
^ self at:aKey ifAbsent:default
|
0
|
326 |
!
|
|
327 |
|
2
|
328 |
name:aKey
|
123
|
329 |
"translate a string"
|
|
330 |
|
72
|
331 |
^ self at:aKey ifAbsent:aKey
|
2
|
332 |
!
|
|
333 |
|
|
334 |
name:aKey default:default
|
123
|
335 |
"translate a string"
|
|
336 |
|
72
|
337 |
^ self at:aKey ifAbsent:default
|
2
|
338 |
!
|
|
339 |
|
0
|
340 |
array:anArray
|
123
|
341 |
"translate a collection of strings"
|
|
342 |
|
0
|
343 |
^ anArray collect:[:r | self at:r default:r]
|
|
344 |
!
|
|
345 |
|
|
346 |
string:s
|
123
|
347 |
"translate a string"
|
|
348 |
|
72
|
349 |
^ self at:s ifAbsent:s
|
0
|
350 |
!
|
|
351 |
|
|
352 |
string:s with:arg
|
123
|
353 |
"translate and expand arg"
|
|
354 |
|
0
|
355 |
^ self string:s withArgs:(Array with:arg)
|
|
356 |
!
|
|
357 |
|
|
358 |
string:s with:arg1 with:arg2
|
123
|
359 |
"translate and expand args"
|
|
360 |
|
0
|
361 |
^ self string:s withArgs:(Array with:arg1 with:arg2)
|
|
362 |
!
|
|
363 |
|
|
364 |
string:s withArgs:argArray
|
123
|
365 |
"translate and expand args"
|
|
366 |
|
135
|
367 |
|template|
|
0
|
368 |
|
|
369 |
template := self at:s ifAbsent:[s].
|
135
|
370 |
^ template expandPlaceholdersWith:argArray
|
0
|
371 |
! !
|
|
372 |
|
|
373 |
!ResourcePack methodsFor:'file reading'!
|
|
374 |
|
72
|
375 |
readFromFile:fileName directory:dirName
|
145
|
376 |
"read definitions from a file in a directory"
|
|
377 |
|
72
|
378 |
|inStream|
|
|
379 |
|
135
|
380 |
dirName = 'resources' ifTrue:[
|
|
381 |
inStream := Smalltalk resourceFileStreamFor:fileName
|
|
382 |
] ifFalse:[
|
|
383 |
inStream := Smalltalk systemFileStreamFor:(dirName , '/' , fileName).
|
|
384 |
].
|
72
|
385 |
inStream isNil ifTrue:[
|
|
386 |
"
|
|
387 |
an empty pack
|
|
388 |
"
|
|
389 |
^ nil
|
|
390 |
].
|
|
391 |
self readFromResourceStream:inStream in:dirName.
|
|
392 |
inStream close.
|
|
393 |
!
|
|
394 |
|
144
|
395 |
processLine:aLine
|
145
|
396 |
"process a single valid line (i.e. #ifdef & #include has already been processed)"
|
|
397 |
|
144
|
398 |
|lineString name stream l rest macroName value|
|
|
399 |
|
145
|
400 |
lineString := aLine withoutSeparators.
|
144
|
401 |
name := nil.
|
|
402 |
(lineString at:1) == $' ifTrue:[
|
|
403 |
stream := ReadStream on:lineString.
|
|
404 |
name := String
|
|
405 |
readFrom:stream
|
145
|
406 |
onError:[('RESOURCEPACK: invalid line <',lineString,'>') errorPrintNL. nil].
|
144
|
407 |
l := stream position.
|
|
408 |
|
|
409 |
"/ l := lineString indexOf:$' startingAt:2.
|
|
410 |
"/ l ~~ 0 ifTrue:[
|
|
411 |
"/ name := (lineString copyFrom:2 to:l-1).
|
|
412 |
"/ l := l + 1
|
|
413 |
"/ ]
|
|
414 |
] ifFalse:[
|
|
415 |
l := lineString indexOfSeparatorStartingAt:1.
|
|
416 |
l ~~ 0 ifTrue:[
|
|
417 |
name := lineString copyFrom:1 to:l-1.
|
|
418 |
]
|
|
419 |
].
|
|
420 |
name notNil ifTrue:[
|
|
421 |
rest := (lineString copyFrom:l) withoutSeparators.
|
|
422 |
"
|
|
423 |
skip <type> if present
|
|
424 |
"
|
|
425 |
(rest startsWith:$<) ifTrue:[
|
|
426 |
l := lineString indexOf:$> startingAt:l.
|
|
427 |
rest := (lineString copyFrom:l+1) withoutSeparators.
|
|
428 |
].
|
|
429 |
(rest startsWith:$=) ifTrue:[
|
|
430 |
rest := rest copyFrom:2.
|
|
431 |
stream := ReadStream on:rest.
|
|
432 |
macroName := stream nextAlphaNumericWord.
|
|
433 |
rest := stream upToEnd.
|
|
434 |
value := self at:macroName.
|
|
435 |
value := Compiler evaluate:('self ' , rest)
|
|
436 |
receiver:value
|
|
437 |
notifying:nil
|
|
438 |
compile:false.
|
|
439 |
] ifFalse:[
|
|
440 |
value := Compiler evaluate:rest compile:false.
|
|
441 |
(value == #Error) ifTrue:[
|
|
442 |
Transcript show:('error in resource:' , name).
|
|
443 |
]
|
|
444 |
].
|
|
445 |
self at:name put:value.
|
|
446 |
]
|
|
447 |
!
|
|
448 |
|
72
|
449 |
readFromResourceStream:inStream in:dirName
|
145
|
450 |
"read definitions from a stream. THe dirName argument is required to
|
|
451 |
specify where #include files are searched for"
|
|
452 |
|
|
453 |
|lineString rest value ifLevel skipping first|
|
0
|
454 |
|
|
455 |
ifLevel := 0.
|
|
456 |
skipping := false.
|
|
457 |
[inStream atEnd] whileFalse:[
|
72
|
458 |
lineString := inStream nextLine.
|
|
459 |
(lineString notNil and:[lineString isEmpty not]) ifTrue:[
|
|
460 |
first := lineString at:1.
|
|
461 |
first == $; ifFalse:[
|
|
462 |
first == $# ifTrue:[
|
|
463 |
lineString := (lineString copyFrom:2) withoutSpaces.
|
|
464 |
|
|
465 |
(lineString startsWith:'if') ifTrue:[
|
|
466 |
skipping ifFalse:[
|
|
467 |
rest := lineString copyFrom:3.
|
109
|
468 |
value := Compiler evaluate:rest compile:false.
|
72
|
469 |
(value == #Error) ifTrue:[
|
|
470 |
Transcript show:('error in resource:' , lineString).
|
|
471 |
].
|
|
472 |
(value == false) ifTrue:[
|
|
473 |
skipping := true
|
|
474 |
]
|
|
475 |
].
|
|
476 |
ifLevel := ifLevel + 1
|
|
477 |
] ifFalse:[
|
|
478 |
(lineString startsWith:'endif') ifTrue:[
|
|
479 |
ifLevel := ifLevel - 1.
|
|
480 |
ifLevel == 0 ifTrue:[
|
|
481 |
skipping := false
|
|
482 |
]
|
|
483 |
] ifFalse:[
|
|
484 |
(lineString startsWith:'else') ifTrue:[
|
|
485 |
skipping := skipping not
|
|
486 |
] ifFalse:[
|
|
487 |
(lineString startsWith:'include') ifTrue:[
|
|
488 |
rest := lineString copyFrom:8.
|
109
|
489 |
value := Compiler evaluate:rest compile:false.
|
72
|
490 |
self readFromFile:value directory:dirName
|
|
491 |
]
|
|
492 |
]
|
|
493 |
]
|
|
494 |
].
|
|
495 |
] ifFalse:[
|
|
496 |
skipping ifFalse:[
|
145
|
497 |
self processLine:lineString
|
72
|
498 |
]
|
|
499 |
]
|
|
500 |
]
|
|
501 |
]
|
0
|
502 |
].
|
|
503 |
! !
|
|
504 |
|
|
505 |
ResourcePack initialize!
|