Localization.cs 8.4 KB

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