Localization.cs 8.5 KB

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