public AsyncTranslationPayload(Guid id, List<string> stringsToTranslate, ILanguage sourceLanguage, ILanguage destLanguage, ILocanTranslator translator) { if (id == Guid.Empty) { throw new ArgumentException("id cannot equal Guid.Empty"); } if (stringsToTranslate == null) { throw new ArgumentNullException("stringsToTranslate"); } if (translator == null) { throw new ArgumentNullException("translator"); } if (sourceLanguage == null) { throw new ArgumentNullException("sourceLanguage"); } if (destLanguage == null) { throw new ArgumentNullException("destLanguage"); } this.Transloator = translator as IAsyncTranslator; if (this.Transloator == null) { throw new UnexpectedStateException("The translator provided doesn't support async operations"); } this.Id = id; this.StringsToTranslate = stringsToTranslate; this.SourceLanguage = sourceLanguage; this.DestLanguage = destLanguage; }
/// <summary> /// This method will look at {Filename}.resx and compare it to {Filename}.resx.cache to see if /// the user has customized {Filename}.resx and if so it will send the updated values to bing /// </summary> private void SendTranslationUpdatesToBing(string resxFilePath, IList <ILocanRow> rowsToTranslate, ILanguage sourceLanguage, ILanguage destLanguage, ILocanTranslator translator) { if (string.IsNullOrWhiteSpace(resxFilePath)) { throw new ArgumentNullException("resxFile"); } if (rowsToTranslate == null) { throw new ArgumentNullException("rowsToTranslate"); } if (sourceLanguage == null) { throw new ArgumentNullException("sourceLanguage"); } if (destLanguage == null) { throw new ArgumentNullException("destLanguage"); } if (translator == null) { throw new ArgumentNullException("translator"); } string resxTranslationFile = this.GetDestFilename(resxFilePath, destLanguage); FileInfo translatedResxFileInfo = new FileInfo(resxTranslationFile); string cacheFile = string.Format("{0}.cache", translatedResxFileInfo.FullName); FileInfo cacheFileInfo = new FileInfo(cacheFile); if (translatedResxFileInfo.Exists && cacheFileInfo.Exists) { IList <ILocanRow> currentTranslatedResxContent = this.ReadResxFileTranslated(translatedResxFileInfo.FullName); // convert resxContent to a dictionary IDictionary <string, string> translateDictionary = this.ConvertTranslatedStringsToDictionary(currentTranslatedResxContent); IList <ILocanRow> cacheContent = this.ReadResxFileTranslated(cacheFileInfo.FullName); IDictionary <string, string> customTranslations = new Dictionary <string, string>(); // now compute the difference foreach (ILocanRow cacheRow in cacheContent) { if (!string.IsNullOrWhiteSpace(cacheRow.Id)) { string currentValue = null; translateDictionary.TryGetValue(cacheRow.Id, out currentValue); if (currentValue != null && string.Compare(currentValue, cacheRow.TranslatedString) != 0) { // the values are different must have been customized by the user // we have to look up the original string in rowsToTranslate string originalText = null; foreach (ILocanRow row in rowsToTranslate) { // TODO: Consider making a dictionary for the rowsToTranslate, if there are a bunch of customizations // then this may take a while. // But there should not be too many updates because Bing should always have the latest, // so in 90% of cases the changes should be small. if (string.Compare(cacheRow.Id, row.Id, StringComparison.OrdinalIgnoreCase) == 0) { originalText = row.StringToTranslate; break; } } if (string.IsNullOrWhiteSpace(originalText)) { string message = string.Format("Unable to find original text for id: [{0}]", cacheRow.Id); throw new UnexpectedStateException(message); } customTranslations.Add(originalText, currentValue); } } } if (customTranslations.Count > 0) { translator.AddCustomTranslations(sourceLanguage, destLanguage, customTranslations); } } }
/// <summary> /// This method will look at {Filename}.resx and compare it to {Filename}.resx.cache to see if /// the user has customized {Filename}.resx and if so it will send the updated values to bing /// </summary> private void SendTranslationUpdatesToBing(string resxFilePath, IList<ILocanRow> rowsToTranslate, ILanguage sourceLanguage, ILanguage destLanguage, ILocanTranslator translator) { if (string.IsNullOrWhiteSpace(resxFilePath)) { throw new ArgumentNullException("resxFile"); } if (rowsToTranslate == null) { throw new ArgumentNullException("rowsToTranslate"); } if (sourceLanguage == null) { throw new ArgumentNullException("sourceLanguage"); } if (destLanguage == null) { throw new ArgumentNullException("destLanguage"); } if (translator == null) { throw new ArgumentNullException("translator"); } string resxTranslationFile = this.GetDestFilename(resxFilePath, destLanguage); FileInfo translatedResxFileInfo = new FileInfo(resxTranslationFile); string cacheFile = string.Format("{0}.cache", translatedResxFileInfo.FullName); FileInfo cacheFileInfo = new FileInfo(cacheFile); if (translatedResxFileInfo.Exists && cacheFileInfo.Exists) { IList<ILocanRow> currentTranslatedResxContent = this.ReadResxFileTranslated(translatedResxFileInfo.FullName); // convert resxContent to a dictionary IDictionary<string, string> translateDictionary = this.ConvertTranslatedStringsToDictionary(currentTranslatedResxContent); IList<ILocanRow> cacheContent = this.ReadResxFileTranslated(cacheFileInfo.FullName); IDictionary<string, string> customTranslations = new Dictionary<string, string>(); // now compute the difference foreach (ILocanRow cacheRow in cacheContent) { if (!string.IsNullOrWhiteSpace(cacheRow.Id)) { string currentValue = null; translateDictionary.TryGetValue(cacheRow.Id, out currentValue); if (currentValue != null && string.Compare(currentValue, cacheRow.TranslatedString) != 0) { // the values are different must have been customized by the user // we have to look up the original string in rowsToTranslate string originalText = null; foreach (ILocanRow row in rowsToTranslate) { // TODO: Consider making a dictionary for the rowsToTranslate, if there are a bunch of customizations // then this may take a while. // But there should not be too many updates because Bing should always have the latest, // so in 90% of cases the changes should be small. if (string.Compare(cacheRow.Id, row.Id, StringComparison.OrdinalIgnoreCase) == 0) { originalText = row.StringToTranslate; break; } } if (string.IsNullOrWhiteSpace(originalText)) { string message = string.Format("Unable to find original text for id: [{0}]", cacheRow.Id); throw new UnexpectedStateException(message); } customTranslations.Add(originalText, currentValue); } } } if (customTranslations.Count > 0) { translator.AddCustomTranslations(sourceLanguage, destLanguage, customTranslations); } } }
public AsyncTranslationPayload(Guid id, List <string> stringsToTranslate, ILanguage sourceLanguage, ILanguage destLanguage, ILocanTranslator translator) { if (id == Guid.Empty) { throw new ArgumentException("id cannot equal Guid.Empty"); } if (stringsToTranslate == null) { throw new ArgumentNullException("stringsToTranslate"); } if (translator == null) { throw new ArgumentNullException("translator"); } if (sourceLanguage == null) { throw new ArgumentNullException("sourceLanguage"); } if (destLanguage == null) { throw new ArgumentNullException("destLanguage"); } this.Transloator = translator as IAsyncTranslator; if (this.Transloator == null) { throw new UnexpectedStateException("The translator provided doesn't support async operations"); } this.Id = id; this.StringsToTranslate = stringsToTranslate; this.SourceLanguage = sourceLanguage; this.DestLanguage = destLanguage; }