Introduction
Project Localizer is a tool to localize any kind of source file. What does it mean, exactly? The tool does two things:
- Replaces all strings in a source file with a specified expression, e.g.
MessageBox.Show("I like you.");
becomes
MessageBox.Show(localizer["ILikeYou"]);
or
MessageBox.Show(localizer.ILikeYou);
- 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.
- Parse the source files.
- 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.
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: &
, <
, 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
My name is Jacek. Currently, I am a Java/kotlin developer. I like C# and Monthy Python's sense of humour.