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

Windows Mobile Globalization in Visual Studio

, 19 Apr 2008 Ms-PL
Rate this:
Please Sign up or sign in to vote.
Simple technique to support multiple languages in Compact Framework WinForms.

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)

Share

About the Author

tbpub

United States United States
No Biography provided

Comments and Discussions

 
QuestionNeed common resource files instead of each form Pinmemberbagu4it14-Jun-13 2:38 
QuestionHad somebody compiled those code passed? Pinmemberjeam25-Oct-11 4:33 
GeneralBest Localization Plug-in for Visual Studio. PinmemberAlexander Nesterenko17-Dec-08 22:44 
GeneralLook good PinmemberYves17-Nov-08 11:00 
GeneralAnother hack to get around CurrentUICulture PinmemberFkYkko6-Nov-08 4:22 
GeneralBrillant! [modified] PinmemberPablo Grisafi4-May-08 11:53 
GeneralMore explanation request PinmemberMaruf Maniruzzaman19-Apr-08 17:22 
GeneralRe: More explanation request Pinmembertbpub20-Apr-08 8:12 
GeneralRe: More explanation request PinmemberAnand.E2-Apr-14 6:28 

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 | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 19 Apr 2008
Article Copyright 2008 by tbpub
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid