Google(R) Translate



04 May 2009 (Updated )

Translates a string between languages using Google(R)'s Language API.

function xlatedString = gtranslate( inputString, destLanguage, sourceLanguage )
%TRANSLATE Uses Google web service to translate a string
%  str = translate(input) converts English string to French using the
%          Google language API.
%  str = translate(input, destinationLanguage) converts English string to
%          another language. The destinationLanguage should be a two-letter
%          language code (example: en, fr, tk, es). See the Google Language
%          API for a list of supported languages:
%  str = translate(input, destLanguage, sourceLanguage) converts a
%          sourceLanguage string to the destLanguage. Both source and
%          destination languages must be the two letter strings. 
% Note that many of the supported languages will be undisplayable in the
% default character encoding on Windows.
% If an incorrect language is supplied, there will be a gtranslate:error
% error usually with the message: "invalid translation language pair"
% For more information about the Google language API see:
% Make sure all your usage conforms to their Terms of Service:

% Acknowledgements:  Franois Glineur for (another) JSON Parser

% Copyright 2009 The MathWorks, Inc.

if nargin < 2
    destLanguage = 'fr';
if nargin < 3
    %'en' (English) is the base language
    sourceLanguage = 'en';

%build url and send to google
url = '';
page = urlread(url, 'get', {'v', '1.0','q', inputString, ...
    'langpair', [sourceLanguage '|' destLanguage]});

%parse the response
response = parse_json(page);
if response.responseStatus ~= 200 %there was an error
    error('gtranslate:error', response.responseDetails);
xlatedString = response.responseData.translatedText;

Contact us