Click here to Skip to main content
Click here to Skip to main content

Project Localizer

, 13 May 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
A tool for localizing source files and generating helper classes in any programming language
Screenshot - ProjectLocalizer_1.gif

Introduction

Project Localizer is a tool to localize any kind of source file. What does it mean, exactly? The tool does two things:

  1. Replaces all strings in a source file with a specified expression, e.g.
    MessageBox.Show("I like you.");
    becomes
    MessageBox.Show(localizer["ILikeYou"]);  // XML localizer
    or
    MessageBox.Show(localizer.ILikeYou);  // hard-coded localizer
  2. Generates localizer files in any programming language using a predefined template, e.g. an XML file:
    <localizer>
        <language name="English">
            <string id="ILikeYou">I like you.</string>
        </language>
    </localizer>
    or a hard-coded localizer:
    interface ILocalizer
    {
        string ILikeYou { get; }
    }
    class EnglishLocalizer : ILocalizer
    {
        public string ILikeYou get { return "I like you."; }
    }
    ...etc.

Background

Sometimes a small project suddenly becomes a big project and we want to make it more global. If we were not provident enough to use resource files, we could come across tens of source files full of not localized (likely English) strings inside. So, we set our shoulder to the wheel and start to rewrite the whole code... Or we use some smart tool, like Project Localizer.

Using the Code

I have written a GUI (Graphics User Interface) for my tool and nothing else because there are two stages of localizing source files.

  1. Parse the source files.
  2. Choose which strings should be localized and name the variables (like ILikeYou for "I like you").

If the modified files don't compile, some manual corrections may be required. However, I did my best to prevent such situations.

The Project Localizer

After starting the application, you will see a window with three frames. With the one on the top, you can set the behaviour of the localizer. The most important properties are:

  • InputType - sets which property should be used as a source of files, InputDirectory or InputFiles.
  • InputDirectory and InputFiles - sets a location of source files to localize.
  • Template - sets how a generated localizer should look. I give you two ready-to-use templates, which can be accessed via xml.txt and hard.txt. See the Templates chapter to learn how to create templates.
  • All the rest properties are described in the program.

When everything is set up, press the rosy button "Process files." Then will appear all files that were successfully processed. If you click on any of them, then a grid with all strings found in a source file will be displayed. You can specify there:

  • Create checkbox - determine whether an entry should be included in a resource file.
  • Use checkbox - determine whether a string in a source file should be replaced with an expression referencing a generated resource entry.
  • C & U checkbox - sets both Create and Use.
  • Name of the variable field - A name of an entry in a resource file.
Screenshot - ProjectLocalizer_1.gif

Don't create and don't use buttons just to make your task easier. If you have already chosen which strings should be localized and how, then press the "Generate files" button. New files will be generated in the folder specified in the OutputDirectory property.

Templates

As I mentioned in the Introduction, my tool is language-independent because it uses templates. Now I would like to explain how to create them. templates\hard.txt is a template that generates hard-coded localization classes.

### SET getter=LC.Localizer.$
### HEADER using Gajatko.ProjectLocalizers.LC;
### FILE $sourceName$_hardloc.cs
namespace Gajatko.ProjectLocalizers
{
    partial interface IHardCodedLocalization
    {$loop$
        string $variableName$ { get; }$endLoop$
    }
    partial class $language$HardCodedLocalization : IHardCodedLocalization
    {$loop$
        public string $variableName$
        {
            get { return $content$; }
        }$endLoop$
    }
}
### END FILE
### FILE localizer.cs
using Gajatko.ProjectLocalizers;
namespace Gajatko.ProjectLocalizers.LC
{
    static class LC        // ("LocalizerContainer")
    {
        public static IHardCodedLocalization Localizer = 
            new $language$HardCodedLocalization();
    }
}
### END FILE

I think that construction of this file is self-explanatory in most cases. ### SET getter enables us to specify an expression used to replace strings in a source file, where the $ (dollar) sign represents a name of a resource entry. A $loop$ - $endLoop$ block says to the interpreter that a block should repeated for each string. ### FILE blocks are responsible for creating new files. Other variables enclosed in dollar signs are referencing particular properties.

Download

The solution includes Project Localizer itself and two demos. The Windows Forms application sample uses XML templates and the Console program uses a hard-coded localizer.

Remarks

  • Project Localizer supports strings prefixed with @ (ignoring escape characters) known from C#. However, this functionality can be turned off by setting the AllowIgnoreEscape property to false.
  • When using XML resource files, escape characters found in source files like \" are converted to their generic equivalents, like ". Also, critical for XML characters like ampersand or triangle brackets are converted to their HTML equivalents: &amp;, &lt;, etc.

Points of Interest

From my point of view, I have everything that I need. However, if you have created a new template for your use, especially in another language than C#, then please send it to me and I will upload it here.

History

  • 19 September, 2007 -- Original version posted
  • 13 May, 2008 -- "Index out of bounds" bug fixed

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jacek Gajek
Software Developer (Junior)
Poland Poland
My name is Jacek Gajek. I have graduated in computer science with a master's degree from Polibuda in Wrocław. I like C# and Monthy Python's sense of humour.

Comments and Discussions

 
GeneralVisual Localizer Pinmembercre8or120-Apr-13 3:46 
GeneralMinor bug PinmemberAnders Eriksson19-Jan-11 21:57 
GeneralRe: Minor bug PinmemberJacek Gajek20-Jan-11 8:13 
QuestionWhat about .net localization PinmemberUrs Enzler24-Sep-07 23:17 
AnswerRe: What about .net localization Pinmembergajatko24-Sep-07 23:54 
GeneralExcellent Pinmembermerlin98120-Sep-07 4:41 
GeneralRe: Excellent Pinmembergajatko21-Sep-07 22:00 
QuestionWhat about R# + RGreatEx ? PinmemberAlexander Nestrenko19-Sep-07 21:23 
AnswerRe: What about R# + RGreatEx ? Pinmembergajatko20-Sep-07 1:35 
GeneralRe: What about R# + RGreatEx ? PinmemberAlexander Nestrenko20-Sep-07 1:47 
GeneralRe: What about R# + RGreatEx ? PinmemberAlex Kucherenko14-May-08 7:00 
GeneralRe: What about R# + RGreatEx ? Pinmembergajatko14-May-08 10:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 13 May 2008
Article Copyright 2007 by Jacek Gajek
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid