Note - Tips - Trick - .Net

Lưu các thủ thuật, code mẫu, cách dùng về .Net

WPF chuyển đổi ngôn ngữ ngay lập tức WPF chuyển đổi ngôn ngữ ngay lập tức WPF chuyển đổi ngôn ngữ ngay lập tức WPF chuyển đổi ngôn ngữ ngay lập tức WPF chuyển đổi ngôn ngữ ngay lập tức 0/5 (13 reviews)

WPF chuyển đổi ngôn ngữ ngay lập tức

Đã đăng 2018-06-25 19:37:56 bởi K9
0 bình luận 1960 lượt xem
WPF chuyển đổi ngôn ngữ ngay lập tức 0 /5 stars (0 reviews)
 

MainWindow.xaml

<TextBlock Text="{DynamicResource useNamerHint}"/>
        <Grid Grid.Row="8" Grid.ColumnSpan="2">
            <ComboBox ItemsSource="{Binding ListLanguage}" 
                      HorizontalAlignment="Left" 
                      VerticalAlignment="Center"
                      Width="100"
                      SelectedIndex="0" 
                      SelectionChanged="CoboBox_SelectionChanged"/>
        </Grid>       

MainWindow.cs

/// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private List<string> listLanguage;
        public List<string> ListLanguage
        {
            get { return listLanguage; }
            set
            {
                listLanguage = value;
                OnPropertyChanged(nameof(ListLanguage));
            }
        }
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            ListLanguage = new List<string>() { "Tiếng Việt", "English"};
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox combo = sender as ComboBox;
            if (combo.SelectedItem == null)
                return;
            bool isEN = !combo.SelectedItem.ToString().Equals("English") ? false : true;
            LanguageManager.SetLanguageDictionary(isEN ? ELanguage.English : ELanguage.VietNamese);
            this.InitializeComponent();

        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string newName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(newName));
            }
        }

        private void CoboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox combo = sender as ComboBox;
            if (combo.SelectedItem == null)
                return;
            bool isEN = !combo.SelectedItem.ToString().Equals("English") ? false : true;
            LanguageManager.SetLanguageDictionary(isEN ? ELanguage.English : ELanguage.VietNamese);
            this.InitializeComponent();
        }
    }

    public static class LanguageManager
    {
        public static void SetLanguageDictionary(ELanguage lang)
        {
            ResourceDictionary dict = new ResourceDictionary();
            switch (lang)
            {
                case ELanguage.English:
                    dict.Source = new Uri("..\\Resource\\ResourceString.en-US.xaml",
                             UriKind.Relative);

                    break;
                case ELanguage.VietNamese:
                    dict.Source = new Uri("..\\Resource\\ResourceString.vi-VN.xaml",
                                   UriKind.Relative);
                    break;
                default:
                    dict.Source = new Uri("..\\Resource\\ResourceString.en-US.xaml",
                              UriKind.Relative);
                    break;
            }
            Cons.CurrentLanguage = lang;
            Application.Current.Resources.MergedDictionaries.Clear();
            Application.Current.Resources.MergedDictionaries.Add(dict);
        }
    }
    public enum ELanguage
    {
        English,
        VietNamese
    }

    public static class Cons
    {
        public static ELanguage CurrentLanguage = ELanguage.English;
    }

App.xaml

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary x:Name="ThemeDictionary">
                    <ResourceDictionary.MergedDictionaries>
                        <ResourceDictionary Source="/Resource/ResourceString.en-US.xaml"/>
                        <ResourceDictionary Source="/Resource/BaseResource.xaml"/>
                    </ResourceDictionary.MergedDictionaries>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

Resource\ResourceString.en-US.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:system="clr-namespace:System;assembly=mscorlib"
                    xmlns:local="clr-namespace:Quan_Ly_Ban_Hang.Resource">

    <ImageBrush x:Key="btnLoginImgIdle" ImageSource="/Images/UK/bt_login.png"/>
    <ImageBrush x:Key="btnLoginImgHover" ImageSource="/Images/UK/bt_login_1.png"/>
    <ImageBrush x:Key="btnLoginImgClick" ImageSource="/Images/UK/bt_login_2.png"/>
    
    <system:String x:Key="close">Close</system:String>
    <system:String x:Key="login">Login</system:String>
    <system:String x:Key="useNamerHint">User name</system:String>
    <system:String x:Key="passwordHint">Password</system:String>
    <system:String x:Key="rememberMe">Remember me?</system:String>
    <system:String x:Key="loginButton">Login</system:String>
    <system:String x:Key="createAccount">Don't have an account?</system:String>
    <system:String x:Key="forgetPassword">Forget password?</system:String>
    <system:String x:Key="mainTitle">Champion online - Created by Kteam</system:String>
    <system:String x:Key="register">Register</system:String>
    <system:String x:Key="registerBaner">Please create your account</system:String>
    <system:String x:Key="registerCheckBoxPrivacy">I'm over 18 years of age and I acept these</system:String>
    <system:String x:Key="registerTermAndCondition">Terms and Condition</system:String>
    <system:String x:Key="registerAnd">and</system:String>
    <system:String x:Key="registerPrivacy">Privacy policy</system:String>
    <system:String x:Key="registerRegisterButton">Register</system:String>
    <system:String x:Key="registerAllFieldAreRequire">*All fields are require</system:String>
    <system:String x:Key="registerFirstName">First name</system:String>
    <system:String x:Key="registerUserName">User name</system:String>
    <system:String x:Key="registerPassword">Password</system:String>
    <system:String x:Key="registerReEnterPassword">Re-enter password</system:String>
    <system:String x:Key="registerLastName">Last name</system:String>
    <system:String x:Key="registerEmail">Email</system:String>
    <system:String x:Key="registerCountry">Country</system:String>
    <system:String x:Key="registerDateOfBirth">Date of birth</system:String>
    <system:String x:Key="registerGender">Gender</system:String>
    <system:String x:Key="forgotPasswordTitle">Forget password</system:String>
    <system:String x:Key="forgotPasswordBanner">Reset your password</system:String>
    <system:String x:Key="forgotPasswordHint">Champion Online ID</system:String>
    <system:String x:Key="forgotPasswordButton">Reset password</system:String>
    <system:String x:Key="ok">OK</system:String>
    <system:String x:Key="language">English</system:String>
    
    <system:String x:Key="dota2">Dota2</system:String>
    <system:String x:Key="lol">LOL</system:String>
    <system:String x:Key="tour1vs1">Tour 1vs1</system:String>
    <system:String x:Key="tour5vs5">Tour 5vs5</system:String>
    <system:String x:Key="t1vs1">1vs1</system:String>
    <system:String x:Key="t5vs5">5vs5</system:String>
    <system:String x:Key="other">Other</system:String>
    <system:String x:Key="option">Option</system:String>
    <system:String x:Key="favoriteGame">Favorite game</system:String>
    <system:String x:Key="account">Account</system:String>
    <system:String x:Key="cashier">Cashier</system:String>
    <system:String x:Key="setting">Setting</system:String>
    <system:String x:Key="getStarted">Get started</system:String>
    <system:String x:Key="aboutUS">About US</system:String>
    <system:String x:Key="history">History</system:String>
    <system:String x:Key="typeGame">Type: </system:String>
    <system:String x:Key="champ">Champ: </system:String>
    <system:String x:Key="pricePool">Price pool: </system:String>
    <system:String x:Key="time">Time: </system:String>
    <system:String x:Key="first">1st: </system:String>
    <system:String x:Key="second">2nd: </system:String>
    <system:String x:Key="thir">3rd: </system:String>
    <system:String x:Key="join">Join</system:String>
    <system:String x:Key="check">Check</system:String>
    <system:String x:Key="start">Start</system:String>
    <system:String x:Key="nextGameIn">Next game in: </system:String>
    <system:String x:Key="cashierWithDraw">Withdraw</system:String>
    <system:String x:Key="cashierDeposit">Deposit</system:String>
    <system:String x:Key="cashierHistory">History transfer</system:String>
    <system:String x:Key="cashierDepositBanner">Deposit your money with Paypal. Please click to button.</system:String>
    <system:String x:Key="cashierDepositPaypalButton">Paypal</system:String>
    <system:String x:Key="cashierWithdrawBanner">Withdraw your money with Paypal. Please click to button.</system:String>
    <system:String x:Key="cashierWithdrawPaypalButton">Paypal</system:String>
    <system:String x:Key="cashierMoneyTransferHistory">Transfer history</system:String>
    <system:String x:Key="accountInfoChangeAvatarButton">Change image</system:String>
    <system:String x:Key="accountInfoEditButton">Edit</system:String>
    <system:String x:Key="accountStartID">Nick name:</system:String>
    <system:String x:Key="accountPassword">Password:</system:String>
    <system:String x:Key="accountNewPassword">New password</system:String>
    <system:String x:Key="accountReEnterPassword">Re-enter password</system:String>
    <system:String x:Key="accountChangedPassword">Password changed. Please re-login!</system:String>
    <system:String x:Key="accountEmail">Email:</system:String>
    <system:String x:Key="accountPhone">Phone:</system:String>
    <system:String x:Key="accountAddress">Address:</system:String>
    <system:String x:Key="accountDateOfBirth">Date of birth:</system:String>
    <system:String x:Key="accountGender">Gender:</system:String>
    <system:String x:Key="accountStartPIN">Start PIN:</system:String>
    <system:String x:Key="accountRSASecurityToken">RSASecurityToken:</system:String>
    <system:String x:Key="accountSMSSecurityToken">SMS security token:</system:String>
    <system:String x:Key="accountVerifyAccount">Verify account</system:String>
    
    <system:String x:Key="errorTitle">Error</system:String>
    <system:String x:Key="succesTitle">Successed</system:String>
    <system:String x:Key="loginWrongUserName">Wrong user name</system:String>
    <system:String x:Key="loginWrongPassword">Wrong password</system:String>
    <system:String x:Key="systemError">System error</system:String>
    <system:String x:Key="registerOver18">You have to over 18 years of age to create account</system:String>
    <system:String x:Key="registerInvalidEmail">Invalid email</system:String>
    <system:String x:Key="registerReenterPassword">Re-Enter password must be correct with password</system:String>
    <system:String x:Key="registerExistUserName">Exists user name</system:String>
    <system:String x:Key="registerPasswordNotStrong">Password must contain at least: Upper, Lower, Number and special character</system:String>
    <system:String x:Key="registerSuccess">Successfully register account</system:String>
    <system:String x:Key="registerEmtyUserName">User name can not be null or emty</system:String>
    <system:String x:Key="fogetPasswordSuccessed">New password sent to email!</system:String>
    <system:String x:Key="verify">Verify Account</system:String>
    <system:String x:Key="chamiponRoomHeader">Champion</system:String>
    <system:String x:Key="feeRoomHeader">Fee</system:String>
    <system:String x:Key="timeRoomHeader">Time</system:String>
    <system:String x:Key="typeRoomHeader">Type</system:String>
    <system:String x:Key="statusRoomHeader">Status</system:String>
</ResourceDictionary>

Resource\ResourceString.vi-VN.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:system="clr-namespace:System;assembly=mscorlib"
                    xmlns:local="clr-namespace:Quan_Ly_Ban_Hang.Resource">

    <ImageBrush  x:Key="btnLoginImgIdle" ImageSource="/Images/VN/bt_login.png"/>
    <ImageBrush  x:Key="btnLoginImgHover" ImageSource="/Images/VN/bt_login_1.png"/>
    <ImageBrush x:Key="btnLoginImgClick" ImageSource="/Images/VN/bt_login_2.png"/>
    
    <system:String x:Key="close">Đóng</system:String>
    <system:String x:Key="login">Đăng nhập</system:String>
    <system:String x:Key="useNamerHint">Tài khoản</system:String>
    <system:String x:Key="passwordHint">Mật khẩu</system:String>
    <system:String x:Key="rememberMe">Nhớ tài khoản</system:String>
    <system:String x:Key="loginButton">Đăng nhập</system:String>
    <system:String x:Key="createAccount">Tạo tài khoản</system:String>
    <system:String x:Key="forgetPassword">Quên mật khẩu</system:String>
    <system:String x:Key="mainTitle">Champion online - Lập trình bởi Kteam</system:String>
    <system:String x:Key="register">Đăng kí</system:String>
    <system:String x:Key="registerBaner">Tạo tài khoản</system:String>
    <system:String x:Key="registerCheckBoxPrivacy">Tôi trên 18 tuổi và tôi đồng ý những</system:String>
    <system:String x:Key="registerTermAndCondition">Điều khoản sử dụng</system:String>
    <system:String x:Key="registerAnd">và</system:String>
    <system:String x:Key="registerPrivacy">Thông tin bảo mật</system:String>
    <system:String x:Key="registerRegisterButton">Đăng kí</system:String>
    <system:String x:Key="registerAllFieldAreRequire">Tất cả các trường cần phải điền</system:String>
    <system:String x:Key="registerFirstName">Tên</system:String>
    <system:String x:Key="registerUserName">Tài khoản</system:String>
    <system:String x:Key="registerPassword">Mật khẩu</system:String>
    <system:String x:Key="registerReEnterPassword">Nhập lại mật khẩu</system:String>
    <system:String x:Key="registerLastName">Họ</system:String>
    <system:String x:Key="registerEmail">Địa chỉ Email</system:String>
    <system:String x:Key="registerCountry">Quốc gia</system:String>
    <system:String x:Key="registerDateOfBirth">Ngày sinh</system:String>
    <system:String x:Key="registerGender">Giới tính</system:String>
    <system:String x:Key="forgotPasswordTitle">Quên mật khẩu</system:String>
    <system:String x:Key="forgotPasswordBanner">Cấp lại mật khẩu</system:String>
    <system:String x:Key="forgotPasswordHint">Email hoặc Champion Online ID</system:String>
    <system:String x:Key="forgotPasswordButton">Cấp lại mật khẩu</system:String>
    <system:String x:Key="ok">Đồng ý</system:String>
    <system:String x:Key="language">Tiếng Việt</system:String>
</ResourceDictionary>

Resource\BaseResource.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:Quan_Ly_Ban_Hang.Resource">
    <Style TargetType="Image">
        <Setter Property="Cursor" Value="Hand"></Setter>
    </Style>
    
    <Style TargetType="Button"  x:Key="btnLoginStyle">
        <Setter Property="Background" Value="{DynamicResource btnLoginImgIdle}"></Setter>
        <Setter Property="BorderThickness" Value="0"></Setter>
        <Setter Property="Cursor" Value="Hand" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                    Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="{DynamicResource btnLoginImgHover}"/>           
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" Value="{DynamicResource btnLoginImgClick}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
</ResourceDictionary>

 

Chia sẻ:
Thảo luận Hỏi và đáp Báo lỗi bài viết
Hủy bỏ   hoặc  
Hủy bỏ   hoặc  
Hủy bỏ   hoặc  

Chiến dịch

Kteam - Howkteam Free Education