Powered by GeSHi

Source code for translateculture.cs

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

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