/// <summary> /// Applies localization using the specified language code to all form controls, /// the main menu and the form itself. /// </summary> /// <param name="form"></param> /// <param name="menu">The main form menu.</param> /// <param name="languageCode">The language code to use the localized strings for.</param> /// <returns></returns> public static bool Localize(Form form, MainMenu menu, string languageCode) { try { SortedDictionary <string, CultureInfo> availableLanguages = GetLanguages(); if (availableLanguages == null || availableLanguages.Count == 0) { throw new UnableToLocalizeException(); } CultureInfo culture; try { culture = new CultureInfo(languageCode); } catch { culture = new CultureInfo(defaultLanguage); } if (culture.TwoLetterISOLanguageName != currentCulture.TwoLetterISOLanguageName) { localizedStrings.Clear(); if (!availableLanguages.ContainsKey(culture.TwoLetterISOLanguageName)) { culture = new CultureInfo(defaultLanguage); } } if (localizedStrings.Count == 0) { if (!LoadStrings(availableLanguages[culture.TwoLetterISOLanguageName].Name)) { throw new UnableToLocalizeException(); } } currentCulture = availableLanguages[culture.TwoLetterISOLanguageName]; isRtlCulture = currentCulture.TextInfo.IsRightToLeft; string formPrefix = form.Name + "."; string localizedText = String.Empty; if (!isRtlCulture) { form.RightToLeft = RightToLeft.No; } else { form.RightToLeft = RightToLeft.Yes; } if (localizedStrings.TryGetValue(formPrefix + "Text", out localizedText)) { form.Text = localizedText; } foreach (Control control in form.Controls) { LocalizeControl(control, formPrefix); } if (menu != null) { if (!isRtlCulture) { menu.RightToLeft = RightToLeft.No; } else { menu.RightToLeft = RightToLeft.Yes; } foreach (MenuItem menuItem in menu.MenuItems) { LocalizeMenu(menuItem, formPrefix); } } if (form.RightToLeftLayout != isRtlCulture) { form.RightToLeftLayout = isRtlCulture; } Thread.CurrentThread.CurrentUICulture = currentCulture; Thread.CurrentThread.CurrentCulture = currentCulture; return(true); } catch (FileNotFoundException ex) { string text = "Can't translate the program because the language file doesn't exists." + "\n\nExtract the \"" + Path.GetFileName(localeFile) + "\" file from the program archive."; ExceptionHandler.LogException(ex); ExceptionHandler.ShowMessage(text); return(false); } catch (UnableToLocalizeException ex) { ExceptionHandler.LogException(ex); return(false); } catch (Exception ex) { ExceptionHandler.LogException(ex); return(false); } }