Header Ads

Breaking News
recent

Changing the language of a DevExpress XtraGrid

Until recently most desktop applications were written in one language, and for the most part this was English.  With the advent of the internet in almost every home more frequently applications would appear on the market in multiple languages, some even giving the end-user a mechanism to translate it to their own language.
Fast forward to 2010 where most are written in multiple languages and often global enterprises will only buy software that either is or can be translated into other languages.  This of course brings a set of challenges for developers who for the most part only know a few languages and the majority of those are programming not spoken ones.  These challenges are even more apparent when 3rd party components are used, such as data grids, editors and such like because translating these isn’t so obvious.
We use the DevExpress XtraGrid which are very configurable so I was shocked to see this code in our app to change the filter popup text:-
private void gridView1_ShowFilterPopupListBox(object sender, FilterPopupListBoxEventArgs e)
{
    //Custom
    ((FilterItem)e.ComboBox.Items[0]).Text =  
        _localisedResourceProvider.GetStringResource("GridControl", "FilterPopup.CustomText");
    //Blanks
    ((FilterItem)e.ComboBox.Items[1]).Text =  
        localisedResourceProvider.GetStringResource("GridControl", "FilterPopup.BlanksText");
    //Non blanks
    ((FilterItem)e.ComboBox.Items[2]).Text =  
        localisedResourceProvider.GetStringResource("GridControl", "FilterPopup.NonBlanksText");
}
This was more worrying that it didn't ALWAYS work correctly and sometimes would except killing the application.  Having used the DevExpress controls for many years I knew their had to be a better way, and their was in the form of the DevExpress.XtraGrid.Localization.GridLocalizer base class:-
public class XtraGridLocalizer : DevExpress.XtraGrid.Localization.GridLocalizer{
    LocalisedResourceProvider _localisedResourceProvider;
    public XtraGridLocalizer(LocalisedResourceProvider localisedResourceProvider)
    {
        _localisedResourceProvider = localisedResourceProvider;
    }

    private XtraGridLocalizer() { }

    private string GetLocalizedResourceString(String resourceId)
    {
        return _localisedResourceProvider.GetStringResource("GridControl", resourceId);
    }

    public override string GetLocalizedString(DevExpress.XtraGrid.Localization.GridStringId id)
    {
        switch (id)
        {
            case GridStringId.PopupFilterCustom:
                return GetLocalizedResourceString("FilterPopup.CustomText");
            case GridStringId.PopupFilterBlanks:
                return GetLocalizedResourceString("FilterPopup.BlanksText");
            case GridStringId.PopupFilterNonBlanks:
                return GetLocalizedResourceString("FilterPopup.NonBlanksText");
            // and so on
        }
        return base.GetLocalizedString(id);
    }
}
To use this new localizer all you need to do is this and your done...
DevExpress.XtraGrid.Localization.GridLocalizer.Active = 
    new XtraGridLocalizer(localisedResourceProvider);
Nice huh?! for more info visit XtraGrid - UI Localization

No comments:

Powered by Blogger.