Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
2 Aug 2009 1  
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 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