Locale settings for macOS: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Locale settings for macOS
| Old | New | Line | |
|---|---|---|---|
| 1 | + | *(macOS only)* | |
| 2 | + | ||
| 3 | + | In macOS, like on other Unix platforms, the RTL does not load the locale settings (date & time separator, currency symbol, etc) by default. They can be initialised in three ways. | |
| 4 | + | ||
| 5 | + | > **Warning:** Adding both *iosxlocale* and *clocale* to the Uses clause will cause the second in line to overwrite the settings set by the first one. | |
| 6 | + | ||
| 7 | + | ## Automatic initialisation based on the System Preferences settings | |
| 8 | + | ||
| 9 | + | In FPC 2.7.1 and later, adding the *iosxlocale* unit to the uses clause will initialise the locale settings using the settings from the System Preferences. This unit is not available in older versions of FPC. | |
| 10 | + | ||
| 11 | + | ## Automatic initialisation based on the settings in the Unix layer | |
| 12 | + | ||
| 13 | + | Adding the *clocale* unit to the uses clause will cause the locale settings to be initialised using the configuration set at the Unix layer of macOS (based on the *LANG* and related environment variables). This unit is also available on other Unix-like platforms. | |
| 14 | + | ||
| 15 | + | The macOS `locale` command line utility available since macOS 10.4 (Tiger) will reveal your locale settings. For example, open a Terminal and type: | |
| 16 | + | ||
| 17 | + | ```text | |
| 18 | + | $ locale | |
| 19 | + | LANG="en_AU.UTF-8" | |
| 20 | + | LC_COLLATE="en_AU.UTF-8" | |
| 21 | + | LC_CTYPE="en_AU.UTF-8" | |
| 22 | + | LC_MESSAGES="en_AU.UTF-8" | |
| 23 | + | LC_MONETARY="en_AU.UTF-8" | |
| 24 | + | LC_NUMERIC="en_AU.UTF-8" | |
| 25 | + | LC_TIME="en_AU.UTF-8" | |
| 26 | + | LC_ALL= | |
| 27 | + | ``` | |
| 28 | + | ||
| 29 | + | ## Manual initialisation | |
| 30 | + | ||
| 31 | + | - Hardcoded: | |
| 32 | + | ||
| 33 | + | ``` pascal | |
| 34 | + | ||
| 35 | + | // use in initialization or in onCreate of the main form | |
| 36 | + | DateSeparator := '.'; | |
| 37 | + | ShortDateFormat := 'dd.mm.yyyy'; | |
| 38 | + | LongDateFormat := 'd. mmmm yyyy'; | |
| 39 | + | ``` | |
| 40 | + | ||
| 41 | + | Source: [Lazarus Form post](http://forum.lazarus.freepascal.org/index.php?topic=9566.0) | |
| 42 | + | ||
| 43 | + | - Partially replicating the functionality of the *iosxlocale* unit: | |
| 44 | + | ||
| 45 | + | ``` pascal | |
| 46 | + | ||
| 47 | + | ||
| 48 | + | uses | |
| 49 | + | MacOSAll, CocoaUtils; | |
| 50 | + | ||
| 51 | + | var | |
| 52 | + | theFormatString: string; | |
| 53 | + | theFormatter: CFDateFormatterRef; | |
| 54 | + | ||
| 55 | + | procedure GetMacDateFormats; | |
| 56 | + | begin | |
| 57 | + | theFormatter := CFDateFormatterCreate(kCFAllocatorDefault, CFLocaleCopyCurrent, kCFDateFormatterMediumStyle, kCFDateFormatterNoStyle); | |
| 58 | + | theFormatString := CFStringToStr(CFDateFormatterGetFormat(theFormatter)); | |
| 59 | + | if pos('.', theFormatString) > 0 then | |
| 60 | + | DefaultFormatSettings.DateSeparator := '.' | |
| 61 | + | else if pos('/', theFormatString) > 0 then | |
| 62 | + | DefaultFormatSettings.DateSeparator := '/' | |
| 63 | + | else if pos('-', theFormatString) > 0 then | |
| 64 | + | DefaultFormatSettings.DateSeparator := '-'; | |
| 65 | + | DefaultFormatSettings.ShortDateFormat := theFormatString; | |
| 66 | + | CFRelease(theFormatter); | |
| 67 | + | theFormatter := CFDateFormatterCreate(kCFAllocatorDefault, CFLocaleCopyCurrent, kCFDateFormatterLongStyle, kCFDateFormatterNoStyle); | |
| 68 | + | theFormatString := CFStringToStr(CFDateFormatterGetFormat(theFormatter)); | |
| 69 | + | DefaultFormatSettings.LongDateFormat := theFormatString; | |
| 70 | + | CFRelease(theFormatter); | |
| 71 | + | end; | |
| 72 | + | ``` | |
| 73 | + | ||
| 74 | + | If the procedure *GetMacDateFormats* is called in the beginning of the program's main unit the "International" settings of systems preferences are used. | |
| 75 | + | ||
| 76 | + | --- | |
| 77 | + | *From the Free Pascal wiki, page "Locale settings for macOS", as it stood on 2020-06-02 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 78 | + | Category: Code Snippets | |
| 79 | + | Category: macOS |
