📄 jcllocales.pas
字号:
{**************************************************************************************************}
{ }
{ Project JEDI Code Library (JCL) }
{ }
{ The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); }
{ you may not use this file except in compliance with the License. You may obtain a copy of the }
{ License at http://www.mozilla.org/MPL/ }
{ }
{ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF }
{ ANY KIND, either express or implied. See the License for the specific language governing rights }
{ and limitations under the License. }
{ }
{ The Original Code is JclLocales.pas. }
{ }
{ The Initial Developer of the Original Code is Petr Vones. }
{ Portions created by Petr Vones are Copyright (C) Petr Vones. All Rights Reserved. }
{ }
{ Contributors: }
{ Marcel van Brakel }
{ Robert Marquardt (marquardt) }
{ Robert Rossmair (rrossmair) }
{ Matthias Thoma (mthoma) }
{ Petr Vones (pvones) }
{ }
{**************************************************************************************************}
{ }
{ This unit contains a set of classes which allow you to easily retrieve locale specific }
{ information such the list of keyboard layouts, names used for dates and characters used for }
{ formatting numbers and dates. }
{ }
{ Unit owner: Petr Vones }
{ }
{**************************************************************************************************}
// Last modified: $Date: 2005/02/25 07:20:15 $
// For history see end of file
unit JclLocales;
{$I jcl.inc}
{$I windowsonly.inc}
interface
uses
{$IFDEF FPC}
JwaWinNLS,
{$ENDIF FPC}
Windows, Classes, SysUtils, Contnrs,
JclBase, JclWin32;
type
// System locales
TJclLocalesDays = 1..7;
TJclLocalesMonths = 1..13;
TJclLocaleDateFormats = (ldShort, ldLong, ldYearMonth);
TJclLocaleInfo = class(TObject)
private
FCalendars: TStringList;
FDateFormats: array [TJclLocaleDateFormats] of TStringList;
FLocaleID: LCID;
FTimeFormats: TStringList;
FUseSystemACP: Boolean;
FValidCalendars: Boolean;
FValidDateFormatLists: set of TJclLocaleDateFormats;
FValidTimeFormatLists: Boolean;
function GetCalendars: TStrings;
function GetCalendarIntegerInfo(Calendar: CALID; InfoType: Integer): Integer;
function GetCalendarStringInfo(Calendar: CALID; InfoType: Integer): string;
function GetIntegerInfo(InfoType: Integer): Integer;
function GetStringInfo(InfoType: Integer): string;
function GetLangID: LANGID;
function GetSortID: Word;
function GetLangIDPrimary: Word;
function GetLangIDSub: Word;
function GetLongMonthNames(Month: TJclLocalesMonths): string;
function GetAbbreviatedMonthNames(Month: TJclLocalesMonths): string;
function GetLongDayNames(Day: TJclLocalesDays): string;
function GetAbbreviatedDayNames(Day: TJclLocalesDays): string;
function GetCharInfo(InfoType: Integer): Char;
function GetTimeFormats: TStrings;
function GetDateFormats(Format: TJclLocaleDateFormats): TStrings;
function GetFontCharset: Byte;
procedure SetUseSystemACP(const Value: Boolean);
procedure SetCharInfo(InfoType: Integer; const Value: Char);
procedure SetIntegerInfo(InfoType: Integer; const Value: Integer);
procedure SetStringInfo(InfoType: Integer; const Value: string);
public
constructor Create(ALocaleID: LCID = LOCALE_SYSTEM_DEFAULT);
destructor Destroy; override;
property CharInfo[InfoType: Integer]: Char read GetCharInfo write SetCharInfo;
property IntegerInfo[InfoType: Integer]: Integer read GetIntegerInfo write SetIntegerInfo;
property StringInfo[InfoType: Integer]: string read GetStringInfo write SetStringInfo; default;
property UseSystemACP: Boolean read FUseSystemACP write SetUseSystemACP;
property FontCharset: Byte read GetFontCharset;
property LangID: LANGID read GetLangID;
property LocaleID: LCID read FLocaleID;
property LangIDPrimary: Word read GetLangIDPrimary;
property LangIDSub: Word read GetLangIDSub;
property SortID: Word read GetSortID;
property DateFormats[Format: TJclLocaleDateFormats]: TStrings read GetDateFormats;
property TimeFormats: TStrings read GetTimeFormats;
// Languages
property LanguageIndentifier: string index LOCALE_ILANGUAGE read GetStringInfo;
property LocalizedLangName: string index LOCALE_SLANGUAGE read GetStringInfo;
property EnglishLangName: string index LOCALE_SENGLANGUAGE read GetStringInfo;
property AbbreviatedLangName: string index LOCALE_SABBREVLANGNAME read GetStringInfo;
property NativeLangName: string index LOCALE_SNATIVELANGNAME read GetStringInfo;
property ISOAbbreviatedLangName: string index LOCALE_SISO639LANGNAME read GetStringInfo;
// Countries
property CountryCode: Integer index LOCALE_ICOUNTRY read GetIntegerInfo;
property LocalizedCountryName: string index LOCALE_SCOUNTRY read GetStringInfo;
property EnglishCountryName: string index LOCALE_SENGCOUNTRY read GetStringInfo;
property AbbreviatedCountryName: string index LOCALE_SABBREVCTRYNAME read GetStringInfo;
property NativeCountryName: string index LOCALE_SNATIVECTRYNAME read GetStringInfo;
property ISOAbbreviatedCountryName: string index LOCALE_SISO3166CTRYNAME read GetStringInfo;
// Codepages
property DefaultLanguageId: Integer index LOCALE_IDEFAULTLANGUAGE read GetIntegerInfo;
property DefaultCountryCode: Integer index LOCALE_IDEFAULTCOUNTRY read GetIntegerInfo;
property DefaultCodePageEBCDIC: Integer index LOCALE_IDEFAULTEBCDICCODEPAGE read GetIntegerInfo;
property CodePageOEM: Integer index LOCALE_IDEFAULTCODEPAGE read GetIntegerInfo;
property CodePageANSI: Integer index LOCALE_IDEFAULTANSICODEPAGE read GetIntegerInfo;
property CodePageMAC: Integer index LOCALE_IDEFAULTMACCODEPAGE read GetIntegerInfo;
// Digits
property ListItemSeparator: Char index LOCALE_SLIST read GetCharInfo write SetCharInfo;
property Measure: Integer index LOCALE_IMEASURE read GetIntegerInfo write SetIntegerInfo;
property DecimalSeparator: Char index LOCALE_SDECIMAL read GetCharInfo write SetCharInfo;
property ThousandSeparator: Char index LOCALE_STHOUSAND read GetCharInfo write SetCharInfo;
property DigitGrouping: string index LOCALE_SGROUPING read GetStringInfo write SetStringInfo;
property NumberOfFractionalDigits: Integer index LOCALE_IDIGITS read GetIntegerInfo write SetIntegerInfo;
property LeadingZeros: Integer index LOCALE_ILZERO read GetIntegerInfo write SetIntegerInfo;
property NegativeNumberMode: Integer index LOCALE_INEGNUMBER read GetIntegerInfo write SetIntegerInfo;
property NativeDigits: string index LOCALE_SNATIVEDIGITS read GetStringInfo;
property DigitSubstitution: Integer index LOCALE_IDIGITSUBSTITUTION read GetIntegerInfo;
// Monetary
property MonetarySymbolLocal: string index LOCALE_SCURRENCY read GetStringInfo write SetStringInfo;
property MonetarySymbolIntl: string index LOCALE_SINTLSYMBOL read GetStringInfo;
property MonetaryDecimalSeparator: Char index LOCALE_SMONDECIMALSEP read GetCharInfo write SetCharInfo;
property MonetaryThousandsSeparator: Char index LOCALE_SMONTHOUSANDSEP read GetCharInfo write SetCharInfo;
property MonetaryGrouping: string index LOCALE_SMONGROUPING read GetStringInfo write SetStringInfo;
property NumberOfLocalMonetaryDigits: Integer index LOCALE_ICURRDIGITS read GetIntegerInfo write SetIntegerInfo;
property NumberOfIntlMonetaryDigits: Integer index LOCALE_IINTLCURRDIGITS read GetIntegerInfo;
property PositiveCurrencyMode: string index LOCALE_ICURRENCY read GetStringInfo write SetStringInfo;
property NegativeCurrencyMode: string index LOCALE_INEGCURR read GetStringInfo write SetStringInfo;
property EnglishCurrencyName: string index LOCALE_SENGCURRNAME read GetStringInfo;
property NativeCurrencyName: string index LOCALE_SNATIVECURRNAME read GetStringInfo;
// Date and time
property DateSeparator: Char index LOCALE_SDATE read GetCharInfo write SetCharInfo;
property TimeSeparator: Char index LOCALE_STIME read GetCharInfo write SetCharInfo;
property ShortDateFormat: string index LOCALE_SSHORTDATE read GetStringInfo write SetStringInfo;
property LongDateFormat: string index LOCALE_SLONGDATE read GetStringInfo write SetStringInfo;
property TimeFormatString: string index LOCALE_STIMEFORMAT read GetStringInfo write SetStringInfo;
property ShortDateOrdering: Integer index LOCALE_IDATE read GetIntegerInfo;
property LongDateOrdering: Integer index LOCALE_ILDATE read GetIntegerInfo;
property TimeFormatSpecifier: Integer index LOCALE_ITIME read GetIntegerInfo write SetIntegerInfo;
property TimeMarkerPosition: Integer index LOCALE_ITIMEMARKPOSN read GetIntegerInfo;
property CenturyFormatSpecifier: Integer index LOCALE_ICENTURY read GetIntegerInfo;
property LeadZerosInTime: Integer index LOCALE_ITLZERO read GetIntegerInfo;
property LeadZerosInDay: Integer index LOCALE_IDAYLZERO read GetIntegerInfo;
property LeadZerosInMonth: Integer index LOCALE_IMONLZERO read GetIntegerInfo;
property AMDesignator: string index LOCALE_S1159 read GetStringInfo write SetStringInfo;
property PMDesignator: string index LOCALE_S2359 read GetStringInfo write SetStringInfo;
property YearMonthFormat: string index LOCALE_SYEARMONTH read GetStringInfo write SetStringInfo;
// Calendar
property CalendarType: Integer index LOCALE_ICALENDARTYPE read GetIntegerInfo write SetIntegerInfo;
property AdditionalCaledarTypes: Integer index LOCALE_IOPTIONALCALENDAR read GetIntegerInfo;
property FirstDayOfWeek: Integer index LOCALE_IFIRSTDAYOFWEEK read GetIntegerInfo write SetIntegerInfo;
property FirstWeekOfYear: Integer index LOCALE_IFIRSTWEEKOFYEAR read GetIntegerInfo write SetIntegerInfo;
// Day and month names
property LongDayNames[Day: TJclLocalesDays]: string read GetLongDayNames;
property AbbreviatedDayNames[Day: TJclLocalesDays]: string read GetAbbreviatedDayNames;
property LongMonthNames[Month: TJclLocalesMonths]: string read GetLongMonthNames;
property AbbreviatedMonthNames[Month: TJclLocalesMonths]: string read GetAbbreviatedMonthNames;
// Sign
property PositiveSign: string index LOCALE_SPOSITIVESIGN read GetStringInfo write SetStringInfo;
property NegativeSign: string index LOCALE_SNEGATIVESIGN read GetStringInfo write SetStringInfo;
property PositiveSignPos: Integer index LOCALE_IPOSSIGNPOSN read GetIntegerInfo;
property NegativeSignPos: Integer index LOCALE_INEGSIGNPOSN read GetIntegerInfo;
property PosOfPositiveMonetarySymbol: Integer index LOCALE_IPOSSYMPRECEDES read GetIntegerInfo;
property SepOfPositiveMonetarySymbol: Integer index LOCALE_IPOSSEPBYSPACE read GetIntegerInfo;
property PosOfNegativeMonetarySymbol: Integer index LOCALE_INEGSYMPRECEDES read GetIntegerInfo;
property SepOfNegativeMonetarySymbol: Integer index LOCALE_INEGSEPBYSPACE read GetIntegerInfo;
// Misc
property DefaultPaperSize: Integer index LOCALE_IPAPERSIZE read GetIntegerInfo;
property FontSignature: string index LOCALE_FONTSIGNATURE read GetStringInfo;
property LocalizedSortName: string index LOCALE_SSORTNAME read GetStringInfo;
// Calendar Info
property Calendars: TStrings read GetCalendars;
property CalendarIntegerInfo[Calendar: CALID; InfoType: Integer]: Integer read GetCalendarIntegerInfo;
property CalendarStringInfo[Calendar: CALID; InfoType: Integer]: string read GetCalendarStringInfo;
property CalTwoDigitYearMax[Calendar: CALID]: Integer index CAL_ITWODIGITYEARMAX read GetCalendarIntegerInfo;
end;
TJclLocalesKind = (lkInstalled, lkSupported);
TJclLocalesList = class(TObjectList)
private
FCodePages: TStringList;
FKind: TJclLocalesKind;
function GetItemFromLangID(LangID: LANGID): TJclLocaleInfo;
function GetItemFromLangIDPrimary(LangIDPrimary: Word): TJclLocaleInfo;
function GetItemFromLocaleID(LocaleID: LCID): TJclLocaleInfo;
function GetItems(Index: Integer): TJclLocaleInfo;
function GetCodePages: TStrings;
protected
procedure CreateList;
public
constructor Create(AKind: TJclLocalesKind = lkInstalled);
destructor Destroy; override;
procedure FillStrings(Strings: TStrings; InfoType: Integer);
property CodePages: TStrings read GetCodePages;
property ItemFromLangID[LangID: LANGID]: TJclLocaleInfo read GetItemFromLangID;
property ItemFromLangIDPrimary[LangIDPrimary: Word]: TJclLocaleInfo read GetItemFromLangIDPrimary;
property ItemFromLocaleID[LocaleID: LCID]: TJclLocaleInfo read GetItemFromLocaleID;
property Items[Index: Integer]: TJclLocaleInfo read GetItems; default;
property Kind: TJclLocalesKind read FKind;
end;
// Keyboard layouts
TJclKeybLayoutFlag = (klReorder, klUnloadPrevious, klSetForProcess,
klActivate, klNotEllShell, klReplaceLang, klSubstituteOK);
TJclKeybLayoutFlags = set of TJclKeybLayoutFlag;
TJclKeyboardLayoutList = class;
TJclAvailableKeybLayout = class(TObject)
private
FIdentifier: DWORD;
FLayoutID: Word;
FLayoutFile: string;
FOwner: TJclKeyboardLayoutList;
FName: string;
function GetIdentifierName: string;
function GetLayoutFileExists: Boolean;
public
function Load(const LoadFlags: TJclKeybLayoutFlags): Boolean;
property Identifier: DWORD read FIdentifier;
property IdentifierName: string read GetIdentifierName;
property LayoutID: Word read FLayoutID;
property LayoutFile: string read FLayoutFile;
property LayoutFileExists: Boolean read GetLayoutFileExists;
property Name: string read FName;
end;
TJclKeyboardLayout = class(TObject)
private
FLayout: HKL;
FLocaleInfo: TJclLocaleInfo;
FOwner: TJclKeyboardLayoutList;
function GetDeviceHandle: Word;
function GetDisplayName: string;
function GetLocaleID: Word;
function GetLocaleInfo: TJclLocaleInfo;
function GetVariationName: string;
public
constructor Create(AOwner: TJclKeyboardLayoutList; ALayout: HKL);
destructor Destroy; override;
function Activate(ActivateFlags: TJclKeybLayoutFlags = []): Boolean;
function Unload: Boolean;
property DeviceHandle: Word read GetDeviceHandle;
property DisplayName: string read GetDisplayName;
property Layout: HKL read FLayout;
property LocaleID: Word read GetLocaleID;
property LocaleInfo: TJclLocaleInfo read GetLocaleInfo;
property VariationName: string read GetVariationName;
end;
TJclKeyboardLayoutList = class(TObject)
private
FAvailableLayouts: TObjectList;
FList: TObjectList;
FOnRefresh: TNotifyEvent;
function GetCount: Integer;
function GetItems(Index: Integer): TJclKeyboardLayout;
function GetActiveLayout: TJclKeyboardLayout;
function GetItemFromHKL(Layout: HKL): TJclKeyboardLayout;
function GetLayoutFromLocaleID(LocaleID: Word): TJclKeyboardLayout;
function GetAvailableLayoutCount: Integer;
function GetAvailableLayouts(Index: Integer): TJclAvailableKeybLayout;
protected
procedure CreateAvailableLayouts;
procedure DoRefresh; dynamic;
public
constructor Create;
destructor Destroy; override;
function ActivatePrevLayout(ActivateFlags: TJclKeybLayoutFlags = []): Boolean;
function ActivateNextLayout(ActivateFlags: TJclKeybLayoutFlags = []): Boolean;
function LoadLayout(const LayoutName: string; LoadFlags: TJclKeybLayoutFlags): Boolean;
procedure Refresh;
property ActiveLayout: TJclKeyboardLayout read GetActiveLayout;
property AvailableLayouts[Index: Integer]: TJclAvailableKeybLayout read GetAvailableLayouts;
property AvailableLayoutCount: Integer read GetAvailableLayoutCount;
property Count: Integer read GetCount;
property ItemFromHKL[Layout: HKL]: TJclKeyboardLayout read GetItemFromHKL;
property Items[Index: Integer]: TJclKeyboardLayout read GetItems; default;
property LayoutFromLocaleID[LocaleID: Word]: TJclKeyboardLayout read GetLayoutFromLocaleID;
property OnRefresh: TNotifyEvent read FOnRefresh write FOnRefresh;
end;
// Various routines
procedure JclLocalesInfoList(const Strings: TStrings; InfoType: Integer = LOCALE_SENGCOUNTRY);
implementation
uses
{$IFDEF FPC}
WinSysUt,
{$ENDIF FPC}
SysConst, JclFileUtils, JclRegistry, JclStrings, JclSysInfo;
const
JclMaxKeyboardLayouts = 16;
LocaleUseAcp: array [Boolean] of DWORD = (0, LOCALE_USE_CP_ACP);
function KeybLayoutFlagsToDWORD(const ActivateFlags: TJclKeybLayoutFlags;
const LoadMode: Boolean): DWORD;
begin
Result := 0;
if klReorder in ActivateFlags then
Inc(Result, KLF_REORDER);
if (klUnloadPrevious in ActivateFlags) and IsWinNT then
Inc(Result, KLF_UNLOADPREVIOUS);
if (klSetForProcess in ActivateFlags) and IsWin2K then
Inc(Result, KLF_SETFORPROCESS);
if LoadMode then
begin
if klActivate in ActivateFlags then
Inc(Result, KLF_ACTIVATE);
if klNotEllShell in ActivateFlags then
Inc(Result, KLF_NOTELLSHELL);
if (klReplaceLang in ActivateFlags) and not IsWinNT3 then
Inc(Result, KLF_REPLACELANG);
if klSubstituteOK in ActivateFlags then
Inc(Result, KLF_SUBSTITUTE_OK);
end;
end;
// EnumXXX functions helper thread variables
threadvar
ProcessedLocaleInfoList: TStrings;
ProcessedLocalesList: TJclLocalesList;
//=== { TJclLocaleInfo } =====================================================
constructor TJclLocaleInfo.Create(ALocaleID: LCID);
begin
inherited Create;
FLocaleID := ALocaleID;
FUseSystemACP := True;
FValidDateFormatLists := [];
end;
destructor TJclLocaleInfo.Destroy;
var
DateFormat: TJclLocaleDateFormats;
begin
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -