-
Notifications
You must be signed in to change notification settings - Fork 0
/
TranModule.m
75 lines (65 loc) · 2.62 KB
/
TranModule.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import "TranModule.h"
@implementation TranModule
+ (NSString *)translate:(NSString *)text fromLang:(NSString *)fromLang toLang:(NSString *)toLang
{
NSString *encodedString = [text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]];
encodedString = [encodedString stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"];
NSArray *sentences = [encodedString componentsSeparatedByString: [NSString stringWithUTF8String: "\n"]];
NSMutableString *result = [NSMutableString string];
for (NSString *sentence in sentences)
{
NSString *trimmedSentence = [sentence stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([trimmedSentence length] > 0)
{
NSString *urlString = [NSString stringWithFormat:
@"https://lingva-translate-eta.vercel.app/api/v1/%@/%@/%@",
fromLang,
toLang,
[trimmedSentence stringByAddingPercentEncodingWithAllowedCharacters:
[NSCharacterSet URLQueryAllowedCharacterSet]]];
NSURL *url = [NSURL URLWithString: urlString];
if (!url)
{
NSLog(@"Некорректный URL для предложения: %@", trimmedSentence);
continue;
}
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url
cachePolicy: NSURLRequestReloadIgnoringCacheData
timeoutInterval: 60];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest: request
returningResponse: &response
error:&error];
if (error)
{
NSLog(@"Ошибка при загрузке данных: %@", [error localizedDescription]);
continue;
}
NSError *jsonError;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData: data
options: 0
error: &jsonError];
if (jsonError)
{
NSLog(@"Ошибка при парсинге JSON: %@", [jsonError localizedDescription]);
continue;
}
NSString *translatedText = [json objectForKey: @"translation"];
if (translatedText)
{
[result appendString: translatedText];
[result appendString: [NSString stringWithUTF8String: "\n"]];
}
}
}
encodedString = [[result stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]]
stringByRemovingPercentEncoding];
encodedString = [encodedString stringByReplacingOccurrencesOfString:@"%2F" withString:@"/"];
return encodedString;
}
@end