Click here to Skip to main content
Click here to Skip to main content
Go to top

Locale Manager (2) - A Practical Tool to Manage *.resx Files for .NET or *.properties Files for Java /Flex

, 2 Aug 2009
Rate this:
Please Sign up or sign in to vote.
Enhanced support for *.resx files from LocaleManager1, a C# implementation to help manage *.resx files or *.properties files in different locale folders.

localeManager1.png

localeManager2.png

localeManager3.png

Introduction

This is an enhanced version of LocaleManager version 1. This software is to help solve the problem that resource files in different locale folders can easily go out of sync. It is very easy to use, and can be handed over to translators or to people who are not developers.

Background

When I started to write this tool, it was mainly to manage the *.properties files I have for an Adobe Air project with Flex development. The code added to support *.resx files was very rough and didn't even support Save changes. Also, I didn't do any research on how to work with *.resx files, and simply parsed them as XML files.

This new build fully supports *.resx files. It also allows the user to create a new locale folder by creating a new directory with the files in the base locale directory copied over. Every single little help makes the user's life easier.

Using the Code

I. Read *.resx files by using ResXResourceReader

ResXResourceReader is very handy, and reads a name and value pair from a *.resx file.

using (ResXResourceReader rd = new ResXResourceReader(path))
{
    KeyValuePair kvp = new KeyValuePair();
    foreach (DictionaryEntry d in rd)
    {
        kvp.key = d.Key.ToString();
        kvp.value = d.Value.ToString();
        //add kvp to DataGrid
    }
}

The only issue with ResXResourceReader is that it is a little bit awkward to get the comments too. An enumerator needs to be used to enumerate each ResXDataNode in order to get the comments. However, a ResXDataNode doesn't include the value of the resource. So, we have to read the *.resx file in two rounds: first, get a key value pair as shown in the code above, then use the code below to get the comment.

sampleResx.png

using (ResXResourceReader rd = new ResXResourceReader(path))
{
    rd.UseResXDataNodes = true;
    IEnumerator enumerator = rd.GetEnumerator();
    while (enumerator.MoveNext())
    {
        DictionaryEntry d = (DictionaryEntry)enumerator.Current;
        ResXDataNode dataNode = (ResXDataNode)d.Value;
        KeyValuePair kvp = new KeyValuePair();
        kvp.key = dataNode.Name;
        kvp.comment = dataNode.Comment;
        AddCommentToGrid(file, kvp);
    }
}

II. Change DataGrid Cell Style

A DataGrid's column style and cell style can be changed easily. In the property view for the DataGrid, DefaultCellStyle and AlternationRowsDefaultCellStyle can be used to set the cell styles.

Then, I use the following code to set the highlight color for the base locale columns:

m_grid.Rows[i].Cells[j].Style.BackColor = BaseColumnColor;
m_grid.Rows[i].Cells[j].Style.BackColor = BaseColumnColor;

III. Display All Languages Selection by Using CultureInfo

As in the main page, the user can select a language and create a new locale folder. It is very easy to get a list of all cultures, by using CultureInfo.

CultureInfo[] allCultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
foreach (CultureInfo c in allCultures)
    m_langue.Items.Add(c);   //add to the ComboBox

m_langue.SelectedIndex = 0;

private void m_langue_SelectedIndexChanged(object sender, EventArgs e)
{
    // on selection of a culture in the combo box
    CultureInfo c = (CultureInfo)(m_langue.SelectedItem);
    //display the default locale name when a culture is selected
    m_locale.Text = c.Name;
}

History

  • 2/20/09 - Initial version to support *.properties files
  • 2/26/09 - Added support for *.resx load and view, but not save
  • 3/20/09 - Full support for *.resx files. Also added the Create a new locale feature.
  • 3/21/09 - Updated download zips
  • 3/24/09 - Updated demo
  • 7/31/09 - Added LocaleManager_1.3_Executables.zip

License

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

Share

About the Author


Comments and Discussions

 
GeneralNew version downloadable at googleCodes PinmemberAngela Han31-Jul-09 17:43 

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
Web02 | 2.8.140926.1 | Last Updated 2 Aug 2009
Article Copyright 2009 by Angela Han
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid