Locale settings for macOS

From Free Pascal wiki
Revision as of 08:19, 2 June 2020 by Trev (talk | contribs) (→‎Manual initialisation: Updated for 64 bit Cocoa)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
macOSlogo.png

This article applies to macOS only.

See also: Multiplatform Programming Guide

English (en)

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.

Warning-icon.png

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.

Automatic initialisation based on the System Preferences settings

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.

Automatic initialisation based on the settings in the Unix layer

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.

The macOS locale command line utility available since macOS 10.4 (Tiger) will reveal your locale settings. For example, open a Terminal and type:

$ locale
LANG="en_AU.UTF-8"
LC_COLLATE="en_AU.UTF-8"
LC_CTYPE="en_AU.UTF-8"
LC_MESSAGES="en_AU.UTF-8"
LC_MONETARY="en_AU.UTF-8"
LC_NUMERIC="en_AU.UTF-8"
LC_TIME="en_AU.UTF-8"
LC_ALL=

Manual initialisation

  • Hardcoded:
 
  // use in initialization or in onCreate of the main form
  DateSeparator := '.';
  ShortDateFormat := 'dd.mm.yyyy';
  LongDateFormat := 'd. mmmm yyyy';

Source: Lazarus Form post

  • Partially replicating the functionality of the iosxlocale unit:
 

uses
  MacOSAll, CocoaUtils;

var
  theFormatString: string;
  theFormatter: CFDateFormatterRef;

procedure GetMacDateFormats;
begin
  theFormatter := CFDateFormatterCreate(kCFAllocatorDefault, CFLocaleCopyCurrent, kCFDateFormatterMediumStyle, kCFDateFormatterNoStyle);
  theFormatString := CFStringToStr(CFDateFormatterGetFormat(theFormatter));
  if pos('.', theFormatString) > 0 then
    DefaultFormatSettings.DateSeparator := '.'
  else if pos('/', theFormatString) > 0 then
    DefaultFormatSettings.DateSeparator := '/'
  else if pos('-', theFormatString) > 0 then
    DefaultFormatSettings.DateSeparator := '-';
  DefaultFormatSettings.ShortDateFormat := theFormatString;
  CFRelease(theFormatter); 
  theFormatter := CFDateFormatterCreate(kCFAllocatorDefault, CFLocaleCopyCurrent, kCFDateFormatterLongStyle, kCFDateFormatterNoStyle);
  theFormatString := CFStringToStr(CFDateFormatterGetFormat(theFormatter));
  DefaultFormatSettings.LongDateFormat := theFormatString;
  CFRelease(theFormatter); 
end;

If the procedure GetMacDateFormats is called in the beginning of the program's main unit the "International" settings of systems preferences are used.