diff --git a/out.strings b/out.strings index 0408f23..44c3fb9 100644 --- a/out.strings +++ b/out.strings @@ -1,5 +1,5 @@ /* example string to localize */ -"greeting" = "Connectez-vous sur MyCochlear" +"greeting" = "Se connecter sur mon programme" /* I am @@ -8,4 +8,4 @@ multiline comment. Followed by another key/value pair */ -"coffee" = "Je café de l'amour" +"coffee" = "J'ai aiment le café" diff --git a/tstrings.py b/tstrings.py index a49597e..8ecab4b 100644 --- a/tstrings.py +++ b/tstrings.py @@ -16,7 +16,11 @@ SPEECH=chr(34) api_url = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate" -app_id = '' +token_url = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13" +access_token = '' +client_id = '' +client_secret = '' + def _unicode_urlencode(params): """ @@ -28,6 +32,26 @@ def _unicode_urlencode(params): return urllib.urlencode([(k, isinstance(v, unicode) and v.encode('utf-8') or v) for k, v in params]) +def _generate_token(): + args = { + 'grant_type': 'client_credentials', + 'client_id': client_id, + 'client_secret': client_secret, + 'scope': 'http://api.microsofttranslator.com' + } + data = urllib.urlencode(args) + sock = urllib.urlopen(token_url, data) + result = sock.read() + if result.startswith(codecs.BOM_UTF8): + result = result.lstrip(codecs.BOM_UTF8).decode('utf-8') + elif result.startswith(codecs.BOM_UTF16_LE): + result = result.lstrip(codecs.BOM_UTF16_LE).decode('utf-16-le') + elif result.startswith(codecs.BOM_UTF16_BE): + result = result.lstrip(codecs.BOM_UTF16_BE).decode('utf-16-be') + + return json.loads(result) + + def _run_query(args): """ takes arguments and optional language argument and runs query on server @@ -36,26 +60,34 @@ def _run_query(args): sock = urllib.urlopen(api_url + '?' + data) result = sock.read() if result.startswith(codecs.BOM_UTF8): - result = result.lstrip(codecs.BOM_UTF8).decode('utf-8') + result = result.lstrip(codecs.BOM_UTF8).decode('utf-8') elif result.startswith(codecs.BOM_UTF16_LE): result = result.lstrip(codecs.BOM_UTF16_LE).decode('utf-16-le') elif result.startswith(codecs.BOM_UTF16_BE): result = result.lstrip(codecs.BOM_UTF16_BE).decode('utf-16-be') return json.loads(result) -def set_app_id(new_app_id): - global app_id - app_id = new_app_id +def set_client_id(new_client_id): + global client_id + client_id = new_client_id + +def set_client_secret(new_client_secret): + global client_secret + client_secret = new_client_secret + +def set_access_token(new_access_token): + global access_token + access_token = new_access_token def translate(text, source, target, html=False): """ action=opensearch """ - if not app_id: - raise ValueError("AppId needs to be set by set_app_id") + if not access_token: + raise ValueError("App token isn't set. Check values of client_id and client_secret") query_args = { - 'appId': app_id, + 'appId': "Bearer " + access_token, 'text': text, 'from': source, 'to': target, @@ -117,7 +149,7 @@ def read_from_file(self, fname=None): for c in comments: self.strings.append(c) #I'm not sure at this time of night how to migrate the regex symbols across but lucky I know what they are :-P - self.strings.append(SPEECH + string.key + SPEECH + " = " + SPEECH + translatedstring + SPEECH+'\n') + self.strings.append(SPEECH + string.key + SPEECH + " = " + SPEECH + translatedstring + SPEECH+';\n') self.strings_d[string.key] = string f.close() @@ -161,9 +193,12 @@ def main(): parser.add_argument('-d',help='Dest Language (e.g fr,ko,ru etc -Check bing',required=False,default='fr') Options = parser.parse_args() - - #brads bing id using for translation - set_app_id('2A9444C210A043010CB9FC4AB8B6DE797AE88790') + + set_client_id('') + set_client_secret('') + token = _generate_token() + + set_access_token(token["access_token"]) #load in strings file LF=LocalizedFile(translate,Options.i,True,Options.s,Options.d) LF.save_to_file(Options.o)