Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for translateculture.cs

(view source code of translateculture.cs as plain text)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Globalization;
  5.  
  6.  
  7. namespace RobvanderWoude
  8. {
  9. 	public class TranslateCulture
  10. 	{
  11. 		static string progver = "1.01";
  12.  
  13.  
  14. 		public static int Main( string[] args )
  15. 		{
  16. 			try
  17. 			{
  18. 				List<CultureInfo> cultures = new List<CultureInfo>( );
  19. 				//IComparer myComparer = new CultureNameComparer( );
  20.  
  21. 				#region Command Line Parsing
  22.  
  23. 				string format = "list";
  24. 				string output = "all";
  25. 				bool sort = false;
  26.  
  27. 				if ( args.Length > 0 )
  28. 				{
  29. 					foreach ( string arg in args )
  30. 					{
  31. 						switch ( arg.ToLower( ) )
  32. 						{
  33. 							case "/?":
  34. 								return ShowHelp( string.Empty );
  35. 							case "/c":
  36. 								if ( format != "list" ) { return ShowHelp( "Duplicate or conflicting command line switches." ); }
  37. 								format = "csv";
  38. 								break;
  39. 							case "/m":
  40. 								if ( output != "all" ) { return ShowHelp( "Duplicate or conflicting command line switches." ); }
  41. 								output = "monthnames";
  42. 								break;
  43. 							case "/s":
  44. 								if ( sort ) { return ShowHelp( "Duplicate command line switches." ); }
  45. 								sort = true;
  46. 								break;
  47. 							case "/w":
  48. 								if ( output != "all" ) { return ShowHelp( "Duplicate or conflicting command line switches." ); }
  49. 								output = "weekdays";
  50. 								break;
  51. 							default:
  52. 								CultureInfo cult = CultureInfo.GetCultureInfo( arg );
  53. 								if ( cultures.Contains( cult ) ) { return ShowHelp( "Duplicate cultures specified." ); }
  54. 								cultures.Add( cult );
  55. 								break;
  56. 						}
  57. 					}
  58. 				}
  59.  
  60. 				#endregion Command Line Parsing
  61.  
  62. 				if ( cultures.Count == 0 )
  63. 				{
  64. 					cultures.AddRange( CultureInfo.GetCultures( CultureTypes.AllCultures ) );
  65. 				}
  66. 				if ( sort )
  67. 				{
  68. 					CultureNameComparer comp = new CultureNameComparer( );
  69. 					cultures.Sort( comp );
  70. 				}
  71. 				foreach ( CultureInfo culture in cultures )
  72. 				{
  73. 					Console.WriteLine( ShowCultureInfo( culture, output, format ) );
  74. 				}
  75.  
  76. 				return 0;
  77. 			}
  78. 			//catch ( CultureNotFoundException e )
  79. 			//{
  80. 			//	Console.Error.WriteLine( "ERROR: {0}", e.Message );
  81. 			//	return 1;
  82. 			//}
  83. 			catch ( Exception e )
  84. 			{
  85. 				return ShowHelp( e.Message );
  86. 			}
  87. 		}
  88.  
  89.  
  90. 		// Custom CultureInfo.Name comparer, by Bas van der Woude
  91. 		public class CultureNameComparer : Comparer<CultureInfo>
  92. 		{
  93. 			public override int Compare( CultureInfo x, CultureInfo y )
  94. 			{
  95. 				if ( x == null ) { throw new ArgumentNullException( "x" ); }
  96. 				if ( y == null ) { throw new ArgumentNullException( "y" ); }
  97. 				return string.Compare( x.Name, y.Name, StringComparison.InvariantCultureIgnoreCase );
  98. 			}
  99. 		}
  100.  
  101.  
  102. 		// Format the requested info for the specified culture
  103. 		public static string ShowCultureInfo( CultureInfo culture, string display, string format )
  104. 		{
  105. 			string output = string.Empty;
  106. 			CultureInfo local = CultureInfo.CurrentCulture;
  107.  
  108. 			switch ( format )
  109. 			{
  110. 				case "csv":
  111. 					output = String.Format( culture, "\"{0}\",\"{1}\"", culture.Name, culture.DisplayName );
  112. 					break;
  113. 				case "list":
  114. 					output = String.Format( culture, "\n{0,-12}      {1}{2}{3}\n{4,-12}      {5}", local.Name, culture.Name, ( culture.Name.Length == 0 ? String.Empty : " :: " ), culture.DisplayName, new String( '=', local.Name.Length ), new String( '=', culture.Name.Length + culture.DisplayName.Length + ( culture.Name.Length == 0 ? 0 : 4 ) ) );
  115. 					break;
  116. 			}
  117. 			string[] days = culture.DateTimeFormat.DayNames;
  118. 			string[] localdays = local.DateTimeFormat.DayNames;
  119. 			string[] months = culture.DateTimeFormat.MonthNames;
  120. 			string[] localmonths = local.DateTimeFormat.MonthNames;
  121. 			if ( display != "monthnames" )
  122. 			{
  123. 				for ( int i = 0; i < 7; i++ )
  124. 				{
  125. 					if ( !string.IsNullOrEmpty( days[i] ) )
  126. 					{
  127. 						switch ( format )
  128. 						{
  129. 							case "csv":
  130. 								output += days[i] + "\",\"";
  131. 								break;
  132. 							case "list":
  133. 								output += String.Format( "\n{0,-12}  ->  {1}", localdays[i], days[i] );
  134. 								break;
  135. 						}
  136. 					}
  137. 				}
  138. 			}
  139. 			if ( display != "weekdays" )
  140. 			{
  141. 				for ( int i = 0; i < 12; i++ )
  142. 				{
  143. 					if ( !string.IsNullOrEmpty( months[i] ) )
  144. 					{
  145. 						switch ( format )
  146. 						{
  147. 							case "csv":
  148. 								output += months[i] + "\",\"";
  149. 								break;
  150. 							case "list":
  151. 								output += String.Format( "\n{0,-12}  ->  {1}", localmonths[i], months[i] );
  152. 								break;
  153. 						}
  154. 					}
  155. 				}
  156. 			}
  157. 			if ( display == "csv" ) { output = output.Substring( 0, output.Length - 2 ); }
  158. 			return output;
  159. 		}
  160.  
  161.  
  162. 		#region Error Handling
  163.  
  164. 		// Code to display help and optional error message, by Bas van der Woude
  165. 		public static int ShowHelp( Exception e )
  166. 		{
  167. 			return ShowHelp( e == null ? null : e.Message );
  168. 		}
  169.  
  170. 		public static int ShowHelp( string errorMessage )
  171. 		{
  172. 			string exeName = Process.GetCurrentProcess( ).ProcessName;
  173.  
  174. 			/*
  175. 			TranslateCulture,  Version 1.01
  176. 			List translations for weekdays and/or month names for the specified culture(s)
  177.  
  178. 			Usage:   TranslateCulture [culture [culture [...]]] [/M|/W] [/S] [/C]
  179.  
  180. 			Where:   culture   is a culture name, e.g. 'pt-BR' for Portugese (Brasil);
  181. 			                   multiple cultures allowed on a single command line; if
  182. 			                   no culture is specified, ALL known cultures are listed.
  183. 			         /C        formats output as CSV  (default: list)
  184. 			         /M        lists month names only (default: month names and weekdays)
  185. 			         /S        sorts by culture name  (default: list in command line order)
  186. 			         /W        lists weekdays only    (default: month names and weekdays)
  187.  
  188. 			Notes:   List format looks like 'Sunday -> domingo' (Sunday in local language).
  189. 			         The /M and /W switches are mutually exclusive.
  190.  
  191. 			Credits: Sort function optimized by Bas van der Woude.
  192.  
  193. 			Written by Rob van der Woude
  194. 			http://www.robvanderwoude.com
  195. 			*/
  196.  
  197. 			if ( string.IsNullOrEmpty( errorMessage ) == false )
  198. 			{
  199. 				Console.Error.WriteLine( );
  200. 				Console.ForegroundColor = ConsoleColor.Red;
  201. 				Console.Error.Write( "ERROR: " );
  202. 				Console.ForegroundColor = ConsoleColor.White;
  203. 				Console.Error.WriteLine( errorMessage );
  204. 				Console.ResetColor( );
  205. 			}
  206.  
  207. 			Console.Error.WriteLine( );
  208.  
  209. 			Console.Error.WriteLine( "{0},  Version {1}", exeName, progver );
  210.  
  211. 			Console.Error.WriteLine( "List translations for weekdays and/or month names for the specified culture(s)" );
  212.  
  213. 			Console.Error.WriteLine( );
  214.  
  215. 			Console.Error.Write( "Usage:   " );
  216. 			Console.ForegroundColor = ConsoleColor.White;
  217. 			Console.Error.WriteLine( "{0} [culture [culture [...]]] [/M|/W] [/S] [/C]", exeName );
  218. 			Console.ResetColor( );
  219.  
  220. 			Console.Error.WriteLine( );
  221.  
  222. 			Console.Error.Write( "Where:   " );
  223. 			Console.ForegroundColor = ConsoleColor.White;
  224. 			Console.Error.Write( "culture" );
  225. 			Console.ResetColor( );
  226. 			Console.Error.WriteLine( "   is a culture name, e.g. 'pt-BR' for Portugese (Brasil);" );
  227.  
  228. 			Console.Error.WriteLine( "                   multiple cultures allowed on a single command line; if" );
  229.  
  230. 			Console.Error.Write( "                   no culture is specified, " );
  231. 			Console.ForegroundColor = ConsoleColor.White;
  232. 			Console.Error.Write( "all" );
  233. 			Console.ResetColor( );
  234. 			Console.Error.WriteLine( " known cultures are listed." );
  235.  
  236. 			Console.ForegroundColor = ConsoleColor.White;
  237. 			Console.Error.Write( "         /C        " );
  238. 			Console.ResetColor( );
  239. 			Console.Error.Write( "formats output as " );
  240. 			Console.ForegroundColor = ConsoleColor.White;
  241. 			Console.Error.Write( "C" );
  242. 			Console.ResetColor( );
  243. 			Console.Error.WriteLine( "SV  (default: list)" );
  244.  
  245. 			Console.ForegroundColor = ConsoleColor.White;
  246. 			Console.Error.Write( "         /M        " );
  247. 			Console.ResetColor( );
  248. 			Console.Error.Write( "lists " );
  249. 			Console.ForegroundColor = ConsoleColor.White;
  250. 			Console.Error.Write( "M" );
  251. 			Console.ResetColor( );
  252. 			Console.Error.WriteLine( "onth names only (default: month names and weekdays)" );
  253.  
  254. 			Console.ForegroundColor = ConsoleColor.White;
  255. 			Console.Error.Write( "         /S        S" );
  256. 			Console.ResetColor( );
  257. 			Console.Error.WriteLine( "orts by culture name  (default: list in command line order)" );
  258.  
  259. 			Console.ForegroundColor = ConsoleColor.White;
  260. 			Console.Error.Write( "         /W        " );
  261. 			Console.ResetColor( );
  262. 			Console.Error.Write( "lists " );
  263. 			Console.ForegroundColor = ConsoleColor.White;
  264. 			Console.Error.Write( "W" );
  265. 			Console.ResetColor( );
  266. 			Console.Error.WriteLine( "eekdays only    (default: month names and weekdays)" );
  267.  
  268. 			Console.Error.WriteLine( );
  269.  
  270. 			Console.Error.Write( "Notes:   List format looks like '" );
  271. 			Console.ForegroundColor = ConsoleColor.White;
  272. 			Console.Error.Write( "Sunday -> domingo" );
  273. 			Console.ResetColor( );
  274. 			Console.Error.WriteLine( "' (Sunday in local language)." );
  275.  
  276. 			Console.Error.Write( "         The " );
  277. 			Console.ForegroundColor = ConsoleColor.White;
  278. 			Console.Error.Write( "/M" );
  279. 			Console.ResetColor( );
  280. 			Console.Error.Write( " and " );
  281. 			Console.ForegroundColor = ConsoleColor.White;
  282. 			Console.Error.Write( "/W" );
  283. 			Console.ResetColor( );
  284. 			Console.Error.WriteLine( " switches are mutually exclusive." );
  285.  
  286. 			Console.Error.WriteLine( );
  287.  
  288. 			Console.Error.WriteLine( "Credits: Sort function optimized by Bas van der Woude." );
  289.  
  290. 			Console.Error.WriteLine( );
  291.  
  292. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  293.  
  294. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  295.  
  296. 			return 1;
  297. 		}
  298.  
  299. 		#endregion Error Handling
  300. 	}
  301. }

page last modified: 2024-04-16; loaded in 0.0089 seconds