Click here to Skip to main content
Click here to Skip to main content

Windows Mobile Globalization in Visual Studio

By , 19 Apr 2008
 

Introduction

The Compact Framework does not support changing Thread.CurrentUICulture to change a form's language on the fly.

Background

This code defines a custom version of System.Globalization.ComponentResourceManager in the global namespace that inherits from the standard version and overrides ApplyResource() to use a custom culture. Code generated in InitializeComponent() will compile using this version, and the Forms Editor will not break because you do not change the generated code. Wrap the function in "pragma warning disable 436" to suppress the warning about using an ambiguous class reference.

Use the References node in the Properties sheet to change the alias for System from "global" to "global,ms".

extern alias ms;
using cm = ms::System.ComponentModel;
using System.Globalization;
using Wm2005Global;

namespace System.ComponentModel
{
    class ComponentResourceManager : cm.ComponentResourceManager
    {
        public ComponentResourceManager(Type type) : base(type) { }
        public override void ApplyResources(object value, 
                        string objectName, CultureInfo culture)
        {
            if (culture == null)
            {
                culture = Program.Culture;
            }
            base.ApplyResources(value, objectName, culture);
        }
    }
}

This shows how Visual Studio generates the normal code and uses the special version of ComponentResourceManager by default. The #pragma disables the warnings about using the ambiguous class:

#region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    #pragma warning disable 436
    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = 
          new System.ComponentModel.ComponentResourceManager(typeof(Form1));
        this.mainMenu1 = new System.Windows.Forms.MainMenu();
        this.button1 = new System.Windows.Forms.Button();
        this.button2 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // button1
        // 
        resources.ApplyResources(this.button1, "button1");
        this.button1.Name = "button1";
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // button2
        // 
        resources.ApplyResources(this.button2, "button2");
        this.button2.Name = "button2";
        this.button2.Click += new System.EventHandler(this.button2_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
        resources.ApplyResources(this, "$this");
        this.Controls.Add(this.button2);
        this.Controls.Add(this.button1);
        this.Menu = this.mainMenu1;
        this.Name = "Form1";
        this.Load += new System.EventHandler(this.Form1_Load);
        this.ResumeLayout(false);

    }
#pragma warning restore 436
    #endregion

    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
}

I think this is the cleanest possible solution to Globalization on Windows Mobile 2005. You do not have to change the default language for the entire device, and you can even change the language on a form from a menu item without closing it! This also demonstrates a technique for disposing all the controls on a form, calling InitializeComponent() and Form_Load() again, so it's like your form is recreated without closing it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

tbpub
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHad somebody compiled those code passed?memberjeam25 Oct '11 - 3:33 
GeneralBest Localization Plug-in for Visual Studio.memberAlexander Nesterenko17 Dec '08 - 21:44 
GeneralLook goodmemberYves17 Nov '08 - 10:00 
GeneralAnother hack to get around CurrentUICulturememberFkYkko6 Nov '08 - 3:22 
GeneralBrillant! [modified]memberPablo Grisafi4 May '08 - 10:53 
GeneralMore explanation requestmemberMaruf Maniruzzaman19 Apr '08 - 16:22 
GeneralRe: More explanation requestmembertbpub20 Apr '08 - 7:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 19 Apr 2008
Article Copyright 2008 by tbpub
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid