/// <summary> /// Flag a language code as used. /// </summary> /// <param name="code">The code that has been used</param> public static void RegisterUsage(string code) { LanguageCode languageCode = FindLanguageCode(code); if (languageCode != null) { languageCode.Used = true; } else { addUndefinedCode(code); } }
/// <summary> /// Add a language code to the collection. /// </summary> /// <param name="newLanguageCode">The language code to be added.</param> public static void addLanguageCode(LanguageCode newLanguageCode) { foreach (LanguageCode oldLanguageCode in languageCodes) { if (oldLanguageCode.Code == newLanguageCode.Code) { return; } if (oldLanguageCode.Code.CompareTo(newLanguageCode.Code) > 0) { languageCodes.Insert(languageCodes.IndexOf(oldLanguageCode), newLanguageCode); return; } } languageCodes.Add(newLanguageCode); }
/// <summary> /// Translate a text string. /// </summary> /// <param name="inputLanguage">The language code of the string.</param> /// <param name="outputLanguage">The language code of the translated string.</param> /// <param name="text">The text to be translated.</param> /// <returns>The translated text.</returns> public static string GetTranslatedText(string inputLanguage, string outputLanguage, string text) { string actualInputLanguage = string.Empty; if (inputLanguage != null) { actualInputLanguage = LanguageCode.FindLanguageCode(inputLanguage).TranslationCode; } else { actualInputLanguage = "en"; } if (actualInputLanguage == null || actualInputLanguage == string.Empty) { return(text); } string actualOutputLanguage = string.Empty; if (outputLanguage != null) { actualOutputLanguage = LanguageCode.FindLanguageCode(outputLanguage).TranslationCode; } else { actualOutputLanguage = "en"; } if (actualOutputLanguage == null || actualOutputLanguage == string.Empty) { return(text); } if (outputLanguage == null || inputLanguage == outputLanguage) { return(text); } /*string appId = "7212DAA1C62A41C5DC706D0B2F6EC913B85E51BE"; * string buffer = sendWebRequest("http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=" + * appId + "&text=" + text + "&from=" + actualInputLanguage + "&to=" + actualOutputLanguage);*/ string userString = string.Empty; IPAddress[] addresses = Dns.GetHostAddresses(Dns.GetHostName()); if (addresses.Length != 0) { userString = "&userip=" + addresses[0].ToString(); } string appId = "AIzaSyCvxyXBtSIzUYtdm1RCziI5-PzESh7yVpk"; string buffer = sendWebRequest("https://ajax.googleapis.com/ajax/services/language/translate?v=1.0" + "&q=" + text.Replace(" ", "%20") + "&langpair=" + actualInputLanguage + "%7C" + actualOutputLanguage + "&key=" + appId + userString); if (buffer == null) { return(text); } int index1 = buffer.IndexOf(@"translatedText"); if (index1 == -1) { return(buffer); } index1 += 17; int index2 = buffer.IndexOf('"', index1); if (index2 == -1) { return(buffer); } return(buffer.Substring(index1, index2 - index1)); }
/// <summary> /// Load the language code collection from the configuration file. /// </summary> public static void Load() { languageCodes = new Collection<LanguageCode>(); languageCodes.Add(new LanguageCode(string.Empty, " -- Undefined --")); LanguageCode languageCode = null; XmlReader reader = null; XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true; string actualName = Path.Combine(RunParameters.ConfigDirectory, fileName); try { reader = XmlReader.Create(actualName, settings); } catch (IOException) { Logger.Instance.Write("Failed to open " + actualName); return; } try { while (!reader.EOF) { reader.Read(); if (reader.IsStartElement()) { switch (reader.Name) { case "LanguageCode": if (languageCode != null) addLanguageCode(languageCode); languageCode = new LanguageCode(); languageCode.Code = reader.GetAttribute("code").Trim().ToLowerInvariant(); languageCode.Description = reader.GetAttribute("description").Trim(); string translationCode = reader.GetAttribute("translationcode"); if (translationCode != null) languageCode.TranslationCode = translationCode.Trim().ToLowerInvariant(); break; default: break; } } } if (languageCode != null) addLanguageCode(languageCode); } catch (XmlException e) { Logger.Instance.Write("Failed to load file " + actualName); Logger.Instance.Write("Data exception: " + e.Message); } catch (IOException e) { Logger.Instance.Write("Failed to load file " + actualName); Logger.Instance.Write("I/O exception: " + e.Message); } if (reader != null) reader.Close(); }
/// <summary> /// Add a language code to the collection. /// </summary> /// <param name="newLanguageCode">The language code to be added.</param> public static void addLanguageCode(LanguageCode newLanguageCode) { foreach (LanguageCode oldLanguageCode in languageCodes) { if (oldLanguageCode.Code == newLanguageCode.Code) return; if (oldLanguageCode.Code.CompareTo(newLanguageCode.Code) > 0) { languageCodes.Insert(languageCodes.IndexOf(oldLanguageCode), newLanguageCode); return; } } languageCodes.Add(newLanguageCode); }
/// <summary> /// Load the language code collection from the configuration file. /// </summary> public static void Load() { languageCodes = new Collection <LanguageCode>(); languageCodes.Add(new LanguageCode(string.Empty, " -- Undefined --")); LanguageCode languageCode = null; XmlReader reader = null; XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true; string actualName = Path.Combine(RunParameters.ConfigDirectory, fileName); try { reader = XmlReader.Create(actualName, settings); } catch (IOException) { Logger.Instance.Write("Failed to open " + actualName); return; } try { while (!reader.EOF) { reader.Read(); if (reader.IsStartElement()) { switch (reader.Name) { case "LanguageCode": if (languageCode != null) { addLanguageCode(languageCode); } languageCode = new LanguageCode(); languageCode.Code = reader.GetAttribute("code").Trim().ToLowerInvariant(); languageCode.Description = reader.GetAttribute("description").Trim(); string translationCode = reader.GetAttribute("translationcode"); if (translationCode != null) { languageCode.TranslationCode = translationCode.Trim().ToLowerInvariant(); } break; default: break; } } } if (languageCode != null) { addLanguageCode(languageCode); } } catch (XmlException e) { Logger.Instance.Write("Failed to load file " + actualName); Logger.Instance.Write("Data exception: " + e.Message); } catch (IOException e) { Logger.Instance.Write("Failed to load file " + actualName); Logger.Instance.Write("I/O exception: " + e.Message); } if (reader != null) { reader.Close(); } }