[macos] send system keyboard lang to editor

This commit is contained in:
maxkadushkin
2025-05-20 00:15:13 +03:00
parent efa2384ae7
commit 82cd28da41
3 changed files with 44 additions and 0 deletions

View File

@ -45,6 +45,7 @@ NS_ASSUME_NONNULL_BEGIN
+ (void)init;
+ (NSDictionary *)availableLanguages;
+ (NSDictionary *)keyboardLanguages;
+ (NSString *)appLanguageCode;
+ (void)setAppLanguageCode:(NSString *)code;
+ (BOOL)isUILayoutDirectionRtl;

View File

@ -39,6 +39,7 @@
#import "ASCLinguist.h"
#import "ASCConstants.h"
#import <Carbon/Carbon.h>
@implementation ASCLinguist
@ -86,6 +87,44 @@ static BOOL uiLayoutDirectionRTL = NO;
return uiLayoutDirectionRTL;
}
+ (NSDictionary *)keyboardLanguages {
NSMutableDictionary * outdict = [NSMutableDictionary dictionary];
CFArrayRef sourceList = TISCreateInputSourceList(NULL, false);
if ( sourceList ) {
CFIndex count = CFArrayGetCount(sourceList);
for (CFIndex i = 0; i < count; i++) {
TISInputSourceRef source = (TISInputSourceRef)CFArrayGetValueAtIndex(sourceList, i);
// Get only keyboard input sources
CFTypeRef type = TISGetInputSourceProperty(source, kTISPropertyInputSourceType);
if ( CFEqual(type, kTISTypeKeyboardLayout) || CFEqual(type, kTISTypeKeyboardInputMode) ) {
CFStringRef localizedName = TISGetInputSourceProperty(source, kTISPropertyLocalizedName);
NSString *name = (__bridge NSString *)localizedName;
// Get the language codes (ISO 639 format)
CFArrayRef languages = TISGetInputSourceProperty(source, kTISPropertyInputSourceLanguages);
if ( languages && CFArrayGetCount(languages) > 0 ) {
// Get the first language code (usually the primary one)
CFStringRef firstLang = CFArrayGetValueAtIndex(languages, 0);
NSString *langString = (__bridge NSString *)firstLang;
outdict[langString] = name;
// NSArray *components = [langString componentsSeparatedByString:@"-"];
// NSString *languageCode = components[0];
// if (components.count > 1) {
// NSString *region = components[1];
// NSLog(@"Language: %@ (ISO code: %@-%@)", name, languageCode, region);
// }
}
}
}
CFRelease(sourceList);
}
return outdict;
}
+ (NSDictionary *)availableLanguages {
return @{
@"en-US": @{@"name": @"English (United States)", @"enname": @""},

View File

@ -87,6 +87,10 @@
}
[self setParameter:@"lang" withString:[ASCLinguist appLanguageCode]];
NSMutableDictionary * keybLangs = [NSMutableDictionary dictionary];
[keybLangs setObject:[ASCLinguist keyboardLanguages] forKey:@"langs"];
[_jsVariables setObject:keybLangs forKey:@"keyboard"];
}
return self;