Debian Codegraph @

src/base/castlesystemlanguage.pas

raw
1 {
2 Copyright 2018-2024 Benedikt Magnus, Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Cross-platform recognition of the system language/locale. }
17 unit CastleSystemLanguage;
18
19 {$I castleconf.inc}
20
21 {$ifndef FPC}
22 {$message fatal 'This unit is only for FPC, not Delphi.'}
23 {$endif}
24
25 interface
26
27 uses
28 GetText {$ifdef ANDROID}, JNI{$endif};
29
30 const
31 SystemDefaultLanguage = 'en';
32 SystemDefaultLocale = 'en_US';
33
34 { Returns the language code of the system language. See SystemDefaultLanguage. }
35 function SystemLanguage(const ADefaultLanguage: String = SystemDefaultLanguage): String; inline;
36 { Returns the locale code of the system locale. See SystemDefaultLocale. }
37 function SystemLocale(const ADefaultLocale: String = SystemDefaultLocale): String; inline;
38
39 {$ifdef ANDROID}
40 { The CGE build tool will make sure to export this function from Android library.
41 @exclude }
42 procedure Java_io_castleengine_MainActivity_jniLanguage(Env: PJNIEnv; This: jobject; JavaLanguageCode: jstring); cdecl;
43 {$endif}
44
45 implementation
46
47 {$ifdef ANDROID}
48 var
49 MobileSystemLanguage: String;
50 {$endif}
51
52 {$ifdef ANDROID}
53 procedure Java_io_castleengine_MainActivity_jniLanguage(Env: PJNIEnv; This: jobject; JavaLanguageCode: jstring); cdecl;
54 var
55 JavaLanguageCodeStr: PChar;
56 Dummy: JBoolean;
57 begin
58 if (JavaLanguageCode <> nil) and (Env^^.GetStringUTFLength(Env, JavaLanguageCode) <> 0) then
59 begin
60 Dummy := 0;
61 JavaLanguageCodeStr := Env^^.GetStringUTFChars(Env, JavaLanguageCode,{$ifdef VER2}Dummy{$else}@Dummy{$endif});
62 try
63 MobileSystemLanguage := AnsiString(JavaLanguageCodeStr); // will copy characters
64 finally Env^^.ReleaseStringUTFChars(Env, JavaLanguageCode, JavaLanguageCodeStr) end;
65 end;
66 end;
67 {$endif ANDROID}
68
69 function SystemLanguage(const ADefaultLanguage: String = SystemDefaultLanguage): String;
70 begin
71 Result := SystemLocale(ADefaultLanguage);
72 Delete(Result, 3, Length(Result)); //Removes the locale info behind the language code.
73 end;
74
75 function SystemLocale(const ADefaultLocale: String = SystemDefaultLocale): String;
76 {$ifndef ANDROID}
77 var
78 TempDefaultLocale: String;
79 {$endif}
80 begin
81 {$ifdef ANDROID}
82 Result := MobileSystemLanguage;
83 {$else}
84 TempDefaultLocale := ADefaultLocale; //Because GetLanguageIDs, whyever, the default language as var parameter...
85 GetLanguageIDs(Result, TempDefaultLocale);
86 {$endif}
87
88 if Result = '' then
89 Result := ADefaultLocale
90 else
91 Delete(Result, 6, Length(Result)); //There can be more than the language code and the locale info in the result string.
92 //For example, on Debian based systems there can be the encoding as suffix. ("en_GB.UTF-8")
93 //So if we really only want the langauge code and the locale info, we have to delete everything behind it.
94 end;
95
96 end.