92
|
1 /*******************************************************************************
|
|
2
|
|
3 @file UMessageFormat.d
|
|
4
|
|
5 Copyright (c) 2004 Kris Bell
|
|
6
|
|
7 This software is provided 'as-is', without any express or implied
|
|
8 warranty. In no event will the authors be held liable for damages
|
|
9 of any kind arising from the use of this software.
|
|
10
|
|
11 Permission is hereby granted to anyone to use this software for any
|
|
12 purpose, including commercial applications, and to alter it and/or
|
|
13 redistribute it freely, subject to the following restrictions:
|
|
14
|
|
15 1. The origin of this software must not be misrepresented; you must
|
|
16 not claim that you wrote the original software. If you use this
|
|
17 software in a product, an acknowledgment within documentation of
|
|
18 said product would be appreciated but is not required.
|
|
19
|
|
20 2. Altered source versions must be plainly marked as such, and must
|
|
21 not be misrepresented as being the original software.
|
|
22
|
|
23 3. This notice may not be removed or altered from any distribution
|
|
24 of the source.
|
|
25
|
|
26 4. Derivative works are permitted, but they must carry this notice
|
|
27 in full and credit the original source.
|
|
28
|
|
29
|
|
30 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
31
|
|
32
|
|
33 @version Initial version, November 2004
|
|
34 @author Kris
|
|
35
|
|
36 Note that this package and documentation is built around the ICU
|
|
37 project (http://oss.software.ibm.com/icu/). Below is the license
|
|
38 statement as specified by that software:
|
|
39
|
|
40
|
|
41 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
42
|
|
43
|
|
44 ICU License - ICU 1.8.1 and later
|
|
45
|
|
46 COPYRIGHT AND PERMISSION NOTICE
|
|
47
|
|
48 Copyright (c) 1995-2003 International Business Machines Corporation and
|
|
49 others.
|
|
50
|
|
51 All rights reserved.
|
|
52
|
|
53 Permission is hereby granted, free of charge, to any person obtaining a
|
|
54 copy of this software and associated documentation files (the
|
|
55 "Software"), to deal in the Software without restriction, including
|
|
56 without limitation the rights to use, copy, modify, merge, publish,
|
|
57 distribute, and/or sell copies of the Software, and to permit persons
|
|
58 to whom the Software is furnished to do so, provided that the above
|
|
59 copyright notice(s) and this permission notice appear in all copies of
|
|
60 the Software and that both the above copyright notice(s) and this
|
|
61 permission notice appear in supporting documentation.
|
|
62
|
|
63 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
64 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
65 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
|
66 OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
67 HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
|
|
68 INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
|
|
69 FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
|
70 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
|
|
71 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
72
|
|
73 Except as contained in this notice, the name of a copyright holder
|
|
74 shall not be used in advertising or otherwise to promote the sale, use
|
|
75 or other dealings in this Software without prior written authorization
|
|
76 of the copyright holder.
|
|
77
|
|
78 ----------------------------------------------------------------------
|
|
79
|
|
80 All trademarks and registered trademarks mentioned herein are the
|
|
81 property of their respective owners.
|
|
82
|
|
83 *******************************************************************************/
|
|
84
|
|
85 module dwtx.dwtxhelper.mangoicu.UMessageFormat;
|
|
86
|
|
87 private import dwtx.dwtxhelper.mangoicu.ICU,
|
|
88 dwtx.dwtxhelper.mangoicu.UString;
|
|
89
|
|
90 public import dwtx.dwtxhelper.mangoicu.ULocale;
|
|
91
|
|
92 /*******************************************************************************
|
|
93
|
|
94 Provides means to produce concatenated messages in language-neutral
|
|
95 way. Use this for all concatenations that show up to end users. Takes
|
|
96 a set of objects, formats them, then inserts the formatted strings into
|
|
97 the pattern at the appropriate places.
|
|
98
|
|
99 See <A HREF="http://oss.software.ibm.com/icu/apiref/umsg_8h.html">
|
|
100 this page</A> for full details.
|
|
101
|
|
102 *******************************************************************************/
|
|
103
|
|
104 class UMessageFormat : ICU
|
|
105 {
|
|
106 private Handle handle;
|
|
107
|
|
108 /***********************************************************************
|
|
109
|
|
110 Open a message formatter with given wchar[] and for the
|
|
111 given locale.
|
|
112
|
|
113 ***********************************************************************/
|
|
114
|
|
115 this (wchar[] pattern, inout ULocale locale = ULocale.Default)
|
|
116 {
|
|
117 UErrorCode e;
|
|
118
|
|
119 handle = umsg_open (pattern.ptr, pattern.length, toString(locale.name), null, e);
|
|
120 testError (e, "failed to open message formatter");
|
|
121 }
|
|
122
|
|
123 /***********************************************************************
|
|
124
|
|
125 Open a message formatter with given pattern and for the
|
|
126 given locale.
|
|
127
|
|
128 ***********************************************************************/
|
|
129
|
|
130 this (UStringView pattern, inout ULocale locale = ULocale.Default)
|
|
131 {
|
|
132 this (pattern.get, locale);
|
|
133 }
|
|
134
|
|
135 /***********************************************************************
|
|
136
|
|
137 Release message formatter
|
|
138
|
|
139 ***********************************************************************/
|
|
140
|
|
141 ~this ()
|
|
142 {
|
|
143 umsg_close (handle);
|
|
144 }
|
|
145
|
|
146 /***********************************************************************
|
|
147
|
|
148 This locale is used for fetching default number or date
|
|
149 format information
|
|
150
|
|
151 ***********************************************************************/
|
|
152
|
|
153 UMessageFormat setLocale (inout ULocale locale)
|
|
154 {
|
|
155 umsg_setLocale (handle, toString(locale.name));
|
|
156 return this;
|
|
157 }
|
|
158
|
|
159 /***********************************************************************
|
|
160
|
|
161 This locale is used for fetching default number or date
|
|
162 format information
|
|
163
|
|
164 ***********************************************************************/
|
|
165
|
|
166 UMessageFormat getLocale (inout ULocale locale)
|
|
167 {
|
|
168 locale.name = toArray (umsg_getLocale (handle));
|
|
169 return this;
|
|
170 }
|
|
171
|
|
172 /***********************************************************************
|
|
173
|
|
174 Sets the pattern
|
|
175
|
|
176 ***********************************************************************/
|
|
177
|
|
178 UMessageFormat setPattern (UStringView pattern)
|
|
179 {
|
|
180 UErrorCode e;
|
|
181
|
|
182 umsg_applyPattern (handle, pattern.get.ptr, pattern.len, null, e);
|
|
183 testError (e, "failed to set formatter pattern");
|
|
184 return this;
|
|
185 }
|
|
186
|
|
187 /***********************************************************************
|
|
188
|
|
189 Gets the pattern
|
|
190
|
|
191 ***********************************************************************/
|
|
192
|
|
193 UMessageFormat getPattern (UString s)
|
|
194 {
|
|
195 uint fmt (wchar* dst, uint length, inout UErrorCode e)
|
|
196 {
|
|
197 return umsg_toPattern (handle, dst, length, e);
|
|
198 }
|
|
199
|
|
200 s.format (&fmt, "failed to get formatter pattern");
|
|
201 return this;
|
|
202 }
|
|
203
|
|
204 /***********************************************************************
|
|
205
|
|
206 This function may perform re-ordering of the arguments
|
|
207 depending on the locale. For all numeric arguments, double
|
|
208 is assumed unless the type is explicitly integer. All choice
|
|
209 format arguments must be of type double.
|
|
210
|
|
211 ***********************************************************************/
|
|
212
|
|
213 UMessageFormat format (UString s, Args* list)
|
|
214 {
|
|
215 uint fmt (wchar* dst, uint length, inout UErrorCode e)
|
|
216 {
|
|
217 return umsg_vformat (handle, dst, length, list.args.ptr, e);
|
|
218 }
|
|
219
|
|
220 s.format (&fmt, "failed to format pattern");
|
|
221 return this;
|
|
222 }
|
|
223
|
|
224
|
|
225 /***********************************************************************
|
|
226
|
|
227 A typesafe list of arguments for the UMessageFormat.format()
|
|
228 method. This should be used in the following manner:
|
|
229
|
|
230 @code
|
|
231 wchar[] format = "{0} {1, number, currency} {2, number, integer}";
|
|
232 UMessageFormat msg = new UMessageFormat (format);
|
|
233
|
|
234 msg.Args args;
|
|
235 msg.format (output, args.add("abc").add(152.0).add(456));
|
|
236 @endcode
|
|
237
|
|
238 Note that the argument order must follow that of the format
|
|
239 string, although the format string may dictate the ultimate
|
|
240 position of each argument.
|
|
241
|
|
242 See http://oss.software.ibm.com/icu/apiref/umsg_8h.html for
|
|
243 details on the format string.
|
|
244
|
|
245 @todo this will likely fail on certain CPU architectures.
|
|
246
|
|
247 ***********************************************************************/
|
|
248
|
|
249 struct Args
|
|
250 {
|
|
251 private uint[32] args;
|
|
252 private uint index;
|
|
253
|
|
254 /***************************************************************
|
|
255
|
|
256 ***************************************************************/
|
|
257
|
|
258 invariant
|
|
259 {
|
|
260 assert (index < args.length);
|
|
261 }
|
|
262
|
|
263 /***************************************************************
|
|
264
|
|
265 ***************************************************************/
|
|
266
|
|
267 Args* reset ()
|
|
268 {
|
|
269 index = 0;
|
|
270 return this;
|
|
271 }
|
|
272
|
|
273 /***************************************************************
|
|
274
|
|
275 ***************************************************************/
|
|
276
|
|
277 Args* add (UStringView x)
|
|
278 {
|
|
279 args[index] = cast(uint) cast(wchar*) x.get();
|
|
280 ++index;
|
|
281 return this;
|
|
282 }
|
|
283
|
|
284 /***************************************************************
|
|
285
|
|
286 ***************************************************************/
|
|
287
|
|
288 Args* add (wchar[] x)
|
|
289 {
|
|
290 args[index] = cast(uint) cast(wchar*) x;
|
|
291 ++index;
|
|
292 return this;
|
|
293 }
|
|
294
|
|
295 /***************************************************************
|
|
296
|
|
297 ***************************************************************/
|
|
298
|
|
299 Args* add (int x)
|
|
300 {
|
|
301 args[index] = x;
|
|
302 ++index;
|
|
303 return this;
|
|
304 }
|
|
305
|
|
306 /***************************************************************
|
|
307
|
|
308 ***************************************************************/
|
|
309
|
|
310 Args* add (double x)
|
|
311 {
|
|
312 *(cast(double*) &args[index]) = x;
|
|
313 index += 2;
|
|
314 return this;
|
|
315 }
|
|
316 }
|
|
317
|
|
318
|
|
319 /***********************************************************************
|
|
320
|
|
321 Bind the ICU functions from a shared library. This is
|
|
322 complicated by the issues regarding D and DLLs on the
|
|
323 Windows platform
|
|
324
|
|
325 ***********************************************************************/
|
|
326
|
|
327 private static void* library;
|
|
328
|
|
329 /***********************************************************************
|
|
330
|
|
331 ***********************************************************************/
|
|
332
|
|
333 private static extern (C)
|
|
334 {
|
|
335 Handle function (wchar*, uint, char*, void*, inout UErrorCode) umsg_open;
|
|
336 void function (Handle) umsg_close;
|
|
337 void function (Handle, char*) umsg_setLocale;
|
|
338 char* function (Handle) umsg_getLocale;
|
|
339 uint function (Handle, wchar*, uint, inout UErrorCode) umsg_toPattern;
|
|
340 void function (Handle, wchar*, uint, void*, inout UErrorCode) umsg_applyPattern;
|
|
341 uint function (Handle, wchar*, uint, void*, inout UErrorCode) umsg_vformat;
|
|
342 }
|
|
343
|
|
344 /***********************************************************************
|
|
345
|
|
346 ***********************************************************************/
|
|
347
|
|
348 static FunctionLoader.Bind[] targets =
|
|
349 [
|
|
350 {cast(void**) &umsg_open, "umsg_open"},
|
|
351 {cast(void**) &umsg_close, "umsg_close"},
|
|
352 {cast(void**) &umsg_setLocale, "umsg_setLocale"},
|
|
353 {cast(void**) &umsg_getLocale, "umsg_getLocale"},
|
|
354 {cast(void**) &umsg_toPattern, "umsg_toPattern"},
|
|
355 {cast(void**) &umsg_applyPattern, "umsg_applyPattern"},
|
|
356 {cast(void**) &umsg_vformat, "umsg_vformat"},
|
|
357 ];
|
|
358
|
|
359 /***********************************************************************
|
|
360
|
|
361 ***********************************************************************/
|
|
362
|
|
363 static this ()
|
|
364 {
|
|
365 library = FunctionLoader.bind (icuin, targets);
|
|
366 //test ();
|
|
367 }
|
|
368
|
|
369 /***********************************************************************
|
|
370
|
|
371 ***********************************************************************/
|
|
372
|
|
373 static ~this ()
|
|
374 {
|
|
375 FunctionLoader.unbind (library);
|
|
376 }
|
|
377
|
|
378 /***********************************************************************
|
|
379
|
|
380 ***********************************************************************/
|
|
381
|
|
382 static void test()
|
|
383 {
|
|
384 UString output = new UString(100);
|
|
385 wchar[] format = "{0} {1, number, currency} {2, number, integer}";
|
|
386
|
|
387 UMessageFormat msg = new UMessageFormat (format);
|
|
388
|
|
389 msg.Args args;
|
|
390 msg.format (output, args.add("abc").add(152.0).add(456));
|
|
391 }
|
|
392 }
|
|
393
|
|
394
|
|
395
|