Skip to content

Commit d9eec55

Browse files
committed
System language list
1 parent a937a6d commit d9eec55

6 files changed

Lines changed: 71 additions & 3 deletions

File tree

Plugins/CS/Device.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static class Device
1818
[DllImport("__Internal")] static extern void Device_PlayHaptics(int style, float intensity);
1919
[DllImport("__Internal")] static extern string Device_GetLocaleISOCode();
2020
[DllImport("__Internal")] static extern string Device_GetLanguageISOCode();
21+
//[DllImport("__Internal")] static extern unsafe char** Device_GetLanguageCodes(ref long count);
2122

2223
/// <summary>
2324
/// 获取系统版本
@@ -123,9 +124,29 @@ public static void PlayHaptics(UIImpactFeedbackStyle style, float intensity)
123124
public static string GetLocaleISOCode() => Device_GetLocaleISOCode();
124125

125126
/// <summary>
126-
/// 获取当前设备语言的ISO码(例:zh-Hans-CN)
127+
/// 获取用户系统设置的主要语言的IETF标签(例:zh-Hans-CN)
128+
/// <para>格式:[language]-[Script]-[REGION]_[sort order]</para>
127129
/// </summary>
128-
/// <returns>语言ISO码</returns>
130+
/// <returns>语言IETF标签</returns>
129131
public static string GetLanguageISOCode() => Device_GetLanguageISOCode();
132+
133+
/*/// <summary>
134+
/// 获取用户系统设置的所有语言的IETF标签(例:zh-Hans-CN)
135+
/// </summary>
136+
/// <returns>语言IETF标签数组,按用户语言优先级排列</returns>*/
137+
/*public static unsafe string[] GetLanguageCodes()
138+
{
139+
long count = 0;
140+
char** languages = Device_GetLanguageCodes(ref count);
141+
if (languages == null || count < 1)
142+
return null;
143+
string[] arr = new string[count];
144+
for (int i = 0; i < count; i++)
145+
{
146+
arr[i] = Utils.StrFromPtr(languages[i]);
147+
}
148+
Utils.FreePtr(languages);
149+
return arr;
150+
}*/
130151
}
131152
}

Plugins/CS/Utils.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,32 @@
11
using System;
2+
using System.Runtime.InteropServices;
23

34
namespace iOSNativePlugin
45
{
6+
public static class Utils
7+
{
8+
[DllImport("__Internal")] static extern unsafe void FreeCPtr(void* ptr);
9+
10+
public static unsafe void FreePtr(IntPtr ptr) => FreeCPtr(ptr.ToPointer());
11+
12+
public static unsafe void FreePtr(void* ptr) => FreeCPtr(ptr);
13+
14+
public static unsafe string StrFromPtr(char* ptr)
15+
{
16+
if (ptr == null)
17+
return string.Empty;
18+
return new string(ptr);
19+
}
20+
21+
public static unsafe string PtrToStr(char* ptr)
22+
{
23+
var str = StrFromPtr(ptr);
24+
FreePtr(ptr);
25+
return str;
26+
}
27+
}
28+
29+
530
delegate void SaveImageToAlbumCallback(bool saved);
631
delegate void DialogSelectionCallback(int selection);
732
delegate void ShareCloseCallback();

Plugins/Native/Headers/Device.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
+(void)PlayHaptics:(int)style _intensity:(float)intensity;
1010
+(NSString *)GetLocaleISOCode;
1111
+(NSString *)GetLanguageISOCode;
12+
+(NSArray<NSString*>*)GetLanguageISOCodes;
1213

1314
@end
1415

@@ -40,4 +41,15 @@ extern "C"
4041
const char* Device_GetLanguageISOCode(){
4142
return StringCopy([[Device GetLanguageISOCode] UTF8String]);
4243
}
44+
char** Device_GetLanguageCodes(long* count)
45+
{
46+
auto array = [Device GetLanguageISOCodes];
47+
*count = array.count;
48+
char** languages = (char**)malloc(sizeof(char*) * array.count);
49+
for (long i = 0; i < *count; i++)
50+
{
51+
languages[i] = StringCopy([array[i] UTF8String]);
52+
}
53+
return languages;
54+
}
4355
}

Plugins/Native/Implementations/Device.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,8 @@ +(NSString *)GetLanguageISOCode
8383
NSString *lang = [NSLocale preferredLanguages].firstObject;
8484
return lang;
8585
}
86+
+(NSArray<NSString*>*)GetLanguageISOCodes
87+
{
88+
return [NSLocale preferredLanguages];
89+
}
8690
@end

Plugins/Native/Utils.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ static void LOG(NSString* log){
2222
return newString;
2323
}
2424

25+
extern "C" void FreeCPtr(void* ptr)
26+
{
27+
free(ptr);
28+
}
29+
30+
2531
static NSDate* DateFromLong(long year, long month, long day, long hour, long minute, long second)
2632
{
2733
NSDateComponents *components = [[NSDateComponents alloc] init];

iOSNativePlugin.asmdef

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"iOS"
88
],
99
"excludePlatforms": [],
10-
"allowUnsafeCode": false,
10+
"allowUnsafeCode": true,
1111
"overrideReferences": false,
1212
"precompiledReferences": [],
1313
"autoReferenced": true,

0 commit comments

Comments
 (0)