Localization.cs 8.2 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 (
  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. Width = Dim.Fill (2),
  92. Text = "Please select a language."
  93. };
  94. Win.Add (selectLanguageLabel);
  95. _languageComboBox = new()
  96. {
  97. X = 2,
  98. Y = Pos.Bottom (selectLanguageLabel) + 1,
  99. Width = _cultureInfoNameSource.Select (cn => cn.Length + 3).Max (),
  100. Height = _cultureInfoNameSource.Length + 1,
  101. HideDropdownListOnClick = true,
  102. Source = new ListWrapper<string> (new (_cultureInfoNameSource)),
  103. SelectedItem = _cultureInfoNameSource.Length - 1
  104. };
  105. _languageComboBox.SetSource<string> (new (_cultureInfoNameSource));
  106. _languageComboBox.SelectedItemChanged += LanguageComboBox_SelectChanged;
  107. Win.Add (_languageComboBox);
  108. var textAndFileDialogLabel = new Label
  109. {
  110. X = 2,
  111. Y = Pos.Top (_languageComboBox) + 3,
  112. Width = Dim.Fill (2),
  113. Height = 1,
  114. Text =
  115. "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."
  116. };
  117. Win.Add (textAndFileDialogLabel);
  118. var textField = new TextView
  119. {
  120. X = 2, Y = Pos.Bottom (textAndFileDialogLabel) + 1, Width = Dim.Fill (32), Height = 1
  121. };
  122. Win.Add (textField);
  123. _allowAnyCheckBox = new()
  124. {
  125. X = Pos.Right (textField) + 1,
  126. Y = Pos.Bottom (textAndFileDialogLabel) + 1,
  127. Checked = false,
  128. Text = "Allow any"
  129. };
  130. Win.Add (_allowAnyCheckBox);
  131. var openDialogButton = new Button
  132. {
  133. X = Pos.Right (_allowAnyCheckBox) + 1, Y = Pos.Bottom (textAndFileDialogLabel) + 1, Text = "Open"
  134. };
  135. openDialogButton.Accept += (sender, e) => ShowFileDialog (false);
  136. Win.Add (openDialogButton);
  137. var saveDialogButton = new Button
  138. {
  139. X = Pos.Right (openDialogButton) + 1, Y = Pos.Bottom (textAndFileDialogLabel) + 1, Text = "Save"
  140. };
  141. saveDialogButton.Accept += (sender, e) => ShowFileDialog (true);
  142. Win.Add (saveDialogButton);
  143. var wizardLabel = new Label
  144. {
  145. X = 2,
  146. Y = Pos.Bottom (textField) + 1,
  147. Width = Dim.Fill (2),
  148. Text = "Click the button to open a wizard."
  149. };
  150. Win.Add (wizardLabel);
  151. var wizardButton = new Button { X = 2, Y = Pos.Bottom (wizardLabel) + 1, Text = "Open _wizard" };
  152. wizardButton.Accept += (sender, e) => ShowWizard ();
  153. Win.Add (wizardButton);
  154. Win.Unloaded += (sender, e) => Quit ();
  155. }
  156. public void ShowFileDialog (bool isSaveFile)
  157. {
  158. FileDialog dialog = isSaveFile ? new SaveDialog () : new OpenDialog { OpenMode = _currentOpenMode };
  159. dialog.AllowedTypes =
  160. [
  161. _allowAnyCheckBox.Checked ?? false
  162. ? new AllowedTypeAny ()
  163. : new AllowedType ("Dynamic link library", ".dll"),
  164. new AllowedType ("Json", ".json"),
  165. new AllowedType ("Text", ".txt"),
  166. new AllowedType ("Yaml", ".yml", ".yaml")
  167. ];
  168. dialog.MustExist = !isSaveFile;
  169. dialog.AllowsMultipleSelection = !isSaveFile;
  170. _currentOpenMode++;
  171. if (_currentOpenMode > OpenMode.Mixed)
  172. {
  173. _currentOpenMode = OpenMode.File;
  174. }
  175. Application.Run (dialog);
  176. dialog.Dispose ();
  177. }
  178. public void ShowWizard ()
  179. {
  180. var wizard = new Wizard { Height = 8, Width = 36, Title = "The wizard" };
  181. wizard.AddStep (new() { HelpText = "Wizard first step" });
  182. wizard.AddStep (new() { HelpText = "Wizard step 2", NextButtonText = ">>> (_N)" });
  183. wizard.AddStep (new() { HelpText = "Wizard last step" });
  184. Application.Run (wizard);
  185. wizard.Dispose ();
  186. }
  187. private void LanguageComboBox_SelectChanged (object sender, ListViewItemEventArgs e)
  188. {
  189. if (e.Value is string cultureName)
  190. {
  191. int index = Array.IndexOf (_cultureInfoNameSource, cultureName);
  192. if (index >= 0)
  193. {
  194. SetCulture (_cultureInfoSource [index]);
  195. }
  196. }
  197. }
  198. }