BPicture: Fix archive constructor.
[haiku.git] / src / kits / locale / MessageFormat.cpp
blob274d4ac8a7b2cef879221063810497d00c0e1614
1 /*
2 * Copyright 2014-2015 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Adrien Destugues, pulkomandy@pulkomandy.tk
7 * John Scipione, jscipione@gmail.com
8 */
10 #include <unicode/uversion.h>
11 #include <MessageFormat.h>
13 #include <Autolock.h>
14 #include <FormattingConventionsPrivate.h>
15 #include <LanguagePrivate.h>
17 #include <ICUWrapper.h>
19 #include <unicode/msgfmt.h>
22 BMessageFormat::BMessageFormat(const BLanguage& language, const BString pattern)
23 : BFormat(language, BFormattingConventions())
25 _Initialize(UnicodeString::fromUTF8(pattern.String()));
29 BMessageFormat::BMessageFormat(const BString pattern)
30 : BFormat()
32 _Initialize(UnicodeString::fromUTF8(pattern.String()));
36 BMessageFormat::~BMessageFormat()
38 delete fFormatter;
42 status_t
43 BMessageFormat::InitCheck()
45 return fInitStatus;
49 status_t
50 BMessageFormat::Format(BString& output, const int64 arg) const
52 if (fInitStatus != B_OK)
53 return fInitStatus;
55 UnicodeString buffer;
56 UErrorCode error = U_ZERO_ERROR;
58 Formattable arguments[] = {
59 (int64)arg
62 FieldPosition pos;
63 buffer = fFormatter->format(arguments, 1, buffer, pos, error);
64 if (!U_SUCCESS(error))
65 return B_ERROR;
67 BStringByteSink byteSink(&output);
68 buffer.toUTF8(byteSink);
70 return B_OK;
74 status_t
75 BMessageFormat::_Initialize(const UnicodeString& pattern)
77 fInitStatus = B_OK;
78 UErrorCode error = U_ZERO_ERROR;
79 Locale* icuLocale
80 = BLanguage::Private(&fLanguage).ICULocale();
82 fFormatter = new MessageFormat(pattern, *icuLocale, error);
84 if (fFormatter == NULL)
85 fInitStatus = B_NO_MEMORY;
87 if (!U_SUCCESS(error)) {
88 delete fFormatter;
89 fInitStatus = B_ERROR;
90 fFormatter = NULL;
93 return fInitStatus;