xref: /haiku/src/apps/haikudepot/util/LocaleUtils.cpp (revision 68d37cfb3a755a7270d772b505ee15c8b18aa5e0)
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 #include "Logger.h"
17 
18 
19 #undef B_TRANSLATION_CONTEXT
20 #define B_TRANSLATION_CONTEXT "LocaleUtils"
21 
22 
23 BCollator* LocaleUtils::sSharedCollator = NULL;
24 
25 
26 /*static*/ BCollator*
27 LocaleUtils::GetSharedCollator()
28 {
29 	if (sSharedCollator == NULL) {
30 		sSharedCollator = new BCollator();
31 		GetCollator(sSharedCollator);
32 	}
33 
34 	return sSharedCollator;
35 }
36 
37 
38 /*static*/ void
39 LocaleUtils::GetCollator(BCollator* collator)
40 {
41 	const BLocale* locale = BLocaleRoster::Default()->GetDefaultLocale();
42 
43 	if (locale->GetCollator(collator) != B_OK)
44 		HDFATAL("unable to get the locale's collator");
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