Skip to content

Commit

Permalink
Semantic efactoring: language -> locale
Browse files Browse the repository at this point in the history
  • Loading branch information
sveinbjornt committed Oct 25, 2023
1 parent 760cbef commit 07683b1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/Hear.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

+ (void)printSupportedLanguages;

- (instancetype)initWithLanguage:(NSString *)language
- (instancetype)initWithLocale:(NSString *)language
input:(NSString *)input
onDevice:(BOOL)useOnDeviceRecognition
singleLineMode:(BOOL)singleLine
Expand Down
27 changes: 13 additions & 14 deletions src/Hear.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ @interface Hear()

@property (nonatomic, retain) AVAudioEngine *engine;

@property (nonatomic, retain) NSString *language;
@property (nonatomic, retain) NSString *locale;
@property (nonatomic, retain) NSString *inputFile;
@property (nonatomic) BOOL useDeviceInput;
@property (nonatomic) BOOL useOnDeviceRecognition;
Expand All @@ -53,21 +53,21 @@ @interface Hear()

@implementation Hear

- (instancetype)initWithLanguage:(NSString *)language
input:(NSString *)input
onDevice:(BOOL)onDevice
singleLineMode:(BOOL)singleLine
addPunctuation:(BOOL)punctuation
exitWord:(NSString *)exitWord {
- (instancetype)initWithLocale:(NSString *)loc
input:(NSString *)input
onDevice:(BOOL)onDevice
singleLineMode:(BOOL)singleLine
addPunctuation:(BOOL)punctuation
exitWord:(NSString *)exitWord {
self = [super init];
if (self) {

if ([[Hear supportedLanguages] containsObject:language] == NO) {
NSPrintErr(@"Locale '%@' not supported. Run with -s flag to see list of supported locales", language);
if ([[Hear supportedLanguages] containsObject:loc] == NO) {
NSPrintErr(@"Locale '%@' not supported. Run with -s flag to see list of supported locales", loc);
exit(EXIT_FAILURE);
}

self.language = language;
self.locale = loc;
self.inputFile = input;
self.useOnDeviceRecognition = onDevice;
self.singleLineMode = singleLine;
Expand All @@ -87,8 +87,7 @@ - (void)applicationDidFinishLaunching:(NSNotification *)notification {
#pragma mark -

- (void)die:(NSString *)errMsg {
NSString *msg = [NSString stringWithFormat:@"Error: %@", errMsg];
NSPrintErr(msg);
NSPrintErr(@"Error: %@", errMsg);
exit(EXIT_FAILURE);
}

Expand Down Expand Up @@ -124,7 +123,7 @@ - (void)requestSpeechRecognitionPermission {

- (void)initRecognizer {
// Initialize speech recognizer
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:self.language];
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:self.locale];
self.recognizer = [[SFSpeechRecognizer alloc] initWithLocale:locale];
if (self.recognizer == nil) {
[self die:@"Unable to initialize speech recognizer"];
Expand All @@ -137,7 +136,7 @@ - (void)initRecognizer {
}

if (self.useOnDeviceRecognition && !self.recognizer.supportsOnDeviceRecognition) {
[self die:[NSString stringWithFormat:@"On-device recognition is not supported for locale '%@'", self.language]];
[self die:[NSString stringWithFormat:@"On-device recognition is not supported for locale '%@'", self.locale]];
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ int main(int argc, const char * argv[]) { @autoreleasepool {
exit(EXIT_FAILURE);
}

NSString *language = DEFAULT_LOCALE;
NSString *locale = DEFAULT_LOCALE;
NSString *inputFilename;
NSString *exitWord;
BOOL useOnDeviceRecognition = NO;
Expand All @@ -98,7 +98,7 @@ int main(int argc, const char * argv[]) { @autoreleasepool {

// Set language (i.e. locale) for speech recognition
case 'l':
language = @(optarg);
locale = @(optarg);
break;

// Input filename (path)
Expand Down Expand Up @@ -143,12 +143,12 @@ int main(int argc, const char * argv[]) { @autoreleasepool {
}

// Instantiate app delegate object with core program functionality
Hear *hear = [[Hear alloc] initWithLanguage:language
input:inputFilename
onDevice:useOnDeviceRecognition
singleLineMode:singleLineMode
addPunctuation:addsPunctuation
exitWord:exitWord];
Hear *hear = [[Hear alloc] initWithLocale:locale
input:inputFilename
onDevice:useOnDeviceRecognition
singleLineMode:singleLineMode
addPunctuation:addsPunctuation
exitWord:exitWord];
[[NSApplication sharedApplication] setDelegate:hear];
[[NSApplication sharedApplication] run];

Expand Down

0 comments on commit 07683b1

Please sign in to comment.