1 //%2006////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
11 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
21 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //==============================================================================
32 //%/////////////////////////////////////////////////////////////////////////////
34 #ifndef Pegasus_MessageLoader_h
35 #define Pegasus_MessageLoader_h
39 #include <Pegasus/Common/Linkage.h>
40 #include <Pegasus/Common/Config.h>
41 #include <Pegasus/Common/String.h>
42 #include <Pegasus/Common/Formatter.h>
43 #include <Pegasus/Common/AcceptLanguageList.h>
44 #include <Pegasus/Common/ContentLanguageList.h>
47 #ifdef PEGASUS_HAS_ICU
48 # include <unicode/uloc.h>
49 # include <unicode/ures.h>
50 # include <unicode/umsg.h>
51 # include <unicode/ucnv.h>
52 # include <unicode/fmtable.h>
53 # include <unicode/msgfmt.h>
56 #ifdef PEGASUS_HAS_ICU
57 # define NO_ICU_MAGIC (UResourceBundle*)0xDEADBEEF
59 # define NO_ICU_MAGIC (void*)0xDEADBEEF
62 PEGASUS_NAMESPACE_BEGIN
65 MessageLoaderParms class is basically a stuct class containing public
66 variables that control the way MessageLoader behaves. MessageLoader uses
67 the fields in this class to decide where and how to load messages from
68 the message resources.
70 class PEGASUS_COMMON_LINKAGE MessageLoaderParms
75 String msg_id: unique message identifier for a particular
76 message in a message resource
81 String default_msg: the default message to use if a message
82 cannot be loaded from a message resource
87 String msg_src_path: this path tells MessageLoader where to
88 find message resources.
89 It can be empty, fully qualified or relative to $PEGASUS_HOME
94 AcceptLanguageList acceptlanguages: This contains the languages
95 that are acceptable by the caller of MessageLoader::getMessage()
96 or openMessageFile(). That is, MessageLoader will do its best to
97 return a message in a language that was specified in this container.
98 This container is naturally ordered using the quality values
99 attached to the languages and MessageLoader iterates through this
100 container in its natural ordering. This container is used by
101 MessageLoader to load messages if it is not empty.
103 AcceptLanguageList acceptlanguages;
106 ContentLanguageList contentlanguages: This is set by
107 MessageLoader::getMessage() and after a message has been loaded
108 from either a message resource or the default message, or by
109 MessageLoader::openMessageFile() after it has identified and
110 opened a message resource. After the call to
111 MessageLoader::getMessage() or MessageLoader::openMessageFile(),
112 the caller can check the MessageLoaderParms.contentlanguages
113 object to see what MessageLoader set it to. In all cases where a
114 message is returned from MessageLoader::getMessage() or will be
115 returned from MessageLoader::getMessage2(), this field will be
116 set to match the language that the message was (or will be)
119 ContentLanguageList contentlanguages;
122 Boolean useProcessLocale: Default is false, if true, MessageLoader
123 uses the system default language to loads messages from.
125 Boolean useProcessLocale;
128 Boolean useThreadLocale: Default is true, this tells
129 MessageLoader to use the AcceptLanguageList container
130 from the current Pegasus thread.
132 Boolean useThreadLocale;
135 Boolean useICUfallback: Default is false. Only relevant if
136 PEGASUS_HAS_ICU is defined. MessageLoader::getMessage() default
137 behaviour is to extract messages for the langauge exactly
138 matching an available message resource. If this is set to true,
139 the MessageLoader is free to extract a message from a less
140 specific message resource according to its search algorithm.
142 #ifdef PEGASUS_HAS_ICU
143 Boolean useICUfallback;
147 const Formatter::Arg&0-9: These are assigned the various
148 substitutions necessary to properly format the message being
149 extracted. MessageLoader substitutes these in the correct
150 places in the message being returned from
151 MessageLoader::getMessage()
165 MessageLoaderParms();
171 const Formatter::Arg& arg0,
172 const Formatter::Arg& arg1,
173 const Formatter::Arg& arg2,
174 const Formatter::Arg& arg3,
175 const Formatter::Arg& arg4,
176 const Formatter::Arg& arg5 = Formatter::DEFAULT_ARG,
177 const Formatter::Arg& arg6 = Formatter::DEFAULT_ARG,
178 const Formatter::Arg& arg7 = Formatter::DEFAULT_ARG,
179 const Formatter::Arg& arg8 = Formatter::DEFAULT_ARG,
180 const Formatter::Arg& arg9 = Formatter::DEFAULT_ARG);
191 const Formatter::Arg& arg0);
197 const Formatter::Arg& arg0,
198 const Formatter::Arg& arg1);
204 const Formatter::Arg& arg0,
205 const Formatter::Arg& arg1,
206 const Formatter::Arg& arg2);
212 const Formatter::Arg& arg0,
213 const Formatter::Arg& arg1,
214 const Formatter::Arg& arg2,
215 const Formatter::Arg& arg3);
232 /** Converts to string. */
235 ~MessageLoaderParms();
238 #ifdef PEGASUS_HAS_ICU
239 UResourceBundle* _resbundl;
246 friend class MessageLoader;
251 MessageLoader is a static class resposible for looking up messages in
253 For specific behaviour details of this class see the Globalization HOWTO.
255 class PEGASUS_COMMON_LINKAGE MessageLoader
260 Retrieves a message from a message resource
261 @param parms MessageLoaderParms - controls the behaviour of how a
263 @return String - the formatted message
265 static String getMessage(MessageLoaderParms& parms);
268 Opens a message resource bundle.
269 If this method fails for some reason, it will set parms.resbundl
270 to NO_ICU_MAGIC, and a subsequent call to getMessage2() will
271 result in the default message being formatted.
272 ATTN: Do we want *real* error codes for this?
273 @param parms MessageLoaderParms - controls the behaviour of how a
274 message is retrieved, this parameter should be used *ONLY* on
275 subsequent calls to getMessage2() and closeMessageFile().
277 static void openMessageFile(MessageLoaderParms& parms);
280 Closes a message resource bundle.
281 @param parms MessageLoaderParms - identifies a previously opened
282 resource bundle returned from openMessageFile().
284 static void closeMessageFile(MessageLoaderParms& parms);
287 Retrieves a message from a message resource previously opened by
289 @param parms MessageLoaderParms - controls the behaviour of how a
290 message is retrieved, and is the same MessageLoaderParms
291 parameter that was passed to openMessageFile().
292 @return String - the formatted message
294 static String getMessage2(MessageLoaderParms& parms);
296 static void setPegasusMsgHome(String home);
298 static void setPegasusMsgHomeRelative(const String& argv0);
300 static Boolean _useProcessLocale;
302 static Boolean _useDefaultMsg;
304 static AcceptLanguageList _acceptlanguages;
308 static String formatDefaultMessage(MessageLoaderParms& parms);
310 static String getQualifiedMsgPath(String path);
312 static void initPegasusMsgHome(const String& startDir);
314 static void checkDefaultMsgLoading();
316 static String pegasus_MSG_HOME;
318 #ifdef PEGASUS_HAS_ICU
319 static void openICUMessageFile(MessageLoaderParms& parms);
321 static String extractICUMessage(
322 UResourceBundle* resbundl,
323 MessageLoaderParms& parms);
325 static String formatICUMessage(
326 UResourceBundle* resbundl,
329 MessageLoaderParms& parms);
331 static String uChar2String(UChar* uchar_str);
333 static String uChar2String(UChar* uchar_str, int len);
335 static void xferFormattable(Formatter::Arg& arg, Formattable& formattable);
340 PEGASUS_NAMESPACE_END