xref: /haiku/src/apps/haikudepot/util/LocaleUtils.cpp (revision 1a3518cf757c2da8006753f83962da5935bbc82b)
1 /*
2  * Copyright 2019, Andrew Lindesay <apl@lindesay.co.nz>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 #include "LocaleUtils.h"
6 
7 #include <stdlib.h>
8 
9 #include <Catalog.h>
10 #include <Collator.h>
11 #include <DateTimeFormat.h>
12 #include <Locale.h>
13 #include <LocaleRoster.h>
14 #include <StringFormat.h>
15 
16 
17 #undef B_TRANSLATION_CONTEXT
18 #define B_TRANSLATION_CONTEXT "LocaleUtils"
19 
20 
21 BCollator* LocaleUtils::sSharedCollator = NULL;
22 
23 
24 /*static*/ BCollator*
25 LocaleUtils::GetSharedCollator()
26 {
27 	if (sSharedCollator == NULL) {
28 		sSharedCollator = new BCollator();
29 		GetCollator(sSharedCollator);
30 	}
31 
32 	return sSharedCollator;
33 }
34 
35 
36 /*static*/ void
37 LocaleUtils::GetCollator(BCollator* collator)
38 {
39 	const BLocale* locale = BLocaleRoster::Default()->GetDefaultLocale();
40 
41 	if (B_OK != locale->GetCollator(collator)) {
42 		debugger("unable to get the locale's collator");
43 		exit(EXIT_FAILURE);
44 	}
45 }
46 
47 
48 /*static*/ BString
49 LocaleUtils::TimestampToDateTimeString(uint64 millis)
50 {
51 	if (millis == 0)
52 		return "?";
53 
54 	BDateTimeFormat format;
55 	BString buffer;
56 	if (format.Format(buffer, millis / 1000, B_SHORT_DATE_FORMAT,
57 		B_SHORT_TIME_FORMAT) != B_OK)
58 		return "!";
59 
60 	return buffer;
61 }
62 
63 
64 /*!	This is used in situations where the user is required to confirm that they
65 	are as old or older than some minimal age.  This is associated with agreeing
66 	to the user usage conditions.
67 */
68 
69 /*static*/ BString
70 LocaleUtils::CreateTranslatedIAmMinimumAgeSlug(int minimumAge)
71 {
72 	BString slug;
73 	static BStringFormat format(B_TRANSLATE("{0, plural,"
74  		"one{I am at least one year old}"
75 		"other{I am # years of age or older}}"));
76 	format.Format(slug, minimumAge);
77 	return slug;
78 }
79