Example #1
0
        public static string[] LanguageNames(string[] languageCodes)
        {
            if (admToken == null) // We initialize it just one until the timer expires
            {
                //Get Client Id and Client Secret from https://datamarket.azure.com/developer/applications/
                //Refer obtaining AccessToken (http://msdn.microsoft.com/en-us/library/hh454950.aspx) 
                AuthenticationToken admAuth = new AuthenticationToken(ClientID, ClientSecret);
                admToken = admAuth.GetAccessToken();
            }

            TranslateService.LanguageServiceClient client = new TranslateService.LanguageServiceClient();
            string[] langNames = client.GetLanguageNames("Bearer" + " " + admToken.access_token, CultureInfo.CurrentCulture.Name, languageCodes);
            client.Close();
            return langNames;
        }
Example #2
0
        public Translate(string localisationPath, string languageCode, string languageName, int row)
        {
            _localisationPath = localisationPath;
            _languageCode = languageCode;
            _languageName = languageName;
            _row = row;
            _englishFilePath = Path.Combine(_localisationPath, "en.txt");
            if (!File.Exists(_englishFilePath))
            {
                Console.WriteLine("English source file not found " + _englishFilePath);
                _cancelled = true; // exit the app
            }

            if (admToken == null) // We initialize it just one until the timer expires
            {
                //Get Client Id and Client Secret from https://datamarket.azure.com/developer/applications/
                //Refer obtaining AccessToken (http://msdn.microsoft.com/en-us/library/hh454950.aspx) 
                AuthenticationToken admAuth = new AuthenticationToken(ClientID, ClientSecret);
                admToken = admAuth.GetAccessToken();
            }
        }
Example #3
0
        private string TranslateText(string englishText, string language)
        {
            try
            {
                // Keep alternating to avoid quota limit issues
                return _client.Translate("Bearer" + " " + admToken.access_token, englishText, "en", language, "text/plain", "general");
            }
            catch (Exception)
            {
                // Maybe IP over quota in which case we need to back off for a while
                // Authentication token expired (valid only 10 minutes at a time)
                // Get a new authentication token
                _client.Close(); // Close it
                _quotaError = true;

                // Wait for 150 seconds for each failure and try again with exponential waiting time each period
                for (long i = 150 * (long)System.Math.Pow((double)++quotaCount, (double)2); i > 0; i--)
                {
                    if (_cancelled)
                        return null;

                    if (Console.KeyAvailable) // if any thread detected an cancel signal (ESC)
                    {
                        ConsoleKeyInfo cki = Console.ReadKey(true);
                        if ((cki.Key == ConsoleKey.Escape) || ((cki.Modifiers == ConsoleModifiers.Control) && (cki.Key == ConsoleKey.C)))
                        {
                            _cancelled = true;
                            return null;
                        }
                    }
                    string consoleTxt = _languageCode + " (" + _languageName + ")" + " -> " + _progress + " translations. Quota exceeded, retrying in " + i + " seconds...";
                    TranslateAll.WriteStatus(consoleTxt, _row, ConsoleColor.Red);
                    Thread.Sleep(1000); // Wait 1 second
                }

                _client = new TranslateService.LanguageServiceClient(); // Create a new connection
     
                try
                {
                    _quotaError = false;
                    AuthenticationToken admAuth = new AuthenticationToken(ClientID, ClientSecret);
                    admToken = admAuth.GetAccessToken();
                    return _client.Translate("Bearer" + " " + admToken.access_token, englishText, "en", language, "text/plain", "general");
                }
                catch (Exception)
                {
                    // Give up and return error
                    //Console.WriteLine(e.ToString()); // Dump the error on the screen
                    return null; // Return a null to indicate an error
                }
            }
        }