예제 #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;
        }
예제 #2
0
        public void WorkerProc()
        {
            _success = true; // all is good unless something fails
            _active = true;

            _client = new TranslateService.LanguageServiceClient();

            // Get the manually corrected values if they exist
            System.Collections.Generic.SortedList<string, string> translatedValues = new SortedList<string, string>(); // automatic translations
            System.Collections.Generic.SortedList<string, string> manualValues = new SortedList<string, string>(); // User translations

            string fixedLanguageFileName = Path.Combine(_localisationPath, _languageCode + "-fixed.txt");

            if (File.Exists(fixedLanguageFileName))
            {
                using (CsvFileReader reader = new CsvFileReader(fixedLanguageFileName))
                {
                    CsvRow row = new CsvRow();
                    while (reader.ReadRow(row))
                    {
                        if (row.Count > 1)
                        {
                            if (!manualValues.ContainsKey(row[0]))
                            {
                                manualValues.Add(row[0], row[1]); // Create a sorted list of values with manual updates
                            }
                        }
                    }
                }
            }

            // Create the language file
            string languageFileName = Path.Combine(_localisationPath, _languageCode + ".txt");

            // If the file already exists then don't overwrite it
            // So we don't overwrite from scratch. If new translations are required then the existing files needs to be deleted first
            // If the language file already exists then load the file so we can add new strings and deletes one not being used
            // BING now limits the amount of data that can be downloaded so we should only update what's required instead of downloading everthing again
            CsvRow orgRow = new CsvRow();
            if (File.Exists(languageFileName))
            {
                using (CsvFileReader orgReader = new CsvFileReader(languageFileName))
                {
                    while (orgReader.ReadRow(orgRow)) // Read all the data in
                    {
                        if (manualValues.ContainsKey(orgRow[0]))
                            _manual++; // We have a manual translation

                        if (!translatedValues.ContainsKey(orgRow[0]))
                            translatedValues.Add(orgRow[0], orgRow[1]); // Add to the sorted listed - we don't add manual translations here, just track them. They are added on the fly at runtime
                        else
                            _duplicate++; // Duplicate
                    }
                }
            }

            using (CsvFileWriter writer = new CsvFileWriter(languageFileName))
            {
                using (CsvFileReader reader = new CsvFileReader(_englishFilePath))
                {
                    CsvRow row = new CsvRow();
                    while (reader.ReadRow(row))
                    {
                        if (row.Count > 0)
                        {
                            while (row.Count > 1) row.RemoveAt(1);
                            string transText = "";
                            if (translatedValues.ContainsKey(row[0]))
                            {
                                transText = translatedValues[row[0]];
                                _replaced++;
                            }
                            else
                            {
                                transText = TranslateText(row[0], _languageCode);

                                // check for a null return, then BING failed due to quota issues
                                if (transText == null)
                                {
                                    _success = false; // break out and clean up the file
                                    break;
                                }
                                else
                                    _downloaded++;
                            }

                            row.Add(transText);
                            writer.WriteRow(row); // Write the data
                            _progress++;

                            if (_cancelled)
                            {
                                _success = false;
                                break;
                            }

                            if (Console.KeyAvailable) // check if any other thread caught the cancellation event (ESC)
                            {
                                ConsoleKeyInfo cki = Console.ReadKey(true);
                                if ((cki.Key == ConsoleKey.Escape) || ((cki.Modifiers == ConsoleModifiers.Control) && (cki.Key == ConsoleKey.C)))
                                {
                                    _cancelled = true; // signal all thread to exit
                                    _success = false; // Clean up the file
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            _client.Close();

            _active = false; // First set this to avoid overwriting console

            // Check for failure, if so delete the file - we do not keep partially translated files
            if (_success == false)
            {
                string consoleTxt = _languageCode + " (" + _languageName + ")" +" -> " + "Translation cancelled...";
                TranslateAll.WriteStatus(consoleTxt, _row, ConsoleColor.DarkMagenta);
                File.Delete(languageFileName);
            }
            else
            {
                string consoleTxt = _languageCode + " (" + _languageName + ")" + " -> " + _progress + " translations complete! Downloaded " + _downloaded + " Existing " + _replaced + " Manual " + _manual + " Duplicate " + _duplicate;
                TranslateAll.WriteStatus(consoleTxt, _row, ConsoleColor.Green);
            }

            return;
        }