Localization.cs 8.5 KB

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