author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Thu, 07 Jul 2011 23:08:07 +0100 | |
branch | jv |
changeset 17845 | 7e0cfaac936d |
parent 17841 | 7abcc4aef871 |
child 17846 | 24edc476ac18 |
permissions | -rw-r--r-- |
6117 | 1 |
"{ Package: 'stx:libbasic' }" |
5996 | 2 |
|
3 |
Object subclass:#UserMessage |
|
4 |
instanceVariableNames:'defaultString key catalogID' |
|
5 |
classVariableNames:'' |
|
6 |
poolDictionaries:'' |
|
7 |
category:'Interface-Internationalization' |
|
8 |
! |
|
9 |
||
6008 | 10 |
!UserMessage class methodsFor:'documentation'! |
11 |
||
12 |
documentation |
|
13 |
" |
|
14 |
added for vw5i compatibility, which accesses messageCatalogs |
|
15 |
via: |
|
16 |
(#key << #catalogID >> 'defaultMessage') |
|
17 |
which creates an instance of UserMessage. |
|
18 |
||
19 |
Currently, this is a dummy operation in ST/X, however it is mapped onto |
|
20 |
the resource mechanism, if the given catalogID is the name of a class; |
|
21 |
i.e. |
|
22 |
(YesNoBox classResources string:'continue') |
|
17841 | 23 |
|
6008 | 24 |
can now also be written as: |
17841 | 25 |
|
26 |
(#continue << YesNoBox) asString |
|
27 |
(#continue << #YesNoBox >> 'Continue really') asString |
|
28 |
" |
|
29 |
! |
|
30 |
||
31 |
examples |
|
32 |
" |
|
33 |
Use a class as catalog: |
|
34 |
[exBegin] |
|
35 |
self warn:(#continue << YesNoBox) asString |
|
36 |
[exEnd] |
|
37 |
Use a class name as catalog: |
|
38 |
[exBegin] |
|
39 |
self warn:(#continue << #YesNoBox) asString |
|
40 |
[exEnd] |
|
41 |
Some default text if the symbol is not present in the catalog: |
|
42 |
[exBegin] |
|
43 |
self warn:(#continue << #YesNoBox >> 'Default for continue') asString. |
|
44 |
self warn:(#continueRRRRRRR << #YesNoBox >> 'Default for continue') asString |
|
45 |
[exEnd] |
|
46 |
Here we inherit from the top catalog: |
|
47 |
[exBegin] |
|
48 |
self information:(Time now printStringFormat:(#TIMEFORMAT << self >> 'Resolved via default %h:%m:%s') asString) |
|
49 |
[exEnd] |
|
50 |
||
51 |
Can also use a package's catalog: |
|
52 |
[exBegin] |
|
53 |
self information:((#'WARN_RENAME' << #'stx:libtool' ) |
|
54 |
withCRs bindWith:'ARG1' with:'ARG2') |
|
55 |
[exEnd] |
|
56 |
||
57 |
Lazy resolving the catalog in a block |
|
58 |
(if you generate the messages at startup and want to recognize language changes): |
|
59 |
[exBegin] |
|
60 |
self information:((#'WARN_RENAME' << [ Tools::NewSystemBrowser classResources ] ) |
|
61 |
withCRs bindWith:'ARG1' with:'ARG2') |
|
62 |
[exEnd] |
|
6008 | 63 |
" |
64 |
! ! |
|
5996 | 65 |
|
66 |
!UserMessage class methodsFor:'instance creation'! |
|
67 |
||
68 |
key:aKeySymbol catalogID:aCatalogSymbol |
|
69 |
^ self new key:aKeySymbol catalogID:aCatalogSymbol |
|
70 |
! |
|
71 |
||
72 |
key:aKeySymbol defaultString:aString |
|
73 |
^ self new key:aKeySymbol defaultString:aString |
|
74 |
! ! |
|
75 |
||
17841 | 76 |
!UserMessage methodsFor:'Compatibility-V''Age'! |
77 |
||
78 |
bindWith:aString |
|
79 |
"return a copy of the receiver, where a '%1' escape is |
|
80 |
replaced by aString. |
|
81 |
This has been added for VisualAge compatibility." |
|
82 |
||
83 |
^ self expandPlaceholdersWith:(Array with:aString) |
|
84 |
||
85 |
" |
|
86 |
'do you like %1 ?' bindWith:'smalltalk' |
|
87 |
" |
|
88 |
! |
|
89 |
||
90 |
bindWith:string1 with:string2 |
|
91 |
"return a copy of the receiver, where a '%1' escape is |
|
92 |
replaced by string1 and '%2' is replaced by string2. |
|
93 |
This has been added for VisualAge compatibility." |
|
94 |
||
95 |
^ self expandPlaceholdersWith:(Array with:string1 with:string2) |
|
96 |
||
97 |
" |
|
98 |
'do you prefer %1 or rather %2 ?' |
|
99 |
bindWith:'smalltalk' with:'c++' |
|
100 |
" |
|
101 |
! |
|
102 |
||
103 |
bindWith:str1 with:str2 with:str3 |
|
104 |
"return a copy of the receiver, where a '%1', '%2' and '%3' escapes |
|
105 |
are replaced by str1, str2 and str3 respectively. |
|
106 |
This has been added for VisualAge compatibility." |
|
107 |
||
108 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 with:str3) |
|
109 |
||
110 |
" |
|
111 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
112 |
bindWith:'smalltalk' with:'c++' with:'c' |
|
113 |
" |
|
114 |
! |
|
115 |
||
116 |
bindWith:str1 with:str2 with:str3 with:str4 |
|
117 |
"return a copy of the receiver, where a '%1', '%2', '%3' and '%4' escapes |
|
118 |
are replaced by str1, str2, str3 and str4 respectively. |
|
119 |
This has been added for VisualAge compatibility." |
|
120 |
||
121 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 with:str3 with:str4) |
|
122 |
||
123 |
" |
|
124 |
'do you prefer %1 or rather %2 (not talking about %3 or even %4) ?' |
|
125 |
bindWith:'smalltalk' with:'c++' with:'c' with:'assembler' |
|
126 |
" |
|
127 |
! |
|
128 |
||
129 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 |
|
130 |
"return a copy of the receiver, where a '%1' .. '%5' escapes |
|
131 |
are replaced by str1 .. str5 respectively. |
|
132 |
This has been added for VisualAge compatibility." |
|
133 |
||
134 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 with:str3 with:str4 with:str5) |
|
135 |
||
136 |
"Created: 31.1.1997 / 16:25:42 / cg" |
|
137 |
! |
|
138 |
||
139 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6 |
|
140 |
"return a copy of the receiver, where a '%1' .. '%6' escapes |
|
141 |
are replaced by str1 .. str6 respectively. |
|
142 |
This has been added for VisualAge compatibility." |
|
143 |
||
144 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 |
|
145 |
with:str3 with:str4 |
|
146 |
with:str5 with:str6) |
|
147 |
! |
|
148 |
||
149 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6 with:str7 |
|
150 |
"return a copy of the receiver, where a '%1' .. '%7' escapes |
|
151 |
are replaced by str1 .. str7 respectively. |
|
152 |
This has been added for VisualAge compatibility." |
|
153 |
||
154 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 |
|
155 |
with:str3 with:str4 |
|
156 |
with:str5 with:str6 |
|
157 |
with:str7) |
|
158 |
! |
|
159 |
||
160 |
bindWithArguments:anArrayOfStrings |
|
161 |
"return a copy of the receiver, where a '%i' escape |
|
162 |
is replaced by the coresponding string from the argument array. |
|
163 |
'i' may be between 1 and 9 (i.e. a maximum of 9 placeholders is allowed). |
|
164 |
This has been added for VisualAge compatibility." |
|
165 |
||
166 |
^ self expandPlaceholdersWith:anArrayOfStrings |
|
167 |
||
168 |
" |
|
169 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
170 |
bindWithArguments:#('smalltalk' 'c++' 'c') |
|
171 |
||
172 |
'do you %(what) ?' |
|
173 |
bindWithArguments:(Dictionary new at:#'what' put:'understand'; yourself) |
|
174 |
" |
|
175 |
! ! |
|
176 |
||
5996 | 177 |
!UserMessage methodsFor:'accessing'! |
178 |
||
179 |
catalogID |
|
180 |
^ catalogID |
|
181 |
! |
|
182 |
||
183 |
catalogID:aCatalogSymbol |
|
184 |
catalogID := aCatalogSymbol |
|
185 |
! |
|
186 |
||
187 |
defaultString |
|
188 |
^ defaultString |
|
189 |
! |
|
190 |
||
191 |
defaultString:aString |
|
192 |
defaultString := aString |
|
193 |
! |
|
194 |
||
195 |
key |
|
196 |
^ key |
|
197 |
! |
|
198 |
||
199 |
key:aKeySymbol |
|
200 |
key := aKeySymbol. |
|
201 |
! |
|
202 |
||
203 |
key:aKeySymbol catalogID:aCatalogSymbol |
|
204 |
key := aKeySymbol. |
|
205 |
catalogID := aCatalogSymbol |
|
206 |
! |
|
207 |
||
208 |
key:aKeySymbol defaultString:aString |
|
209 |
key := aKeySymbol. |
|
210 |
defaultString := aString |
|
211 |
! ! |
|
212 |
||
213 |
!UserMessage methodsFor:'converting'! |
|
214 |
||
215 |
asString |
|
17841 | 216 |
"convert the user message to a string. |
217 |
If there us no mapping for the user message - |
|
218 |
for now: return the defaultString, ignoring the catalogID." |
|
5996 | 219 |
|
220 |
|str| |
|
221 |
||
222 |
str := self lookupInMessageCatalog. |
|
223 |
str notNil ifTrue:[ ^ str ]. |
|
224 |
defaultString notNil ifTrue:[ |
|
225 |
^ defaultString |
|
226 |
]. |
|
227 |
^ key asString |
|
6117 | 228 |
! |
229 |
||
17841 | 230 |
string |
231 |
^ self asString |
|
232 |
! ! |
|
233 |
||
234 |
!UserMessage methodsFor:'printing & storing'! |
|
235 |
||
236 |
displayOn:aStream |
|
237 |
(aStream isStream not and:[aStream ~~ Transcript]) ifTrue:[ |
|
238 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
|
239 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC. |
|
240 |
self asString displayOn:aStream. |
|
241 |
^ self |
|
242 |
]. |
|
243 |
||
244 |
key storeOn:aStream. |
|
245 |
aStream nextPutAll:' << '. |
|
246 |
catalogID storeOn:aStream. |
|
247 |
defaultString notNil ifTrue:[ |
|
248 |
aStream nextPutAll:' >> '. |
|
249 |
defaultString storeOn:aStream. |
|
250 |
]. |
|
251 |
! |
|
252 |
||
253 |
printOn:aStream |
|
254 |
aStream nextPutAll:self asString |
|
255 |
! ! |
|
256 |
||
257 |
!UserMessage methodsFor:'special string converting'! |
|
258 |
||
6117 | 259 |
expandMacros |
260 |
^ self asString expandMacros |
|
261 |
||
262 |
! |
|
263 |
||
264 |
expandMacrosWith:arg1 |
|
265 |
^ self asString expandMacrosWith:arg1 |
|
266 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
267 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
268 |
|
17841 | 269 |
expandPlaceholdersWith:argArrayOrDictionary |
270 |
^ self asString expandPlaceholdersWith:argArrayOrDictionary |
|
271 |
! |
|
6591 | 272 |
|
17841 | 273 |
expandPlaceholdersWith:argArrayOrDictionary on:aStream |
274 |
^ self asString expandPlaceholdersWith:argArrayOrDictionary on:aStream |
|
275 |
! |
|
6591 | 276 |
|
17841 | 277 |
withCRs |
278 |
^ self asString withCRs |
|
6591 | 279 |
! ! |
280 |
||
5996 | 281 |
!UserMessage methodsFor:'utilities'! |
282 |
||
283 |
<< aSymbol |
|
284 |
"set the catalogID" |
|
285 |
||
286 |
self catalogID:aSymbol |
|
287 |
! |
|
288 |
||
289 |
>> aString |
|
290 |
"set the default string" |
|
291 |
||
292 |
self defaultString:aString |
|
293 |
||
294 |
" |
|
295 |
(#theFooMessage << #myMessages >> 'cannot read subclass of metaclass') |
|
296 |
" |
|
297 |
! |
|
298 |
||
299 |
lookupInMessageCatalog |
|
17841 | 300 |
|messageCatalog resolvedMessageCatalog| |
5996 | 301 |
|
17841 | 302 |
"catalogID may be a block" |
303 |
messageCatalog := resolvedMessageCatalog :=catalogID value. |
|
5996 | 304 |
|
17841 | 305 |
"for now - handle the case that the catalogID is |
306 |
a classes name; in that case, ask its resourcePack." |
|
307 |
messageCatalog isSymbol ifTrue:[ |
|
308 |
resolvedMessageCatalog := Smalltalk at:messageCatalog. |
|
309 |
resolvedMessageCatalog isNil ifTrue:[ |
|
310 |
"no class, so it is a packageID" |
|
311 |
resolvedMessageCatalog := ResourcePack forPackage:messageCatalog cached:true. |
|
312 |
]. |
|
6887 | 313 |
]. |
17841 | 314 |
resolvedMessageCatalog isBehavior ifTrue:[ |
315 |
resolvedMessageCatalog := resolvedMessageCatalog classResources. |
|
5996 | 316 |
]. |
17841 | 317 |
(resolvedMessageCatalog isNil) ifTrue:[ |
6887 | 318 |
defaultString isNil ifTrue:[ |
17841 | 319 |
^ 'Non-existant message: (%1<<%2)' bindWith:key with:catalogID printString. |
6887 | 320 |
]. |
321 |
^ defaultString. |
|
322 |
]. |
|
323 |
||
17841 | 324 |
^ resolvedMessageCatalog at:key ifAbsent:nil. |
5996 | 325 |
|
326 |
" |
|
17841 | 327 |
(#'WARN_RENAME' << #BrowserView >> 'A class named %1 already exists (in ''%2'')\\that class will no longer be visible (i.e. removed) if you continue.\\Continue ?' ) asString |
328 |
(#'WARN_RENAME' << #BrowserView) asString |
|
329 |
(#'WARN_RENAME' << BrowserView classResources) asString |
|
330 |
(#'WARN_RENAME' << [ BrowserView classResources] ) asString |
|
331 |
(#'WARN_RENAME' << #'stx:libtool' ) asString |
|
6887 | 332 |
(#dontKnow << #nonExistantMessageCatalog) asString |
17841 | 333 |
(#dontKnow << [YesNoBox]) asString |
334 |
(#dontKnow << #nonExistantMessageCatalog >> 'Default text') asString |
|
5996 | 335 |
" |
336 |
! ! |
|
337 |
||
338 |
!UserMessage class methodsFor:'documentation'! |
|
339 |
||
340 |
version |
|
17845 | 341 |
^ '$Id: UserMessage.st 10656 2011-07-07 22:08:07Z vranyj1 $' |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
342 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
343 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
344 |
version_CVS |
17845 | 345 |
^ '§Header: /cvs/stx/stx/libbasic/UserMessage.st,v 1.7 2011/05/09 13:59:06 stefan Exp §' |
346 |
! ! |