(view source code of translateculture.cs as plain text)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
namespace RobvanderWoude
{
public class TranslateCulture
{
static string progver = "1.01";
public static int Main( string[] args )
{
try
{
List<CultureInfo> cultures = new List<CultureInfo>( );
//IComparer myComparer = new CultureNameComparer( );
#region Command Line Parsing
string format = "list";
string output = "all";
bool sort = false;
if ( args.Length > 0 )
{
foreach ( string arg in args )
{
switch ( arg.ToLower( ) )
{
case "/?":
return ShowHelp( string.Empty );
case "/c":
if ( format != "list" ) { return ShowHelp( "Duplicate or conflicting command line switches." ); }
format = "csv";
break;
case "/m":
if ( output != "all" ) { return ShowHelp( "Duplicate or conflicting command line switches." ); }
output = "monthnames";
break;
case "/s":
if ( sort ) { return ShowHelp( "Duplicate command line switches." ); }
sort = true;
break;
case "/w":
if ( output != "all" ) { return ShowHelp( "Duplicate or conflicting command line switches." ); }
output = "weekdays";
break;
default:
CultureInfo cult = CultureInfo.GetCultureInfo( arg );
if ( cultures.Contains( cult ) ) { return ShowHelp( "Duplicate cultures specified." ); }
cultures.Add( cult );
break;
}
}
}
#endregion Command Line Parsing
if ( cultures.Count == 0 )
{
cultures.AddRange( CultureInfo.GetCultures( CultureTypes.AllCultures ) );
}
if ( sort )
{
CultureNameComparer comp = new CultureNameComparer( );
cultures.Sort( comp );
}
foreach ( CultureInfo culture in cultures )
{
Console.WriteLine( ShowCultureInfo( culture, output, format ) );
}
return 0;
}
//catch ( CultureNotFoundException e )
//{
// Console.Error.WriteLine( "ERROR: {0}", e.Message );
// return 1;
//}
catch ( Exception e )
{
return ShowHelp( e.Message );
}
}
// Custom CultureInfo.Name comparer, by Bas van der Woude
public class CultureNameComparer : Comparer<CultureInfo>
{
public override int Compare( CultureInfo x, CultureInfo y )
{
if ( x == null ) { throw new ArgumentNullException( "x" ); }
if ( y == null ) { throw new ArgumentNullException( "y" ); }
return string.Compare( x.Name, y.Name, StringComparison.InvariantCultureIgnoreCase );
}
}
// Format the requested info for the specified culture
public static string ShowCultureInfo( CultureInfo culture, string display, string format )
{
string output = string.Empty;
CultureInfo local = CultureInfo.CurrentCulture;
switch ( format )
{
case "csv":
output = String.Format( culture, "\"{0}\",\"{1}\"", culture.Name, culture.DisplayName );
break;
case "list":
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 ) ) );
break;
}
string[] days = culture.DateTimeFormat.DayNames;
string[] localdays = local.DateTimeFormat.DayNames;
string[] months = culture.DateTimeFormat.MonthNames;
string[] localmonths = local.DateTimeFormat.MonthNames;
if ( display != "monthnames" )
{
for ( int i = 0; i < 7; i++ )
{
if ( !string.IsNullOrEmpty( days[i] ) )
{
switch ( format )
{
case "csv":
output += days[i] + "\",\"";
break;
case "list":
output += String.Format( "\n{0,-12} -> {1}", localdays[i], days[i] );
break;
}
}
}
}
if ( display != "weekdays" )
{
for ( int i = 0; i < 12; i++ )
{
if ( !string.IsNullOrEmpty( months[i] ) )
{
switch ( format )
{
case "csv":
output += months[i] + "\",\"";
break;
case "list":
output += String.Format( "\n{0,-12} -> {1}", localmonths[i], months[i] );
break;
}
}
}
}
if ( display == "csv" ) { output = output.Substring( 0, output.Length - 2 ); }
return output;
}
#region Error Handling
// Code to display help and optional error message, by Bas van der Woude
public static int ShowHelp( Exception e )
{
return ShowHelp( e == null ? null : e.Message );
}
public static int ShowHelp( string errorMessage )
{
string exeName = Process.GetCurrentProcess( ).ProcessName;
/*
TranslateCulture, Version 1.01
List translations for weekdays and/or month names for the specified culture(s)
Usage: TranslateCulture [culture [culture [...]]] [/M|/W] [/S] [/C]
Where: culture is a culture name, e.g. 'pt-BR' for Portugese (Brasil);
multiple cultures allowed on a single command line; if
no culture is specified, ALL known cultures are listed.
/C formats output as CSV (default: list)
/M lists month names only (default: month names and weekdays)
/S sorts by culture name (default: list in command line order)
/W lists weekdays only (default: month names and weekdays)
Notes: List format looks like 'Sunday -> domingo' (Sunday in local language).
The /M and /W switches are mutually exclusive.
Credits: Sort function optimized by Bas van der Woude.
Written by Rob van der Woude
http://www.robvanderwoude.com
*/
if ( string.IsNullOrEmpty( errorMessage ) == false )
{
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write( "ERROR: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( errorMessage );
Console.ResetColor( );
}
Console.Error.WriteLine( );
Console.Error.WriteLine( "{0}, Version {1}", exeName, progver );
Console.Error.WriteLine( "List translations for weekdays and/or month names for the specified culture(s)" );
Console.Error.WriteLine( );
Console.Error.Write( "Usage: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "{0} [culture [culture [...]]] [/M|/W] [/S] [/C]", exeName );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.Write( "Where: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "culture" );
Console.ResetColor( );
Console.Error.WriteLine( " is a culture name, e.g. 'pt-BR' for Portugese (Brasil);" );
Console.Error.WriteLine( " multiple cultures allowed on a single command line; if" );
Console.Error.Write( " no culture is specified, " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "all" );
Console.ResetColor( );
Console.Error.WriteLine( " known cultures are listed." );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /C " );
Console.ResetColor( );
Console.Error.Write( "formats output as " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "C" );
Console.ResetColor( );
Console.Error.WriteLine( "SV (default: list)" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /M " );
Console.ResetColor( );
Console.Error.Write( "lists " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "M" );
Console.ResetColor( );
Console.Error.WriteLine( "onth names only (default: month names and weekdays)" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /S S" );
Console.ResetColor( );
Console.Error.WriteLine( "orts by culture name (default: list in command line order)" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /W " );
Console.ResetColor( );
Console.Error.Write( "lists " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "W" );
Console.ResetColor( );
Console.Error.WriteLine( "eekdays only (default: month names and weekdays)" );
Console.Error.WriteLine( );
Console.Error.Write( "Notes: List format looks like '" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "Sunday -> domingo" );
Console.ResetColor( );
Console.Error.WriteLine( "' (Sunday in local language)." );
Console.Error.Write( " The " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/M" );
Console.ResetColor( );
Console.Error.Write( " and " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/W" );
Console.ResetColor( );
Console.Error.WriteLine( " switches are mutually exclusive." );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Credits: Sort function optimized by Bas van der Woude." );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Written by Rob van der Woude" );
Console.Error.WriteLine( "http://www.robvanderwoude.com" );
return 1;
}
#endregion Error Handling
}
}
page last modified: 2024-04-16; loaded in 0.0102 seconds