Click here to Skip to main content
15,896,111 members
Articles / Programming Languages / C#

pseudoLocalizer -- a tool to aid development and testing of internationalized applications

Rate me:
Please Sign up or sign in to vote.
4.50/5 (7 votes)
7 Oct 20047 min read 49.7K   1.8K   23  
An explanation of why you want to pseudo-localize, and a pair of tools for 'translating' into a useful pseudolocal language.
/*
File: pLoc.cs
Version:  1.0

Author:
  Anne Gunn (ompeag at wyoming dot com)
  
Copyright:
  Copyright (c) 2004, Anne Gunn.
  
License:
  This module is distributed under the MIT License.
  http://www.opensource.org/licenses/mit-license.php
  
Notes:
  This module is a commandline wrapper for the pseudolocalization enginen.
  See pLocalize.cs for more info.

*/

using System;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;

namespace pseudoLocalizer
{
	/// <summary>
	/// pLoc -- console application wrapper for pseudoLocalization engine
	/// </summary>
	class pLoc
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		public static void Main(string[] argv)
		  {
		  bool bPrintExplanation = false;
      //Console.WriteLine ("argv length = {0}", argv.Length);
      ArrayList inputList = new ArrayList ();
		  
      TextWriter writer = Console.Out;
      string outFileName = "";
	  
      // we need some arguments to do any work
  		if (argv.Length < 1)
  		  {
  		  Console.WriteLine ("*** Error -- you must pass at least one argument on the commandline");
  		  bPrintExplanation = true;
  		  }
  		else
  		  {
  		  // first argument could be something special
  		  string firstArg = argv[0];
  		  //Console.WriteLine ("first arg = {0}", firstArg);
  		  // a cry for help?
  		  Regex helpRegx = new Regex ("^[/|-][?|H|h]");
  		  if (helpRegx.IsMatch (firstArg))
  		    {
  		    bPrintExplanation = true;
  		    }
  		  else
  		    {
  		    //Console.WriteLine ("first arg is not a request for help");
  		    // or a request to get our input from file?
          Regex fileRegx = new Regex ("^[/|-][F|f]");
          if (fileRegx.IsMatch (firstArg))
            {
            if (argv.Length > 1)
              {
              try 
                {
                // see if we can get the input
                string inFileName = argv[1];
                StreamReader reader = new StreamReader (inFileName);
                string line;
                while ((line = reader.ReadLine ()) != null)
                  {
                  inputList.Add (line);
                  }
                // and, if we can get the input, we set up a non-console output file
                // output file name is <name of input>_ploc.<ext of input>
                Regex nameRegex = new Regex (@"(.*)\.(.*?)");
                outFileName = nameRegex.Replace (inFileName,@"$1_ploc.$2");
                try
                  {
                  writer = new StreamWriter (outFileName);
                  }
                catch (Exception e)
                  {
                  Console.WriteLine ("*** Error -- couldn't open output file {0}", outFileName);
                  Console.WriteLine (e.Message);
                  // we fall back to console output -- not much else we can do
                  outFileName = "";
                  }
                }
              catch (Exception e)
                {
                Console.WriteLine ("*** Error -- couldn't open or read from file {0}", argv[1]);
                Console.WriteLine (e.Message);
                }
              
              }
            else
              {
              Console.WriteLine ("*** Error -- file option requires a filename");
              bPrintExplanation = true;
              }
            }
          else
            {
            // arguments are words or phrases to be translated
            foreach (string s in argv)
              {
              inputList.Add (s);
              }
            }
          }
        }

      if (inputList.Count > 0)
        {
        string pLocText;
        pLocalizeEngine pLocEngine = new pLocalizeEngine ();
        using (writer)
          {
          foreach (string text in inputList)
            {
            pLocText = pLocEngine.Localize (text);
            writer.WriteLine (pLocText);
            }
          }
        if (outFileName != "")
          {
          Console.WriteLine ("output is in file:  {0}", outFileName);
          }
        } 		  
  		if (bPrintExplanation)
  		  {
  		  ExplainUsage ();
  		  }
		  }
		
    static void ExplainUsage ( )
      {
      Console.WriteLine ("");
      Console.WriteLine ("Syntax is:  ");
      Console.WriteLine ("  pLoc [word(s)] | [phrase(s) -- wrapped in double quotes] | [-f <infile>]");
      Console.WriteLine ("");
      Console.WriteLine ("  Words and phrases may be intermingled.");
      Console.WriteLine ("  If used, the infile option must be by itself on the commandline.");
      Console.WriteLine ("");
      Console.WriteLine ("Examples:");
      Console.WriteLine ("  pLoc these four words for me");
      Console.WriteLine ("  pLoc \"this phrase for me\"");
      Console.WriteLine ("  pLoc word1 \"a different phrase\" word2");
      Console.WriteLine ("  pLoc -f lines.txt");
      Console.WriteLine ("    (output will go to lines_ploc.txt");
      Console.WriteLine ("     wildcards and multiple files-at-a-time are not supported)");
      Console.WriteLine ("");
      }
	}  // class pLoc
}  // namespace pseudoLocalizer

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions