Line data Source code
1 : /**
2 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev>
3 :
4 : Permission is hereby granted, free of charge, to any person obtaining a copy
5 : of this software and associated documentation files (the "Software"), to deal
6 : in the Software without restriction, including without limitation the rights
7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : copies of the Software, and to permit persons to whom the Software is
9 : furnished to do so, subject to the following conditions:
10 :
11 : The above copyright notice and this permission notice shall be included in
12 : all copies or substantial portions of the Software.
13 :
14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : THE SOFTWARE.
21 : **/
22 :
23 : #ifndef XENOLITH_FONT_XLFONTLOCALE_H_
24 : #define XENOLITH_FONT_XLFONTLOCALE_H_
25 :
26 : #include "XLEventHeader.h"
27 : #include "XLFontConfig.h"
28 : #include "SPMetastring.h"
29 :
30 : namespace STAPPLER_VERSIONIZED stappler::xenolith {
31 :
32 : #ifdef __clang__
33 : #pragma clang diagnostic push
34 : #pragma clang diagnostic ignored "-Wgnu-string-literal-operator-template"
35 : #endif
36 :
37 : // full localized string
38 10 : template <typename CharType, CharType ... Chars> auto operator "" _locale() {
39 10 : return metastring::merge("@Locale:"_meta, metastring::metastring<Chars ...>());
40 : }
41 :
42 : #ifdef __clang__
43 : #pragma clang diagnostic pop
44 : #endif
45 :
46 : // localized token
47 : inline String operator"" _token ( const char* str, std::size_t len) {
48 : String ret;
49 : ret.reserve(len + 2);
50 : ret.append("%");
51 : ret.append(str, len);
52 : ret.append("%");
53 : return ret;
54 : }
55 :
56 37 : inline String localeIndex(size_t idx) {
57 37 : String ret; ret.reserve(20);
58 37 : ret.append("%=");
59 37 : ret.append(toString(idx));
60 37 : ret.push_back('%');
61 37 : return ret;
62 0 : }
63 :
64 : template <size_t Index>
65 : inline constexpr auto localeIndex() {
66 : return metastring::merge("%="_meta, metastring::numeric<Index>(), "%"_meta);
67 : }
68 :
69 : }
70 :
71 : namespace STAPPLER_VERSIONIZED stappler::xenolith::locale {
72 :
73 : using LocaleInitList = std::initializer_list<Pair<StringView, StringView>>;
74 : using LocaleIndexList = std::initializer_list<Pair<uint32_t, StringView>>;
75 : using NumRule = Function<uint8_t(uint32_t)>;
76 :
77 : enum class TimeTokens {
78 : Today = 0,
79 : Yesterday,
80 : Jan,
81 : Feb,
82 : Mar,
83 : Apr,
84 : Nay,
85 : Jun,
86 : jul,
87 : Aug,
88 : Sep,
89 : Oct,
90 : Nov,
91 : Dec,
92 : Max
93 : };
94 :
95 : extern EventHeader onLocale;
96 :
97 : void define(const StringView &locale, LocaleInitList &&);
98 : void define(const StringView &locale, LocaleIndexList &&);
99 : void define(const StringView &locale, const std::array<StringView, toInt(TimeTokens::Max)> &);
100 :
101 : void setDefault(const String &);
102 : const String &getDefault();
103 :
104 : void setLocale(const String &);
105 : const String &getLocale();
106 :
107 : void setNumRule(const String &, NumRule &&);
108 :
109 : WideStringView string(const WideStringView &);
110 : WideStringView string(size_t);
111 :
112 : template <char ... Chars>
113 30 : WideStringView string(const metastring::metastring<Chars...> &str) {
114 30 : return string(WideStringView(str.to_std_ustring()));
115 : }
116 :
117 : WideStringView numeric(const WideStringView &, uint32_t);
118 :
119 : template <char ... Chars>
120 90 : WideStringView numeric(const metastring::metastring<Chars...> &str, uint32_t n) {
121 90 : return numeric(WideStringView(str.to_std_ustring()), n);
122 : }
123 :
124 : bool hasLocaleTagsFast(const WideStringView &);
125 : bool hasLocaleTags(const WideStringView &);
126 : WideString resolveLocaleTags(const WideStringView &);
127 :
128 : String language(const StringView &locale);
129 :
130 : // convert locale name to common form ('en-us', 'ru-ru', 'fr-fr')
131 : String common(const StringView &locale);
132 :
133 : StringView timeToken(TimeTokens);
134 :
135 : const std::array<memory::string, toInt(TimeTokens::Max)> &timeTokenTable();
136 :
137 : String localDate(Time);
138 : String localDate(const std::array<StringView, toInt(TimeTokens::Max)> &, Time);
139 :
140 : }
141 :
142 : #endif /* XENOLITH_FONT_XLFONTLOCALE_H_ */
|