Localization.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Threading;
  6. using Terminal.Gui;
  7. namespace UICatalog.Scenarios {
  8. [ScenarioMetadata (Name: "Localization", Description: "Test for localization resources.")]
  9. [ScenarioCategory ("Text and Formatting")]
  10. [ScenarioCategory ("Tests")]
  11. public class Localization : Scenario {
  12. public CultureInfo CurrentCulture { get; private set; } = Thread.CurrentThread.CurrentUICulture;
  13. private CultureInfo [] _cultureInfoSource;
  14. private string [] _cultureInfoNameSource;
  15. private ComboBox _languageComboBox;
  16. private CheckBox _allowAnyCheckBox;
  17. private OpenMode _currentOpenMode = OpenMode.File;
  18. public override void Setup ()
  19. {
  20. base.Setup ();
  21. _cultureInfoSource = Application.SupportedCultures.Append (CultureInfo.InvariantCulture).ToArray ();
  22. _cultureInfoNameSource = Application.SupportedCultures.Select (c => $"{c.NativeName} ({c.Name})").Append ("Invariant").ToArray ();
  23. var languageMenus = Application.SupportedCultures
  24. .Select (c => new MenuItem ($"{c.NativeName} ({c.Name})", "", () => SetCulture (c)))
  25. .Concat (
  26. new MenuItem [] {
  27. null,
  28. new MenuItem ("Invariant", "", () => SetCulture (CultureInfo.InvariantCulture))
  29. }
  30. )
  31. .ToArray ();
  32. var menu = new MenuBar (new MenuBarItem [] {
  33. new MenuBarItem ("_File", new MenuItem [] {
  34. new MenuBarItem("_Language", languageMenus),
  35. null,
  36. new MenuItem ("_Quit", "", Quit),
  37. }),
  38. });
  39. Application.Top.Add (menu);
  40. var selectLanguageLabel = new Label ("Please select a language.") {
  41. X = 2,
  42. Y = 1,
  43. Width = Dim.Fill (2),
  44. AutoSize = true
  45. };
  46. Win.Add (selectLanguageLabel);
  47. _languageComboBox = new ComboBox (_cultureInfoNameSource) {
  48. X = 2,
  49. Y = Pos.Bottom (selectLanguageLabel) + 1,
  50. Width = _cultureInfoNameSource.Select (cn => cn.Length + 3).Max (),
  51. Height = _cultureInfoNameSource.Length + 1,
  52. HideDropdownListOnClick = true,
  53. AutoSize = true,
  54. SelectedItem = _cultureInfoNameSource.Length - 1
  55. };
  56. _languageComboBox.SelectedItemChanged += LanguageComboBox_SelectChanged;
  57. Win.Add (_languageComboBox);
  58. var textAndFileDialogLabel = new Label ("Right click on the text field to open a context menu, click the button to open a file dialog.\r\nOpen mode will loop through 'File', 'Directory' and 'Mixed' as 'Open' or 'Save' button clicked.") {
  59. X = 2,
  60. Y = Pos.Top (_languageComboBox) + 3,
  61. Width = Dim.Fill (2),
  62. AutoSize = true
  63. };
  64. Win.Add (textAndFileDialogLabel);
  65. var textField = new TextView {
  66. X = 2,
  67. Y = Pos.Bottom (textAndFileDialogLabel) + 1,
  68. Width = Dim.Fill (32),
  69. Height = 1
  70. };
  71. Win.Add (textField);
  72. _allowAnyCheckBox = new CheckBox {
  73. X = Pos.Right (textField) + 1,
  74. Y = Pos.Bottom (textAndFileDialogLabel) + 1,
  75. Checked = false,
  76. Text = "Allow any"
  77. };
  78. Win.Add (_allowAnyCheckBox);
  79. var openDialogButton = new Button ("Open") {
  80. X = Pos.Right (_allowAnyCheckBox) + 1,
  81. Y = Pos.Bottom (textAndFileDialogLabel) + 1
  82. };
  83. openDialogButton.Clicked += (sender, e) => ShowFileDialog (false);
  84. Win.Add (openDialogButton);
  85. var saveDialogButton = new Button ("Save") {
  86. X = Pos.Right (openDialogButton) + 1,
  87. Y = Pos.Bottom (textAndFileDialogLabel) + 1
  88. };
  89. saveDialogButton.Clicked += (sender, e) => ShowFileDialog (true);
  90. Win.Add (saveDialogButton);
  91. var wizardLabel = new Label ("Click the button to open a wizard.") {
  92. X = 2,
  93. Y = Pos.Bottom (textField) + 1,
  94. Width = Dim.Fill (2),
  95. AutoSize = true
  96. };
  97. Win.Add (wizardLabel);
  98. var wizardButton = new Button ("Open _wizard") {
  99. X = 2,
  100. Y = Pos.Bottom (wizardLabel) + 1
  101. };
  102. wizardButton.Clicked += (sender, e) => ShowWizard ();
  103. Win.Add (wizardButton);
  104. Win.Unloaded += (sender, e) => Quit ();
  105. }
  106. public void SetCulture (CultureInfo culture)
  107. {
  108. if (_cultureInfoSource [_languageComboBox.SelectedItem] != culture) {
  109. _languageComboBox.SelectedItem = Array.IndexOf (_cultureInfoSource, culture);
  110. }
  111. if (this.CurrentCulture == culture) return;
  112. this.CurrentCulture = culture;
  113. Thread.CurrentThread.CurrentUICulture = culture;
  114. Application.Refresh ();
  115. }
  116. private void LanguageComboBox_SelectChanged (object sender, ListViewItemEventArgs e)
  117. {
  118. if (e.Value is string cultureName) {
  119. var index = Array.IndexOf (_cultureInfoNameSource, cultureName);
  120. if (index >= 0) {
  121. SetCulture (_cultureInfoSource [index]);
  122. }
  123. }
  124. }
  125. public void ShowFileDialog (bool isSaveFile)
  126. {
  127. FileDialog dialog = isSaveFile ? new SaveDialog () : new OpenDialog ("", null, _currentOpenMode);
  128. dialog.AllowedTypes = new List<IAllowedType> () {
  129. (_allowAnyCheckBox.Checked ?? false) ? new AllowedTypeAny() : new AllowedType("Dynamic link library", ".dll"),
  130. new AllowedType("Json", ".json"),
  131. new AllowedType("Text", ".txt"),
  132. new AllowedType("Yaml", ".yml", ".yaml")
  133. };
  134. dialog.MustExist = !isSaveFile;
  135. dialog.AllowsMultipleSelection = !isSaveFile;
  136. _currentOpenMode++;
  137. if (_currentOpenMode > OpenMode.Mixed) {
  138. _currentOpenMode = OpenMode.File;
  139. }
  140. Application.Run (dialog);
  141. }
  142. public void ShowWizard ()
  143. {
  144. Wizard wizard = new Wizard {
  145. Height = 8,
  146. Width = 36,
  147. Title = "The wizard"
  148. };
  149. wizard.AddStep (new WizardStep () {
  150. HelpText = "Wizard first step",
  151. });
  152. wizard.AddStep (new WizardStep () {
  153. HelpText = "Wizard step 2",
  154. NextButtonText = ">>> (_N)"
  155. });
  156. wizard.AddStep (new WizardStep () {
  157. HelpText = "Wizard last step"
  158. });
  159. Application.Run (wizard);
  160. }
  161. public void Quit ()
  162. {
  163. SetCulture (CultureInfo.InvariantCulture);
  164. Application.RequestStop ();
  165. }
  166. }
  167. }