53 #include <sbIStringBundleService.h>
56 #include <nsIStringBundle.h>
57 #include <nsServiceManagerUtils.h>
67 static PRLogModuleInfo* gStringBundleLog = nsnull;
68 #define TRACE(args) PR_LOG(gStringBundleLog, PR_LOG_DEBUG, args)
69 #define LOG(args) PR_LOG(gStringBundleLog, PR_LOG_WARN, args)
71 #define __FUNCTION__ __PRETTY_FUNCTION__
93 sbStringBundle::sbStringBundle(
const char* aURI)
98 if (!gStringBundleLog) {
99 gStringBundleLog = PR_NewLogModule(
"sbStringBundle");
103 TRACE((
"%s[%8.x] - %s", __FUNCTION__,
this, aURI));
107 NS_ENSURE_SUCCESS(rv, );
112 rv = LoadBundle(aURI);
113 NS_ENSURE_SUCCESS(rv, );
115 nsCOMPtr<nsIStringBundle> defaultBundle;
116 rv = mStringBundleService->GetBundle(getter_AddRefs(defaultBundle));
117 NS_ENSURE_SUCCESS(rv, );
118 rv = LoadBundle(defaultBundle);
119 NS_ENSURE_SUCCESS(rv, );
132 sbStringBundle::sbStringBundle(nsIStringBundle* aBundle)
137 if (!gStringBundleLog) {
138 gStringBundleLog = PR_NewLogModule(
"sbStringBundle");
142 TRACE((
"%s[%8.x] - (existing bundle %.08x)", __FUNCTION__,
this, aBundle));
146 NS_ENSURE_SUCCESS(rv, );
149 rv = LoadBundle(aBundle);
150 NS_ENSURE_SUCCESS(rv, );
173 const nsAString& aDefault)
175 nsString stringValue;
178 TRACE((
"%s[%8.x] - %s (default %s)",
181 NS_ConvertUTF16toUTF8(aKey).
get(),
182 NS_ConvertUTF16toUTF8(aDefault).
get()));
185 if (!aDefault.IsVoid())
186 stringValue = aDefault;
191 nsString bundleString;
192 PRInt32 bundleCount = mBundleList.Count();
193 for (PRInt32
i = 0;
i < bundleCount;
i++) {
194 rv = mBundleList[
i]->GetStringFromName(aKey.BeginReading(),
195 getter_Copies(bundleString));
196 if (NS_SUCCEEDED(rv)) {
197 stringValue = bundleString;
203 ApplySubstitutions(stringValue);
210 const char* aDefault)
212 TRACE((
"%s[%8.x] - %s (default %s)", __FUNCTION__,
this, aKey, aDefault));
217 key = NS_ConvertUTF8toUTF16(aKey);
222 nsAutoString defaultString;
224 defaultString = NS_ConvertUTF8toUTF16(aDefault);
228 return Get(key, defaultString);
247 nsTArray<nsString>& aParams,
248 const nsAString& aDefault)
250 nsString stringValue;
253 TRACE((
"%s[%8.x] - %s (has params) (default %s)",
256 NS_ConvertUTF16toUTF8(aKey).
get(),
257 NS_ConvertUTF16toUTF8(aDefault).
get()));
260 if (!aDefault.IsVoid())
261 stringValue = aDefault;
266 nsTArray<const PRUnichar*> params;
267 PRUint32 paramCount = aParams.Length();
268 for (PRUint32
i = 0;
i < paramCount;
i++) {
269 params.AppendElement(aParams[
i].
get());
273 nsString bundleString;
274 PRInt32 bundleCount = mBundleList.Count();
275 for (PRInt32
i = 0;
i < bundleCount;
i++) {
276 rv = mBundleList[
i]->FormatStringFromName(aKey.BeginReading(),
279 getter_Copies(bundleString));
280 if (NS_SUCCEEDED(rv)) {
281 stringValue = bundleString;
287 ApplySubstitutions(stringValue);
294 nsTArray<nsString>& aParams,
295 const char* aDefault)
297 TRACE((
"%s[%8.x] - %s (has params) (default %s)",
306 key = NS_ConvertUTF8toUTF16(aKey);
311 nsAutoString defaultString;
313 defaultString = NS_ConvertUTF8toUTF16(aDefault);
317 return Format(key, aParams, defaultString);
322 const nsAString& aParam,
323 const nsAString& aDefault)
325 TRACE((
"%s[%8.x] - %s (param %s) (default %s)",
328 NS_ConvertUTF16toUTF8(aKey).
get(),
329 NS_ConvertUTF16toUTF8(aParam).
get(),
330 NS_ConvertUTF16toUTF8(aDefault).
get()));
332 nsTArray<nsString> params(1);
333 params.AppendElement(aParam);
334 return Format(aKey, params, aDefault);
339 const nsAString& aParam,
340 const char* aDefault)
342 TRACE((
"%s[%8.x] - %s (param %s) (default %s)",
346 NS_ConvertUTF16toUTF8(aParam).BeginReading(),
349 nsTArray<nsString> params(1);
350 params.AppendElement(aParam);
351 return Format(aKey, params, aDefault);
368 sbStringBundle::LoadBundle(
const char* aURI)
372 TRACE((
"%s[%8.x] - loading bundle %s", __FUNCTION__,
this, aURI));
375 nsCOMPtr<nsIStringBundle>
bundle;
376 nsCOMPtr<nsIStringBundleService>
377 stringBundleService = do_QueryInterface(mStringBundleService, &rv);
378 NS_ENSURE_SUCCESS(rv, rv);
379 rv = stringBundleService->CreateBundle(aURI, getter_AddRefs(bundle));
380 NS_ENSURE_SUCCESS(rv, rv);
383 rv = LoadBundle(bundle);
384 NS_ENSURE_SUCCESS(rv, rv);
398 sbStringBundle::LoadBundle(nsIStringBundle* aBundle)
402 TRACE((
"%s[%8.x] - existing bundle [%8.x]", __FUNCTION__,
this, aBundle));
405 mBundleList.AppendObject(aBundle);
408 nsString includeBundleListString;
409 rv = aBundle->GetStringFromName
410 (NS_LITERAL_STRING(
"include_bundle_list").
get(),
411 getter_Copies(includeBundleListString));
416 nsTArray<nsString> includeBundleList;
418 NS_LITERAL_STRING(
","),
420 PRUint32 includeBundleCount = includeBundleList.Length();
421 for (PRUint32
i = 0;
i < includeBundleCount;
i++) {
422 rv = LoadBundle(NS_ConvertUTF16toUTF8(includeBundleList[
i]).
get());
423 NS_ENSURE_SUCCESS(rv, rv);
437 sbStringBundle::ApplySubstitutions(nsAString& aString)
439 TRACE((
"%s[%8.x] - applying %s",
442 NS_ConvertUTF16toUTF8(aString).
get()));
444 PRInt32 currentOffset = 0;
447 PRInt32 subStartIndex = aString.Find(NS_LITERAL_STRING(
"&"), currentOffset);
448 if (subStartIndex < 0)
450 PRInt32 subEndIndex = aString.Find(NS_LITERAL_STRING(
";"),
456 PRInt32 subKeyStartIndex = subStartIndex + 1;
457 PRInt32 subKeyLength = subEndIndex - subKeyStartIndex;
459 subKey.Assign(nsDependentSubstring(aString,
464 nsAutoString subString;
465 if (subKey.EqualsLiteral(
"amp")) {
466 subString = NS_LITERAL_STRING(
"&");
468 subString =
Get(subKey, NS_LITERAL_STRING(
""));
472 PRInt32 subLength = subEndIndex - subStartIndex + 1;
473 aString.Replace(subStartIndex, subLength, subString);
476 currentOffset = subStartIndex + subString.Length();
#define SB_STRINGBUNDLESERVICE_CONTRACTID
nsString Get(const nsAString &aKey, const nsAString &aDefault=SBVoidString())
nsString Format(const nsAString &aKey, nsTArray< nsString > &aParams, const nsAString &aDefault=SBVoidString())
void nsString_Split(const nsAString &aString, const nsAString &aDelimiter, nsTArray< nsString > &aSubStringArray)
Songbird String Bundle Definitions.
_getSelectedPageStyle s i