Locale settings for macOS

From Lazarus wiki
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.

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.

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
  {$IFDEF LCLCarbon}
  , MacOSAll, CarbonProc
  {$ENDIF}

var
{$IFDEF LCLCarbon}
  theFormatString: string;
  theFormatter: CFDateFormatterRef;
{$ENDIF}

procedure GetMacDateFormats;
begin
  {$IFDEF LCLCarbon}
  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); 
  {$ENDIF}
end;

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