5

I am developing a text-to-speech iphone app that support multi-languages.

Here is my request URL

requestUrlStr = @"http://www.translate.google.com/translate_tts?tl=en&q=hello";

for english the above url has no problem

but for Chinese

requestUrlStr = @"http://www.translate.google.com/translate_tts?tl=zh-TW&q=你好";

I know the above url will give 'Bad URL', so I used follow method to encode the string into UTF-8

requestUrlStr = [requestUrlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

It will become

http://www.translate.google.com/translate_tts?tl=zh-CN&q=%E4%BD%A0%E5%A5%BD

Then the Google TTS cannot recognize this Chinese text.

Charles
  • 50,943
  • 13
  • 104
  • 142
a380
  • 53
  • 1
  • 5
  • there seems to be some referrer restrictions... may be that is the problem.. and not the URL : http://jalam1001.posterous.com/google-text-to-speech-tts-weston-ruter-sent-u – govi May 08 '11 at 01:42

1 Answers1

10

You have to pretend to be a User-Agent other than the default (appName, etc) in your NSURLRequest. Try this (I use Greek language) ...

NSString* userAgent = @"Mozilla/5.0";

NSURL *url = [NSURL URLWithString:[@"http://www.translate.google.com/translate_tts?tl=el&q=Καλημέρα" 
                                   stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];


NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];

[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];


NSURLResponse* response = nil;
NSError* error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request
                                     returningResponse:&response
                                                 error:&error];



[data writeToFile:@"/var/tmp/tts.mp3" atomically:YES];

UPDATE 2017

Since our favorite companies enjoy to update and deprecate things, here is the above example as it should be now...

NSString* text = @"καλημέρα";
NSString* lang = @"el";

NSString* sUrl = [NSString stringWithFormat:@"https://translate.google.com/translate_tts?q=%@&tl=%@&client=tw-ob", text, lang];
sUrl = [sUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
NSURL* url = [NSURL URLWithString:sUrl];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:@"Mozilla/5.0" forHTTPHeaderField:@"User-Agent"];

NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
                                                      delegate:nil
                                                 delegateQueue:[NSOperationQueue mainQueue]];

[[session dataTaskWithRequest:request
            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                [data writeToFile:@"/var/tmp/tts.mp3" atomically:YES];
            }
  ] resume];

The ...delegate:nil delegateQueue:[NSOperationQueue mainQueue] can be omitted.

Vassilis
  • 2,878
  • 1
  • 28
  • 43
  • It works perfectly. I dun know how many times I have to say thankyou to you. You really helped my problem alot. You are rock!! – a380 May 17 '11 at 06:19
  • I have a similar problem on android and haven't gotten any answers. what exactly are you doing here? – Amanni Apr 20 '12 at 12:11
  • Have you tried to make the request through a browser? Did google returned the mp3 ? – Vassilis Apr 21 '12 at 17:48