0

I want to write WPF MVVM application with Multilingual User Interface. (on same screen) I have a "change language" button.And I want to when I clicked the button user interface language will change I want to use translating the UI English From Turkish and Turkish from English with a button.s I prepared all resx file and I m sharing all my codes. My codes work properly.(But language doesnt change when I click the button UI language doesnt change

I m sharing all my codes please help me.

//MainWindow.xaml It's my xaml class I use bindings and resources.

<Window x:Class = "WPFLocalization.MainWindow" 
       xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:local = "clr-namespace:WPFLocalization" 
       xmlns:p = "clr-namespace:WPFLocalization.Properties"
       Title = "{x:Static p:Resources.Title}" Height = "350" Width = "604">

        <Grid>
            <TextBox x:Name = "textBox" HorizontalAlignment = "Left" Height = "23" 
             Margin = "128,45,0,0" TextWrapping = "Wrap" VerticalAlignment = "Top" Width = "304"/>
            <Label x:Name = "label" Content = "{x:Static p:Resources.Name}"
             HorizontalAlignment = "Left" Margin = "52,45,0,0" VerticalAlignment = "Top" Width = "86"/>

            <TextBox x:Name = "textBox1" HorizontalAlignment = "Left" Height = "23" 
             Margin = "128,102,0,0" TextWrapping = "Wrap" VerticalAlignment = "Top" Width = "304"/>

            <Label x:Name = "label1" Content = "{x:Static p:Resources.Address}" 
             HorizontalAlignment = "Left" Margin = "52,102,0,0" VerticalAlignment = "Top" Width = "86"/>

            <TextBox x:Name = "textBox2" HorizontalAlignment = "Left" Height = "23" 
             Margin = "128,157,0,0" TextWrapping = "Wrap" VerticalAlignment = "Top" Width = "80"/>

            <Label x:Name = "label2" Content = "{x:Static p:Resources.Age}" 
             HorizontalAlignment = "Left" Margin = "52,157,0,0" VerticalAlignment = "Top" Width = "86"/>

            <Button x:Name = "button" Content = "{x:Static p:Resources.OK_Button}" 
             HorizontalAlignment = "Left" Margin = "128,241,0,0" VerticalAlignment = "Top" Width = "80"/>

            <Button x:Name = "button1" Content = "{x:Static p:Resources.Cancel_Button}" 
             HorizontalAlignment = "Left" Margin = "265,241,0,0" VerticalAlignment = "Top" Width = "80">

            </Button>

            <Button x:Name = "button2" Content = "{x:Static p:Resources.Help_Button}" 
             HorizontalAlignment = "Left" Margin = "380,241,0,0" VerticalAlignment = "Top" Width = "70"/>
            <Button x:Name="button3" Command= "{Binding ChangeButtonCommand}"  Content= "{x:Static p:Resources.Change_Button}" 
            HorizontalAlignment="Left" Margin="470,241,0,0" VerticalAlignment="Top" Width="100"/>
        </Grid> 

    </Window>
//LocalizationViewModel.cs It 's my MVVM class.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Configuration;
    using System.Globalization;
    using System.Reflection;
    using System.Resources;
    namespace WPFLocalization
    {
 class LocalizationViewModel : ViewModelBase
        {
           public LocalizationViewModel()//Constructor
            {

            }
            #region Commands

            #region changeButtonCommand
            private DelegateCommand _changeButtonCommand;
            public DelegateCommand ChangeButtonCommand
            {
                get
                {
                    return _changeButtonCommand ?? (_changeButtonCommand = new DelegateCommand(ExecuteLocalization, CanExecuteLocalization));

                }
            }



            public bool CanExecuteLocalization()
            {



                return true;
            }


            public void ExecuteLocalization()
            {

                Thread.CurrentThread.CurrentUICulture = new CultureInfo ("en-US");
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");




            }
        }

        #endregion
        #endregion
    }





   // DelegateCommand.cs// its constant.
 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Input;

    namespace WPFLocalization
    {
        //https://www.codecompiled.com/wpf/implementing-icommand-in-wpf-using-mvvm/
        public class DelegateCommand : ICommand
        {
            public event EventHandler CanExecuteChanged
            {
                add { CommandManager.RequerySuggested += value; }
                remove { CommandManager.RequerySuggested -= value; }
            }

            private readonly Action _executeMethod;
            private readonly Func<bool> _canExecuteMethod;

            public DelegateCommand(Action executeMethod, Func<bool> canExecuteMethod)
            {
                _executeMethod = executeMethod;
                _canExecuteMethod = canExecuteMethod;
            }
            //public void RaiseCanExecuteChanged()
            //{
            //    if (CanExecuteChanged != null)
            //        CanExecuteChanged(this, new EventArgs());
            //}

            public bool CanExecute()
            {
                if (_canExecuteMethod != null)
                {
                    return _canExecuteMethod();
                }
                return true;
            }

            public void Execute()
            {
                if (_executeMethod != null)
                {
                    _executeMethod();
                }
            }
            bool ICommand.CanExecute(object parameter)
            {
                return CanExecute();
            }

            void ICommand.Execute(object parameter)
            {
                Execute();
            }
        }

        public class DelegateCommand<T> : ICommand
        {
            public event EventHandler CanExecuteChanged
            {
                add { CommandManager.RequerySuggested += value; }
                remove { CommandManager.RequerySuggested -= value; }
            }
            private readonly Action<T> _executeMethod;
            private readonly Func<T, bool> _canExecuteMethod;

            #region Constructors
            public DelegateCommand(Action<T> executeMethod)
            {
                _executeMethod = executeMethod;
            }
            public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)
            {
                _executeMethod = executeMethod;
                _canExecuteMethod = canExecuteMethod;
            }
            #endregion

            #region Public Methods
            public bool CanExecute(T parameter)
            {
                if (_canExecuteMethod != null)
                {
                    return _canExecuteMethod(parameter);
                }
                return true;
            }

            public void Execute(T parameter)
            {
                if (_executeMethod != null)
                {
                    _executeMethod(parameter);
                }
            }
            #endregion

            #region ICommand Members

            bool ICommand.CanExecute(object parameter)
            {
                if (parameter == null &&
                    typeof(T).IsValueType)
                {
                    return _canExecuteMethod == null;
                }
                return CanExecute((T)parameter);
            }

            void ICommand.Execute(object parameter)
            {
                Execute((T)parameter);
            }

            #endregion
        }
    }

I don't take error message. Only UI doesn't change. please help me

mrBLACK
  • 21
  • 4
  • Possible duplicate of [WPF: How to change the CurrentUICulture at runtime](https://stackoverflow.com/questions/4626627/wpf-how-to-change-the-currentuiculture-at-runtime) – Sinatr Aug 01 '19 at 06:54
  • It doesnt use mvvm – mrBLACK Aug 01 '19 at 07:05
  • Accepted answer in duplicate explains why *"language doesnt change"*. You have to reload resources (e.g. by closing and opening window again). – Sinatr Aug 01 '19 at 07:19

1 Answers1

0

I think what you need is to refresh the screen after updating the language. Maybe looking at this project might set you in the right direction.

https://jeremybytes.blogspot.com/2013/07/changing-culture-in-wpf.html

By the looks of it, the whole window needs to be recreated after the culture changes. Create the static functions Jeremy mentions in App.xaml.cs and call it from ExecuteLocalization() with your new culture.

Fernando
  • 614
  • 1
  • 9
  • 20