Click here to Skip to main content
15,884,866 members
Articles / Programming Languages / C#

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

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
2 Aug 2009CPOL2 min read 38.3K   565   28   5
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.

C#
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

C#
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:

C#
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.

C#
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)


Written By
United States United States

Comments and Discussions

 
QuestionHow to get Comment from resx Pin
Member 109197702-Jul-14 5:51
Member 109197702-Jul-14 5:51 
GeneralLatest source + crash Pin
Jan Sundqvist22-Feb-11 3:05
Jan Sundqvist22-Feb-11 3:05 
GeneralRe: Latest source + crash Pin
Angela Han17-Mar-11 6:12
Angela Han17-Mar-11 6:12 
GeneralSampleResourceBundle project - demos for how to organize locale files in the same locale folder in .Net, including WPF projects Pin
Angela Han5-Sep-09 9:19
Angela Han5-Sep-09 9:19 
GeneralNew version downloadable at googleCodes Pin
Angela Han31-Jul-09 17:43
Angela Han31-Jul-09 17:43 

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.