Click here to Skip to main content
15,881,380 members
Articles / Programming Languages / C#
Article

Project Localizer

Rate me:
Please Sign up or sign in to vote.
4.35/5 (21 votes)
13 May 2008CPOL4 min read 47.8K   824   43   13
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.
    C#
    MessageBox.Show("I like you.");
    becomes
    C#
    MessageBox.Show(localizer["ILikeYou"]);  // XML localizer
    or
    C#
    MessageBox.Show(localizer.ILikeYou);  // hard-coded localizer
  2. Generates localizer files in any programming language using a predefined template, e.g. an XML file:
    XML
    <localizer>
        <language name="English">
            <string id="ILikeYou">I like you.</string>
        </language>
    </localizer>
    or a hard-coded localizer:
    C#
    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)


Written By
Software Developer
Poland Poland
My name is Jacek. Currently, I am a Java/kotlin developer. I like C# and Monthy Python's sense of humour.

Comments and Discussions

 
GeneralVisual Localizer Pin
cre8or120-Apr-13 3:46
cre8or120-Apr-13 3:46 
GeneralRe: Visual Localizer Pin
Ed Gadziemski11-Nov-14 4:47
professionalEd Gadziemski11-Nov-14 4:47 
GeneralMinor bug Pin
Anders Eriksson19-Jan-11 21:57
Anders Eriksson19-Jan-11 21:57 
GeneralRe: Minor bug Pin
Lutosław20-Jan-11 8:13
Lutosław20-Jan-11 8:13 
QuestionWhat about .net localization Pin
Urs Enzler24-Sep-07 23:17
Urs Enzler24-Sep-07 23:17 
AnswerRe: What about .net localization Pin
Lutosław24-Sep-07 23:54
Lutosław24-Sep-07 23:54 
GeneralExcellent Pin
merlin98120-Sep-07 4:41
professionalmerlin98120-Sep-07 4:41 
GeneralRe: Excellent Pin
Lutosław21-Sep-07 22:00
Lutosław21-Sep-07 22:00 
QuestionWhat about R# + RGreatEx ? Pin
Alexander Nesterenko19-Sep-07 21:23
Alexander Nesterenko19-Sep-07 21:23 
AnswerRe: What about R# + RGreatEx ? Pin
Lutosław20-Sep-07 1:35
Lutosław20-Sep-07 1:35 
GeneralRe: What about R# + RGreatEx ? Pin
Alexander Nesterenko20-Sep-07 1:47
Alexander Nesterenko20-Sep-07 1:47 
GeneralRe: What about R# + RGreatEx ? Pin
Oleksandr Kucherenko14-May-08 7:00
Oleksandr Kucherenko14-May-08 7:00 
GeneralRe: What about R# + RGreatEx ? Pin
Lutosław14-May-08 10:01
Lutosław14-May-08 10:01 

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

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