/// <summary> /// Creates objects needed for the writing process and starts it. /// </summary> /// <param name="fileCreationType"> /// The strategy for creating the excel file. /// It is assumed that the function is only called with <see cref="FileCreationType.UpdateExistingFile" />, /// <see cref="FileCreationType.CreateEmptyFile" /> or <see cref="FileCreationType.CreateNewFile" /> /// as possible values. /// </param> /// <param name="translationsDictionary"> /// The translations that should be written into the dictionary. /// </param> /// <param name="path">The path of the excel sheet.</param> private void CreateExelFileBasedOnCreationType(FileCreationType fileCreationType, Dictionary<CultureInfo, Dictionary<string, string>> translationsDictionary, string path) { var excel = new ExcelInterop.Application(); ExcelInterop.Workbook workbook = null; try { //FileCreationType.UpdateExistingFile if (fileCreationType == FileCreationType.UpdateExistingFile) { workbook = excel.Workbooks.Open(System.IO.Path.GetFullPath(path)); } //FileCreationType.CreateEmptyFile or FileCreationType.CreateNewFile else { workbook = excel.Workbooks.Add(); } //FileCreationType.UpdateExistingFile or FileCreationType.CreateNewFile if (fileCreationType != FileCreationType.CreateEmptyFile) { //get parameters. var worksheet = (ExcelInterop.Worksheet) workbook.Worksheets[1]; var textLocalizations = TextLocalizationsUtils.FlipLocalizationsDictionary(translationsDictionary); //write to sheet. WriteTranslationsToWorksheet(worksheet, textLocalizations); } //saving. excel.DisplayAlerts = false; //FileCreationType.UpdateExistingFile. if (fileCreationType == FileCreationType.UpdateExistingFile) { workbook.Save(); } //FileCreationType.CreateEmptyFile or FileCreationType.CreateNewFile. else { workbook.SaveAs(System.IO.Path.GetFullPath(path)); } } finally { workbook?.Close(); excel.Quit(); } }
public override ObservableCollection <TextLocalization> GetGuiTranslation(DependencyObject element) { if (!ControlElementInspector.GetControlProperties(element, out var controlId, out var currentText, out var controlType, out var parentDialogName)) { return(null); } var dictOfDicts = GetDictionaryFromFileProvider(); ICollection <TextLocalization> localizations = new Collection <TextLocalization>(); foreach (var language in dictOfDicts.Keys) { localizations.Add(GetLiteral(language, parentDialogName, controlType, controlId, true)); } //if entry is new, use text from XAML. if (localizations.All(localization => string.IsNullOrWhiteSpace(localization.Text))) { localizations.First(localization => Equals(localization.Language, InputLanguage)).Text = currentText; } GetTranslationDummyText(localizations, InputLanguage, PreferredLanguage); //fill known translations and convert to ObservableCollection. var sourceLocalization = localizations.FirstOrDefault(loc => Equals(loc.Language, InputLanguage)); var observableLocalizations = new ObservableCollection <TextLocalization>(); foreach (var localization in localizations) { if (sourceLocalization != null) { localization.KnownTranslations = TextLocalizationsUtils .ExtractKnownTranslations(sourceLocalization.Text, localization.Language, InputLanguage, dictOfDicts); } observableLocalizations.Add(localization); } return(observableLocalizations); }
public override ObservableCollection <TextLocalization> GetGuiTranslation(DependencyObject element) { //collect translation individually. ICollection <TextLocalization> localizations = new Collection <TextLocalization>(); foreach (var lang in GetKnownLanguages()) { var translation = GetTranslation(GetKeyFromUnkownElementType(element), lang, true); localizations.Add(new TextLocalization { Language = lang, Text = translation }); } //fill translations without Text. GetTranslationDummyText(localizations, InputLanguage, PreferredLanguage); //fill known translations and convert to ObservableCollection. var sourceLocalization = localizations.FirstOrDefault(loc => Equals(loc.Language, InputLanguage)); var observableLocalizations = new ObservableCollection <TextLocalization>(); foreach (var localization in localizations) { if (sourceLocalization != null) { localization.KnownTranslations = TextLocalizationsUtils .ExtractKnownTranslations(sourceLocalization.Text, localization.Language, InputLanguage, _dictOfDicts); } observableLocalizations.Add(localization); } return(observableLocalizations); }