-
Notifications
You must be signed in to change notification settings - Fork 282
/
maclocale.pas
289 lines (260 loc) · 7.96 KB
/
maclocale.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
{
This file is part of the Free Pascal packages library.
Copyright (c) 2013 by Yury Sidorov, member of the
Free Pascal development team
This unit is used to get format settings of the current
Mac OS UI locale. Works on Mac OS X 10.3 or later.
Function CFStringToStr() has been taken from CarbonProc LCL unit.
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
unit MacLocale;
{$mode objfpc}{$H+}
interface
uses
SysUtils, MacOSAll;
procedure GetMacFormatSettings(var ASettings: TFormatSettings);
implementation
{------------------------------------------------------------------------------
Name: CFStringToStr
Params: AString - Core Foundation string ref
Encoding - Result data encoding format
Returns: UTF-8 string
Converts Core Foundation string to string
------------------------------------------------------------------------------}
function CFStringToStr(AString: CFStringRef; Encoding: CFStringEncoding = kCFStringEncodingUTF8): String;
var
Str: Pointer;
StrSize: CFIndex;
StrRange: CFRange;
begin
if AString = nil then
begin
Result := '';
Exit;
end;
// Try the quick way first
Str := CFStringGetCStringPtr(AString, Encoding);
if Str <> nil then
Result := PChar(Str)
else
begin
// if that doesn't work this will
StrRange.location := 0;
StrRange.length := CFStringGetLength(AString);
CFStringGetBytes(AString, StrRange, Encoding,
Ord('?'), False, nil, 0, StrSize{%H-});
SetLength(Result, StrSize);
if StrSize > 0 then
CFStringGetBytes(AString, StrRange, Encoding,
Ord('?'), False, @Result[1], StrSize, StrSize);
end;
end;
function StrOfChar(c: AnsiChar; Count: integer): ansistring;
begin
SetLength(Result, Count);
FillChar(PAnsiChar(Result)^, Count, c);
end;
function ConvertFormatStr(const fmt: ansistring): ansistring;
var
cnt: integer;
c, q: AnsiChar;
p: PAnsiChar;
s: ansistring;
begin
Result:='';
q:=#0;
cnt:=1;
p:=PAnsiChar(fmt);
while p^ <> #0 do begin
s:='';
c:=p^;
if c in ['''', '"'] then begin
if q = #0 then
q:=c
else
if c = q then begin
q:=#0;
cnt:=1;
end;
s:=c;
end
else
if q <> #0 then
s:=c
else begin
if (p + 1)^ = c then
Inc(cnt)
else begin
case c of
'y', 'Y':
begin
c:='y';
if cnt > 2 then
cnt:=4
else
cnt:=2;
end;
'M', 'L':
begin
c:='m';
if cnt > 4 then
cnt:=3;
end;
'd':
if cnt > 2 then
cnt:=2;
'E', 'e', 'c':
begin
c:='d';
if (cnt < 3) or (cnt > 4) then
cnt:=3;
end;
'a':
begin
cnt:=0;
s:='ampm';
end;
'h', 'H', 'k', 'K':
begin
c:='h';
if cnt > 2 then
cnt:=2;
end;
'm':
begin
c:='n';
if cnt > 2 then
cnt:=2;
end;
's':
if cnt > 2 then
cnt:=2;
'S':
begin
c:='z';
cnt:=1;
end;
'G','u','Q','q','w','W','D','F','g','A','z','Z','v':
cnt:=0;
end;
if cnt > 0 then
s:=StrOfChar(c, cnt);
cnt:=1;
end;
end;
Inc(p);
if s <> '' then
Result:=Result + s;
end;
end;
procedure GetMacFormatSettings(var ASettings: TFormatSettings);
var
loc: CFLocaleRef;
function _GetFormat(dateStyle: CFDateFormatterStyle; timeStyle: CFDateFormatterStyle; const DefFormat: string): string;
var
fmt: CFDateFormatterRef;
begin
Result:='';
fmt:=CFDateFormatterCreate(nil, loc, dateStyle, timeStyle);
if fmt <> nil then begin
Result:=ConvertFormatStr(CFStringToStr(CFDateFormatterGetFormat(fmt)));
CFRelease(fmt);
end;
if Result = '' then
Result:=DefFormat;
end;
function _DateToStr(fmt: CFDateFormatterRef; const AFormat: ansistring; AYear: integer; AMonth, ADay, AHour: byte;
const ADefault: ansistring): ansistring;
var
cs: CFStringRef;
gd: CFGregorianDate;
at: CFAbsoluteTime;
tz: CFTimeZoneRef;
begin
cs:=CFStringCreateWithCString(nil, Pointer(PAnsiChar(AFormat)), kCFStringEncodingUTF8);
CFDateFormatterSetFormat(fmt, cs);
CFRelease(cs);
FillChar(gd, SIzeOf(gd), 0);
gd.year:=AYear;
gd.month:=AMonth;
gd.day:=ADay;
gd.hour:=AHour;
tz:=CFTimeZoneCopyDefault;
at:=CFGregorianDateGetAbsoluteTime(gd, tz);
CFRelease(tz);
cs:=CFDateFormatterCreateStringWithAbsoluteTime(nil, fmt, at);
Result:=CFStringToStr(cs);
CFRelease(cs);
if Result = '' then
Result:=ADefault;
end;
function _GetSeparator(dateStyle: CFDateFormatterStyle; timeStyle: CFDateFormatterStyle; DefSep: char): char;
var
fmt: CFDateFormatterRef;
s: ansistring;
p: PAnsiChar;
begin
Result:=DefSep;
fmt:=CFDateFormatterCreate(nil, loc, dateStyle, timeStyle);
if fmt <> nil then begin
s:=_DateToStr(fmt, CFStringToStr(CFDateFormatterGetFormat(fmt)), 2000, 1, 1, 0, DefSep);
s:=Trim(s);
p:=PAnsiChar(s);
while p^ <> #0 do
if (p^ > ' ') and (p^ < 'A') and not (p^ in ['0'..'9']) then begin
Result:=p^;
break;
end
else
Inc(p);
CFRelease(fmt);
end;
end;
var
s: string;
fmt: CFDateFormatterRef;
i: integer;
begin
with ASettings do begin
loc:=CFLocaleCopyCurrent;
if loc = nil then
exit;
s:=CFStringToStr(CFLocaleGetValue(loc, kCFLocaleDecimalSeparator));
if Length(s) = 1 then
DecimalSeparator:=s[1];
s:=CFStringToStr(CFLocaleGetValue(loc, kCFLocaleGroupingSeparator));
if Length(s) = 1 then
ThousandSeparator:=s[1]
else
ThousandSeparator:=' '; // Unicode char has been returned. Probably it is a whitespace
CurrencyString:=CFStringToStr(CFLocaleGetValue(loc, kCFLocaleCurrencySymbol));
DateSeparator:=_GetSeparator(kCFDateFormatterShortStyle, kCFDateFormatterNoStyle, DateSeparator);
TimeSeparator:=_GetSeparator(kCFDateFormatterNoStyle, kCFDateFormatterShortStyle, TimeSeparator);
LongDateFormat:=_GetFormat(kCFDateFormatterLongStyle, kCFDateFormatterNoStyle, LongDateFormat);
ShortDateFormat:=_GetFormat(kCFDateFormatterShortStyle, kCFDateFormatterNoStyle, ShortDateFormat);
LongTimeFormat:=_GetFormat(kCFDateFormatterNoStyle, kCFDateFormatterLongStyle, LongTimeFormat);
ShortTimeFormat:=_GetFormat(kCFDateFormatterNoStyle, kCFDateFormatterShortStyle, ShortTimeFormat);
fmt:=CFDateFormatterCreate(nil, loc, kCFDateFormatterNoStyle, kCFDateFormatterNoStyle);
if fmt <> nil then begin
for i:=1 to 12 do begin
LongMonthNames[i]:=_DateToStr(fmt, 'LLLL', 2006, i, 1, 0, LongMonthNames[i]);
ShortMonthNames[i]:=_DateToStr(fmt, 'LLL', 2006, i, 1, 0, ShortMonthNames[i]);
end;
for i:=1 to 7 do begin
LongDayNames[i]:=_DateToStr(fmt, 'cccc', 2006, 1, i, 0, LongDayNames[i]);
ShortDayNames[i]:=_DateToStr(fmt, 'ccc', 2006, 1, i, 0, ShortDayNames[i]);
end;
TimeAMString:=_DateToStr(fmt, 'a', 2006, 1, 1, 1, TimeAMString);
TimePMString:=_DateToStr(fmt, 'a', 2006, 1, 1, 13, TimePMString);
CFRelease(fmt);
end;
CFRelease(loc);
end;
end;
initialization
GetMacFormatSettings(DefaultFormatSettings);
end.