Laden...

Lokalisierung von Windows Mobile 6.0 Applikationen - Leicht gemacht...

Erstellt von ernsti vor 13 Jahren Letzter Beitrag vor 13 Jahren 4.779 Views
E
ernsti Themenstarter:in
38 Beiträge seit 2005
vor 13 Jahren
Lokalisierung von Windows Mobile 6.0 Applikationen - Leicht gemacht...

Hallo zusammen,

Ich habe lange nach einer Möglichkeit gesucht, die
CurrentCulture und CurrentUICulture von Windows Mobile Applikationen auch während der Laufzeit zu ändern, wie man es von Desktop Applikationen gewohnt ist, um mehrsprachige Anwendungen testen zu können.
Ich denke, dass ich nicht der erste und einzige bin, der auf dieses Problem gestossen ist, und möchte euch daher meine Lösung nicht vorenthalten.

Vorgehensweise:


Windows.Forms.Form 

erstellen.

Property


Localizeable=true

einstellen, und wie (aus Desktop Appliationen) gewohnt lokalisieren.

Anschliessend kann mit folgendem Code (analog zu Desktop Applikationen) die Kultur eingestellt (und während der Laufzeit geändert) werden, damit die Formulare und Controls lokalisiert dargestellt werden:


System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("de-at");
Windows.Mobile.Extensions.MobileGlobalization.CurrentCulture=ci;
Windows.Mobile.Extensions.MobileGlobalization.CurrentUICulture=ci;

Dank


System.Reflection

hab ich mir folgende Klasse gebastelt, die mir das erledigt:



using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace Windows.Mobile.Extensions
{
    /// <summary>
    /// Provides methos to get and set cultures.
    /// (Workaround for windows mobile 6.0)
    /// </summary>
    public static class MobileGlobalization
    {
        /// <summary>
        /// Occurs, when the CurrentCulture is changed.
        /// </summary>
        public static event System.EventHandler CurrentCultureChanged;
        /// <summary>
        /// Occurs, when the CurrentUICulture is changed.
        /// </summary>
        public static event System.EventHandler CurrentUICultureChanged;

        /// <summary>
        /// Gets or stes the System.Globalization.CultureInfo
        /// that represents the culture used by the current thread.
        /// </summary>
        public static System.Globalization.CultureInfo CurrentCulture
        {
            get
            {
                return System.Globalization.CultureInfo.CurrentCulture;
            }
            set
            {
                if (value.Name != System.Globalization.CultureInfo.CurrentUICulture.Name)
                {
                    MobileGlobalization.SetCultureInfo("m_userDefaultCulture", value);
                    OnCurrentCultureChanged(new System.EventArgs());
                }
            }
        }
        /// <summary>
        /// Gets or stes the System.Globalization.CultureInfo
        /// that represents the culture used by the Resouce Manager 
        /// to lookup culture-specific resources at run time.
        /// </summary>
        public static System.Globalization.CultureInfo CurrentUICulture
        {
            get
            {
                return System.Globalization.CultureInfo.CurrentUICulture;
            }
            set
            {
                if (value.Name != System.Globalization.CultureInfo.CurrentUICulture.Name)
                {
                    MobileGlobalization.SetCultureInfo("m_userDefaultUICulture", value);
                    OnCurrentUICultureChanged(new System.EventArgs());
                }
            }
        }
        /// <summary>
        /// Sets the specified cultureInfo to the field
        /// </summary>
        /// <param name="fieldName">Name of the field to set the specified cultureInfo.</param>
        /// <param name="cultureInfo">System.Globalization.CultureInfo to set to the field for specified fieldName.</param>
        private static void SetCultureInfo(string fieldName, System.Globalization.CultureInfo cultureInfo)
        {
            System.Reflection.FieldInfo currentCulture = typeof(System.Globalization.CultureInfo).GetField(fieldName,
                                                           System.Reflection.BindingFlags.Static |
                                                           System.Reflection.BindingFlags.NonPublic);

            currentCulture.SetValue(null, cultureInfo);
        }
        /// <summary>
        /// Raises the CurrentCultureChanged event.
        /// </summary>
        private static void OnCurrentCultureChanged(System.EventArgs e)
        {
            if(CurrentCultureChanged != null)
            {
                CurrentCultureChanged(null, e);
            }
        }
        /// <summary>
        /// Raises the CurrentUICultureChanged event.
        /// </summary>
        private static void OnCurrentUICultureChanged(System.EventArgs e)
        {
            if (CurrentUICultureChanged != null)
            {
                CurrentUICultureChanged(null, e);
            }
        }
    }
}

Nachdem ich auch Formulare benötigte, die es ermöglichen sofort nach dem Kulturwechsel die Anzeige zu lokalisieren, habe ich mir folgende Basisklasse für Windows Mobile Applikationen gemacht:


using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Windows.Mobile.Extensions.Forms
{
    /// <summary>
    /// Represents the base class for Mobile forms.
    /// This form supports localization changes at runtime
    /// </summary>
    public partial class MobileForm : System.Windows.Forms.Form
    {
        /// <summary>
        /// Applies the resources from the actual culture to
        /// the specified form.
        /// </summary>
        /// <param name="form">Form to reload resources.</param>
        public static void ReloadResources(this System.Windows.Forms.Form form)
        {
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
            try
            {
                System.ComponentModel.ComponentResourceManager resources =
                    new System.ComponentModel.ComponentResourceManager(form.GetType());
                ApplyResources(resources, form);
            }
            finally
            {
                System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
            }
        }
        /// <summary>
        /// Applies the resources from the specified resource manager to 
        /// the specified control.
        /// </summary>
        /// <param name="resources">ResourceManager to get resources.</param>
        /// <param name="control">Control to reload resources.</param>
        private static void ApplyResources(System.ComponentModel.ComponentResourceManager resources, System.Windows.Forms.Control control)
        {
            foreach (System.Windows.Forms.Control c in control.Controls)
            {
                ApplyResources(resources, c);
            }
            resources.ApplyResources(control, control.Name);
        }
        /// <summary>
        /// Returns true, if the specified controls is in the design mode.
        /// </summary>
        /// <param name="control">Control to determine, if it is design mode.</param>
        /// <returns>true, if the specified controls is in design mode.</returns>
        public static bool IsDesignMode(this System.Windows.Forms.Control control)
        {
            return ((control.Site != null && control.Site.DesignMode));
        }
        /// <summary>
        /// Initializes an new instance of the MobileForm-class.
        /// </summary>
        public MobileForm()
        {
            InitializeComponent();
            MobileGlobalization.CurrentUICultureChanged += new EventHandler(MobileGlobalizationCurrentUICultureChanged);
        }
        /// <summary>
        /// Raises the Closed event.
        /// </summary>
        protected override void OnClosed(EventArgs e)
        {
            MobileGlobalization.CurrentUICultureChanged -= new EventHandler(MobileGlobalizationCurrentUICultureChanged);
            base.OnClosed(e);
        }
        /// <summary>
        /// Reloads the resources, if the culture is changed.
        /// </summary>
        protected virtual void MobileGlobalizationCurrentUICultureChanged(object sender, System.EventArgs e)
        {
            this.ReloadResources();
        }
    }
}

Viel Spaß und schöne Grüße aus Austria ! 😁

185 Beiträge seit 2005
vor 13 Jahren

hallo,

danke für das Tutorial, ich möchte jedoch noch darauf hinweisen, dass es ein Problem bei der Lokalisierung gibt, sobald man für sein CF-Programm ein CAB Setup erstellt.

Das "tückische" ist, dass alles supa funktioniert, solange man im Debugger testet - wenn man jedoch deployd, funktioniert es dann nicht mehr.

Beschreibung des Bugs:
http://www.codeproject.com/KB/windows/celocalization.aspx
http://blogs.compactframework.de/Peter.Nowak/Trackback.aspx?guid=c1173b7e-3dd6-4734-86eb-596f2abb3762

fg
hannes

E
ernsti Themenstarter:in
38 Beiträge seit 2005
vor 13 Jahren

Hallo zusammen,

Ich hab grade aktuell ein Projekt für Windows Mobile 6.1 ( VS2008 ) mit der o.a. Lokalisierung gemacht, habe die Release Version als CAB zum Installieren aufs Mobile gespielt und installiert - KEINE PROBLEME ! - Lokalisierung funktioniert tadellos ! 8)