Localization.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 Main ()
  38. {
  39. Application.Init ();
  40. var top = new Toplevel ();
  41. var win = new Window { Title = GetQuitKeyAndName () };
  42. _cultureInfoSource = Application.SupportedCultures.Append (CultureInfo.InvariantCulture).ToArray ();
  43. _cultureInfoNameSource = Application.SupportedCultures.Select (c => $"{c.NativeName} ({c.Name})")
  44. .Append ("Invariant")
  45. .ToArray ();
  46. MenuItem [] languageMenus = Application.SupportedCultures
  47. .Select (
  48. c => new MenuItem (
  49. $"{c.NativeName} ({c.Name})",
  50. "",
  51. () => SetCulture (c)
  52. )
  53. )
  54. .Concat (
  55. new MenuItem []
  56. {
  57. null,
  58. new (
  59. "Invariant",
  60. "",
  61. () =>
  62. SetCulture (
  63. CultureInfo
  64. .InvariantCulture
  65. )
  66. )
  67. }
  68. )
  69. .ToArray ();
  70. var menu = new MenuBar
  71. {
  72. Menus =
  73. [
  74. new (
  75. "_File",
  76. new MenuItem []
  77. {
  78. new MenuBarItem (
  79. "_Language",
  80. languageMenus
  81. ),
  82. null,
  83. new ("_Quit", "", Quit)
  84. }
  85. )
  86. ]
  87. };
  88. top.Add (menu);
  89. var selectLanguageLabel = new Label
  90. {
  91. X = 2,
  92. Y = 1,
  93. Width = Dim.Fill (2),
  94. Text = "Please select a language."
  95. };
  96. win.Add (selectLanguageLabel);
  97. _languageComboBox = new()
  98. {
  99. X = 2,
  100. Y = Pos.Bottom (selectLanguageLabel) + 1,
  101. Width = _cultureInfoNameSource.Select (cn => cn.Length + 3).Max (),
  102. Height = _cultureInfoNameSource.Length + 1,
  103. HideDropdownListOnClick = true,
  104. Source = new ListWrapper<string> (new (_cultureInfoNameSource)),
  105. SelectedItem = _cultureInfoNameSource.Length - 1
  106. };
  107. _languageComboBox.SetSource<string> (new (_cultureInfoNameSource));
  108. _languageComboBox.SelectedItemChanged += LanguageComboBox_SelectChanged;
  109. win.Add (_languageComboBox);
  110. var textAndFileDialogLabel = new Label
  111. {
  112. X = 2,
  113. Y = Pos.Top (_languageComboBox) + 3,
  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()
  126. {
  127. X = Pos.Right (textField) + 1,
  128. Y = Pos.Bottom (textAndFileDialogLabel) + 1,
  129. CheckedState = CheckState.UnChecked,
  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.Accepting += (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.Accepting += (sender, e) => ShowFileDialog (true);
  144. win.Add (saveDialogButton);
  145. var wizardLabel = new Label
  146. {
  147. X = 2,
  148. Y = Pos.Bottom (textField) + 1,
  149. Width = Dim.Fill (2),
  150. Text = "Click the button to open a wizard."
  151. };
  152. win.Add (wizardLabel);
  153. var wizardButton = new Button { X = 2, Y = Pos.Bottom (wizardLabel) + 1, Text = "Open _wizard" };
  154. wizardButton.Accepting += (sender, e) => ShowWizard ();
  155. win.Add (wizardButton);
  156. win.Unloaded += (sender, e) => Quit ();
  157. win.Y = Pos.Bottom (menu);
  158. top.Add (win);
  159. Application.Run (top);
  160. top.Dispose ();
  161. Application.Shutdown ();
  162. }
  163. public void ShowFileDialog (bool isSaveFile)
  164. {
  165. FileDialog dialog = isSaveFile ? new SaveDialog () : new OpenDialog { OpenMode = _currentOpenMode };
  166. dialog.AllowedTypes =
  167. [
  168. _allowAnyCheckBox.CheckedState == CheckState.Checked
  169. ? new AllowedTypeAny ()
  170. : new AllowedType ("Dynamic link library", ".dll"),
  171. new AllowedType ("Json", ".json"),
  172. new AllowedType ("Text", ".txt"),
  173. new AllowedType ("Yaml", ".yml", ".yaml")
  174. ];
  175. dialog.MustExist = !isSaveFile;
  176. dialog.AllowsMultipleSelection = !isSaveFile;
  177. _currentOpenMode++;
  178. if (_currentOpenMode > OpenMode.Mixed)
  179. {
  180. _currentOpenMode = OpenMode.File;
  181. }
  182. Application.Run (dialog);
  183. dialog.Dispose ();
  184. }
  185. public void ShowWizard ()
  186. {
  187. var wizard = new Wizard { Height = 8, Width = 36, Title = "The wizard" };
  188. wizard.AddStep (new() { HelpText = "Wizard first step" });
  189. wizard.AddStep (new() { HelpText = "Wizard step 2", NextButtonText = ">>> (_N)" });
  190. wizard.AddStep (new() { HelpText = "Wizard last step" });
  191. Application.Run (wizard);
  192. wizard.Dispose ();
  193. }
  194. private void LanguageComboBox_SelectChanged (object sender, ListViewItemEventArgs e)
  195. {
  196. if (e.Value is string cultureName)
  197. {
  198. int index = Array.IndexOf (_cultureInfoNameSource, cultureName);
  199. if (index >= 0)
  200. {
  201. SetCulture (_cultureInfoSource [index]);
  202. }
  203. }
  204. }
  205. }