private void OKButton_Click(object sender, RoutedEventArgs args) { // Unfocus parameter name TextBox to have its changes updated OKButton.Focus(); string textKey = TextKeyText.Text != null?TextKeyText.Text.Trim() : ""; // Validate the entered text key string errorMessage; if (!TextKeyViewModel.ValidateName(textKey, out errorMessage)) { App.WarningMessage(Tx.T("msg.invalid text key entered", "msg", errorMessage)); TextKeyText.Focus(); return; } string colonSuffix = null; string translationString = TranslationText.Text; if (SourceCSharpButton.IsChecked == true && AddColonCheckbox.IsChecked == true) { var match = Regex.Match(parsedText, @"^(.+?)\s*:(\s*)$"); if (match.Success) { translationString = match.Groups[1].Value; colonSuffix = match.Groups[2].Value; } } // Check if the text key already exists but the translated text is different TextKeyViewModel existingTextKeyVM; if (MainViewModel.Instance.TextKeys.TryGetValue(textKey, out existingTextKeyVM)) { if (translationString != existingTextKeyVM.CultureTextVMs[0].Text) { TaskDialogResult result = TaskDialog.Show( owner: this, allowDialogCancellation: true, title: "TxEditor", mainInstruction: Tx.T("window.wizard.text key exists", "key", Tx.Q(textKey)), content: Tx.T("window.wizard.text key exists.content"), customButtons: new string[] { Tx.T("task dialog.button.overwrite"), Tx.T("task dialog.button.cancel") }); if (result.CustomButtonResult != 0) { TextKeyText.Focus(); return; } } } // Backup current clipboard content ClipboardBackup = ClipboardHelper.GetDataObject(); // Build the text to paste back into the source document, depending on the selected code // language if (SourceCSharpButton.IsChecked == true && SourceHtmlButton.IsChecked != true) { App.Settings.Wizard.SourceCode = "C#"; string keyString = textKey.Replace("\\", "\\\\").Replace("\"", "\\\""); StringBuilder codeSb = new StringBuilder(); if (isPartialString) { codeSb.Append("\" + "); } codeSb.Append("Tx.T"); if (AddColonCheckbox.IsChecked == true) { codeSb.Append("C"); } codeSb.Append("(\"" + keyString + "\""); var countPlaceholder = placeholders.FirstOrDefault(p => p.Name == "#"); if (countPlaceholder != null) { codeSb.Append(", "); codeSb.Append(countPlaceholder.Code); } foreach (var pd in placeholders) { if (pd == countPlaceholder) { continue; // Already processed } if (string.IsNullOrWhiteSpace(pd.Name)) { continue; // Not used anymore } codeSb.Append(", \"" + pd.Name.Replace("\\", "\\\\").Replace("\"", "\\\"") + "\""); codeSb.Append(", "); if (pd.IsQuoted) { codeSb.Append("Tx.Q("); } codeSb.Append(pd.Code); if (pd.IsQuoted) { codeSb.Append(")"); } } codeSb.Append(")"); if (isPartialString) { codeSb.Append(" + \""); if (!string.IsNullOrEmpty(colonSuffix)) { codeSb.Append(colonSuffix); } } else if (!string.IsNullOrEmpty(colonSuffix)) { codeSb.Append(" + \""); codeSb.Append(colonSuffix); codeSb.Append("\""); } Clipboard.SetText(codeSb.ToString()); } if (SourceHtmlButton.IsChecked == true && SourceCSharpButton.IsChecked == true) { App.Settings.Wizard.SourceCode = "cshtml"; string keyString = textKey.Replace("\\", "\\\\").Replace("\"", "\\\""); StringBuilder codeSb = new StringBuilder(); codeSb.Append("@Tx.T"); if (AddColonCheckbox.IsChecked == true) { codeSb.Append("C"); } codeSb.Append("(\"" + keyString + "\")"); if (!string.IsNullOrEmpty(colonSuffix)) { codeSb.Append(colonSuffix); } Clipboard.SetText(codeSb.ToString()); } if (SourceXamlButton.IsChecked == true) { App.Settings.Wizard.SourceCode = "XAML"; string keyString = textKey.Replace("\\", "\\\\").Replace("'", "\\'"); string defaultString = translationString.Replace("\\", "\\\\").Replace("'", "\\'"); string code = "{Tx:T '" + keyString + "'"; if (SetDefaultCheckbox.IsChecked == true) { code += ", Default='" + defaultString + "'"; } code += "}"; Clipboard.SetText(code); } if (SourceAspxButton.IsChecked == true) { App.Settings.Wizard.SourceCode = "aspx"; string keyString = textKey.Replace("\\", "\\\\").Replace("\"", "\\\""); StringBuilder codeSb = new StringBuilder(); codeSb.Append("<%= Tx.T(\"" + keyString + "\") %>"); Clipboard.SetText(codeSb.ToString()); } // Remember the text key for next time and close the wizard dialog window prevTextKey = textKey; TranslationText.Text = translationString; DialogResult = true; Close(); }