/// <summary> /// Get a localized string in the currently selected language /// </summary> /// <param name="componentName">The key value of the string phrase</param> /// <returns></returns> public static string GetTranslatedString(string componentName) { if (!TranslationsLoaded) { Logging.Error(LogOptions.MethodAndClassName, "Translations have not been loaded"); return(null); } string s; //check if componentName key exists in current language if (CurrentLanguage.ContainsKey(componentName)) { s = CurrentLanguage[componentName]; //if the value is TODO, check if we have it in english (unless it is english) if (s.Equals(TranslationNeeded)) { //Log warning it is todo in selected language Logging.WriteToLog(string.Format("Missing translation key={0}, value=TODO, language={1}", componentName, CurrentLanguageEnum.ToString()), Logfiles.Application, LogLevel.Error); s = English[componentName]; if (s.Equals(TranslationNeeded)) { //Log error it is todo in english Logging.WriteToLog(string.Format("Missing translation key={0}, value=TODO, language=English", componentName), Logfiles.Application, LogLevel.Error); s = componentName; } } } else { //check if key exists in english (should not be the case 99% of the time) if (English.ContainsKey(componentName)) { Logging.WriteToLog(string.Format("Missing translation: key={0}, value=TODO, language={1}", componentName, CurrentLanguageEnum.ToString()), Logfiles.Application, LogLevel.Error); s = English[componentName]; if (s.Equals(TranslationNeeded)) { //Log error it is todo in english Logging.WriteToLog(string.Format("Missing translation: key={0}, value=TODO, language=English", componentName), Logfiles.Application, LogLevel.Error); } } //Log error it does not exist Logging.WriteToLog(string.Format("Translation {0} does not exist in any languages", componentName), Logfiles.Application, LogLevel.Error); s = componentName; } return(s); }
//https://stackoverflow.com/questions/793100/globally-catch-exceptions-in-a-wpf-application private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { if (!exceptionShown) { exceptionShown = true; if (!Logging.IsLogDisposed(Logfiles.Application) && Logging.IsLogOpen(Logfiles.Application)) { Logging.WriteToLog(e.Exception.ToString(), Logfiles.Application, LogLevel.ApplicationHalt); } exceptionCaptureDisplay.ExceptionText = e.Exception.ToString(); exceptionCaptureDisplay.ShowDialog(); CloseApplicationLog(true); } }
private static void TranslateComponent(FrameworkElement frameworkElement, bool applyToolTips) { //check if component name is valid string string componentName = frameworkElement.Name; if (string.IsNullOrWhiteSpace(componentName)) { //Logging.WriteToLog("Translation component name is blank", Logfiles.Application, LogLevel.Debug); return; } //first check name is none or on blacklist if (TranslationComponentBlacklist.Contains(componentName)) { Logging.WriteToLog(string.Format("Skipping translation of {0}, present in blacklist and consider=true", componentName), Logfiles.Application, LogLevel.Debug); return; } //getting here means that the object is a framework UI element, has a name, and is not on te blacklist. it's safe to translate //use the "is" keyword to be able to apply translations (text is under different properties for each type of visuals) if (frameworkElement is Control control) { //Generic control //headered content controls have a header and content object if (control is HeaderedContentControl headeredContentControl) { //ALWAYS make sure that the header and content are of type string BEFORE over-writing! (what if it is an image?) if (headeredContentControl.Header is string) { headeredContentControl.Header = GetTranslatedString(headeredContentControl.Name + "Header"); } if (headeredContentControl.Content is string) { headeredContentControl.Content = GetTranslatedString(headeredContentControl.Name); } if (applyToolTips) { if (ExistsInCurrentLanguage(headeredContentControl.Name + "Description")) { headeredContentControl.ToolTip = GetTranslatedString(headeredContentControl.Name + "Description"); } } } //RelhaxHyperlink has text stored at the child textbox else if (control is UI.RelhaxHyperlink link) { link.Text = GetTranslatedString(componentName); if (applyToolTips) { if (ExistsInCurrentLanguage(componentName + "Description")) { link.ToolTip = GetTranslatedString(componentName + "Description"); } } } //content controls have only a heder //NOTE: button is this type else if (control is ContentControl contentControl) { //ALWAYS make sure that the header and content are of type string BEFORE over-writing! (what if it is an image?) if (contentControl.Content is string) { contentControl.Content = GetTranslatedString(contentControl.Name); } if (applyToolTips) { if (ExistsInCurrentLanguage(contentControl.Name + "Description")) { contentControl.ToolTip = GetTranslatedString(contentControl.Name + "Description"); } } } //textbox only has string text as input else if (control is TextBox textBox) { textBox.Text = GetTranslatedString(textBox.Name); if (applyToolTips) { if (ExistsInCurrentLanguage(textBox.Name + "Description")) { textBox.ToolTip = GetTranslatedString(textBox.Name + "Description"); } } } } else if (frameworkElement is TextBlock textBlock) { //lightweight block of text that only uses string as it's input. makes it not a control (no content of children property) textBlock.Text = GetTranslatedString(textBlock.Name); //apply tool tips? if (applyToolTips) { if (ExistsInCurrentLanguage(textBlock.Name + "Description")) { textBlock.ToolTip = GetTranslatedString(textBlock.Name + "Description"); } } } }