none
wpf internationalization - change culture repeatedly during runtime

    Question

  • Does anyone know if there is an easy way to build a WPF app that can change culture at the touch of a button?  It seems easy enough to change it once at app startup but my app needs to have buttons for changing language/culture at which point text/controls in windows that have already been loaded need to change. 

    I am currently getting my text from resx files, so my xaml looks like :

    <Window x:Class="xxx.HwDiagWnd" 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:properties="clr-namespace:xxx.Properties"   
       Title="testwnd" WindowStartupLocation="CenterScreen" Height="600" Width="800"   
       WindowStyle="None" FontSize="16" FontWeight="Bold" Activated="Window_Activated">  
       <Button Margin="402,34,0,0" Name="yyy" Height="43" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120" Content="{x:Static properties:Resources.yyyText}"/>  
    </Window> 
     
     

    code to switch language currently looks like below - this works fine only on windows that have not previously been loaded.

     

    private void SwitchLang(string langname)  
    {  
        if (Properties.Resources.Culture.Name != langname)  
        {  
           Properties.Resources.Culture = new CultureInfo(langname);  
           // reload all windows so it regenerates language strings?  
        }  
    }  
     
     


    thanks for help!

    Tuesday, February 24, 2009 9:26 PM

Answers

  • I'm using this: http://www.codeplex.com/WPFLocalizeExtension

    Works like a charm.  And yes, we do have to change cultures on the fly.  I looked at a few alternatives and this was the simplest one (with the bonus that it actually works as well. :))

    Also, CodeProject article about it: http://www.codeproject.com/KB/WPF/WPF_Localization.aspx
    • Marked as answer by Lucretious Wednesday, February 25, 2009 7:47 PM
    Wednesday, February 25, 2009 4:11 PM

All replies

  • You can add an additional wrapper layer.

    e.g.
    public class Cls_Window1_LocaliztionHandler:System.ComponentModel.INotifyPropertyChanged 
        { 
            public string Label1 
            { 
                get 
                { 
                    return Window1Resource.Label1; 
                } 
            } 
     .......
           






     
     
            public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged; 
            public void CultureInfoChanged() 
            { 
                if (PropertyChanged != null) 
                { 
                    PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(null)); 
                } 
            } 
     
        } 

    In the window, add the resource

    <Window...
    <Window.Resources>
    </Window.Resources>
    ... <ns:Cls_Window1_LocaliztionHandler x:Key="localizer1" />


    using the code to bind the localized string
    ...
    <TextBlock Text="{Binding Path=Label1, Source={StaticResource localizer1}}"/>
    ....

    {
               //after change the cultureInfo,e.g. in your case
                //Properties.Resources.Culture = new CultureInfo(langname)

                //call
                Cls_Window1_LocaliztionHandler rr = (Cls_Window1_LocaliztionHandler)FindResource("localizer1");
                rr.CultureInfoChanged();
            }

    Wednesday, February 25, 2009 1:47 AM
  • Hi Jacky,

    thanks for assist.  i'm new to XAML and WPF and am having some trouble getting it to work.  the line "<ns:Cls_Window1_LocaliztionHandler x:Key="localizer1" />" fails as the parser doesn't know what ns is.  don't i have to define that in the XAML?

    also, this looks like i would have to write a property for every single bit of text and that would be quite bit of a burden as well as make it difficult to provide custom skins for our application without programmer assistance (another goal). 
    • Proposed as answer by Michel R Wednesday, February 25, 2009 4:12 PM
    Wednesday, February 25, 2009 3:49 PM
  • I'm using this: http://www.codeplex.com/WPFLocalizeExtension

    Works like a charm.  And yes, we do have to change cultures on the fly.  I looked at a few alternatives and this was the simplest one (with the bonus that it actually works as well. :))

    Also, CodeProject article about it: http://www.codeproject.com/KB/WPF/WPF_Localization.aspx
    • Marked as answer by Lucretious Wednesday, February 25, 2009 7:47 PM
    Wednesday, February 25, 2009 4:11 PM
  • Michel,

    thank you, gracias, merci, etc.  it works!

    Luc

    Wednesday, February 25, 2009 7:47 PM
  • When I try to run my app with the extension, I recieve the following XamlParseException:

    'WPFLocalization.LocExtension' value cannot be assigned to property "Content' of object "System.Windows.Controls.Label'. Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Test1.Properties.Resources.resources" was correctly embedded or linked into assembly "Test1" at compile time, or that all the satellite assemblies required are loadable and fully signed. Error at object

    Any clues would be appreciated.

    :-)

     


    SoftwareDrone
    Monday, April 04, 2011 8:32 PM