Today
-
Yesterday
-
Total
-

ABOUT ME

-

방문자

  • [WPF] 지역화 (다중 언어)
    Microsoft .NET/WPF 2020. 12. 7. 18:43

    본 예제는 resource에 국가별 언어를 미리 등록해두고

    설정(Language Culture)에 따라 표출되는 언어가 변경되도록 하는 예제입니다.

    Git - https://github.com/jeseok-Mun/WPF_Example_MultiLanguage

     

    참고
    PC 언어 코드 System.Globalization.CultureInfo.CurrentCulture

     

    국가별 언어 코드(Language Culture)

    더보기
    Code Name
    af Afrikaans
    af-ZA Afrikaans (South Africa)
    ar Arabic
    ar-AE Arabic (U.A.E.)
    ar-BH Arabic (Bahrain)
    ar-DZ Arabic (Algeria)
    ar-EG Arabic (Egypt)
    ar-IQ Arabic (Iraq)
    ar-JO Arabic (Jordan)
    ar-KW Arabic (Kuwait)
    ar-LB Arabic (Lebanon)
    ar-LY Arabic (Libya)
    ar-MA Arabic (Morocco)
    ar-OM Arabic (Oman)
    ar-QA Arabic (Qatar)
    ar-SA Arabic (Saudi Arabia)
    ar-SY Arabic (Syria)
    ar-TN Arabic (Tunisia)
    ar-YE Arabic (Yemen)
    az Azeri (Latin)
    az-AZ Azeri (Latin) (Azerbaijan)
    az-AZ Azeri (Cyrillic) (Azerbaijan)
    be Belarusian
    be-BY Belarusian (Belarus)
    bg Bulgarian
    bg-BG Bulgarian (Bulgaria)
    bs-BA Bosnian (Bosnia and Herzegovina)
    ca Catalan
    ca-ES Catalan (Spain)
    cs Czech
    cs-CZ Czech (Czech Republic)
    cy Welsh
    cy-GB Welsh (United Kingdom)
    da Danish
    da-DK Danish (Denmark)
    de German
    de-AT German (Austria)
    de-CH German (Switzerland)
    de-DE German (Germany)
    de-LI German (Liechtenstein)
    de-LU German (Luxembourg)
    dv Divehi
    dv-MV Divehi (Maldives)
    el Greek
    el-GR Greek (Greece)
    en English
    en-AU English (Australia)
    en-BZ English (Belize)
    en-CA English (Canada)
    en-CB English (Caribbean)
    en-GB English (United Kingdom)
    en-IE English (Ireland)
    en-JM English (Jamaica)
    en-NZ English (New Zealand)
    en-PH English (Republic of the Philippines)
    en-TT English (Trinidad and Tobago)
    en-US English (United States)
    en-ZA English (South Africa)
    en-ZW English (Zimbabwe)
    eo Esperanto
    es Spanish
    es-AR Spanish (Argentina)
    es-BO Spanish (Bolivia)
    es-CL Spanish (Chile)
    es-CO Spanish (Colombia)
    es-CR Spanish (Costa Rica)
    es-DO Spanish (Dominican Republic)
    es-EC Spanish (Ecuador)
    es-ES Spanish (Castilian)
    es-ES Spanish (Spain)
    es-GT Spanish (Guatemala)
    es-HN Spanish (Honduras)
    es-MX Spanish (Mexico)
    es-NI Spanish (Nicaragua)
    es-PA Spanish (Panama)
    es-PE Spanish (Peru)
    es-PR Spanish (Puerto Rico)
    es-PY Spanish (Paraguay)
    es-SV Spanish (El Salvador)
    es-UY Spanish (Uruguay)
    es-VE Spanish (Venezuela)
    et Estonian
    et-EE Estonian (Estonia)
    eu Basque
    eu-ES Basque (Spain)
    fa Farsi
    fa-IR Farsi (Iran)
    fi Finnish
    fi-FI Finnish (Finland)
    fo Faroese
    fo-FO Faroese (Faroe Islands)
    fr French
    fr-BE French (Belgium)
    fr-CA French (Canada)
    fr-CH French (Switzerland)
    fr-FR French (France)
    fr-LU French (Luxembourg)
    fr-MC French (Principality of Monaco)
    gl Galician
    gl-ES Galician (Spain)
    gu Gujarati
    gu-IN Gujarati (India)
    he Hebrew
    he-IL Hebrew (Israel)
    hi Hindi
    hi-IN Hindi (India)
    hr Croatian
    hr-BA Croatian (Bosnia and Herzegovina)
    hr-HR Croatian (Croatia)
    hu Hungarian
    hu-HU Hungarian (Hungary)
    hy Armenian
    hy-AM Armenian (Armenia)
    id Indonesian
    id-ID Indonesian (Indonesia)
    is Icelandic
    is-IS Icelandic (Iceland)
    it Italian
    it-CH Italian (Switzerland)
    it-IT Italian (Italy)
    ja Japanese
    ja-JP Japanese (Japan)
    ka Georgian
    ka-GE Georgian (Georgia)
    kk Kazakh
    kk-KZ Kazakh (Kazakhstan)
    kn Kannada
    kn-IN Kannada (India)
    ko Korean
    ko-KR Korean (Korea)
    kok Konkani
    kok-IN Konkani (India)
    ky Kyrgyz
    ky-KG Kyrgyz (Kyrgyzstan)
    lt Lithuanian
    lt-LT Lithuanian (Lithuania)
    lv Latvian
    lv-LV Latvian (Latvia)
    mi Maori
    mi-NZ Maori (New Zealand)
    mk FYRO Macedonian
    mk-MK FYRO Macedonian (Former Yugoslav Republic of Macedonia)
    mn Mongolian
    mn-MN Mongolian (Mongolia)
    mr Marathi
    mr-IN Marathi (India)
    ms Malay
    ms-BN Malay (Brunei Darussalam)
    ms-MY Malay (Malaysia)
    mt Maltese
    mt-MT Maltese (Malta)
    nb Norwegian (Bokm?l)
    nb-NO Norwegian (Bokm?l) (Norway)
    nl Dutch
    nl-BE Dutch (Belgium)
    nl-NL Dutch (Netherlands)
    nn-NO Norwegian (Nynorsk) (Norway)
    ns Northern Sotho
    ns-ZA Northern Sotho (South Africa)
    pa Punjabi
    pa-IN Punjabi (India)
    pl Polish
    pl-PL Polish (Poland)
    ps Pashto
    ps-AR Pashto (Afghanistan)
    pt Portuguese
    pt-BR Portuguese (Brazil)
    pt-PT Portuguese (Portugal)
    qu Quechua
    qu-BO Quechua (Bolivia)
    qu-EC Quechua (Ecuador)
    qu-PE Quechua (Peru)
    ro Romanian
    ro-RO Romanian (Romania)
    ru Russian
    ru-RU Russian (Russia)
    sa Sanskrit
    sa-IN Sanskrit (India)
    se Sami (Northern)
    se-FI Sami (Northern) (Finland)
    se-FI Sami (Skolt) (Finland)
    se-FI Sami (Inari) (Finland)
    se-NO Sami (Northern) (Norway)
    se-NO Sami (Lule) (Norway)
    se-NO Sami (Southern) (Norway)
    se-SE Sami (Northern) (Sweden)
    se-SE Sami (Lule) (Sweden)
    se-SE Sami (Southern) (Sweden)
    sk Slovak
    sk-SK Slovak (Slovakia)
    sl Slovenian
    sl-SI Slovenian (Slovenia)
    sq Albanian
    sq-AL Albanian (Albania)
    sr-BA Serbian (Latin) (Bosnia and Herzegovina)
    sr-BA Serbian (Cyrillic) (Bosnia and Herzegovina)
    sr-SP Serbian (Latin) (Serbia and Montenegro)
    sr-SP Serbian (Cyrillic) (Serbia and Montenegro)
    sv Swedish
    sv-FI Swedish (Finland)
    sv-SE Swedish (Sweden)
    sw Swahili
    sw-KE Swahili (Kenya)
    syr Syriac
    syr-SY Syriac (Syria)
    ta Tamil
    ta-IN Tamil (India)
    te Telugu
    te-IN Telugu (India)
    th Thai
    th-TH Thai (Thailand)
    tl Tagalog
    tl-PH Tagalog (Philippines)
    tn Tswana
    tn-ZA Tswana (South Africa)
    tr Turkish
    tr-TR Turkish (Turkey)
    tt Tatar
    tt-RU Tatar (Russia)
    ts Tsonga
    uk Ukrainian
    uk-UA Ukrainian (Ukraine)
    ur Urdu
    ur-PK Urdu (Islamic Republic of Pakistan)
    uz Uzbek (Latin)
    uz-UZ Uzbek (Latin) (Uzbekistan)
    uz-UZ Uzbek (Cyrillic) (Uzbekistan)
    vi Vietnamese
    vi-VN Vietnamese (Viet Nam)
    xh Xhosa
    xh-ZA Xhosa (South Africa)
    zh Chinese
    zh-CN Chinese (S)
    zh-HK Chinese (Hong Kong)
    zh-MO Chinese (Macau)
    zh-SG Chinese (Singapore)
    zh-TW Chinese (T)
    zu Zulu
    zu-ZA Zulu (South Africa)

    우선 프로젝트에 "Localization"폴더를 생성하고 생성된 폴더에 리소스 "res.resx"파일을 추가합니다.


    생성된 "res.resx"파일은 기본이 되는 파일로 기본값인 "en-US"라고 생각하시면 됩니다.


    "res.resx"파일을 열어 액세스 한정자를 public으로 변경하고 영문 콘텐츠를 입력합니다.


    이제 "Localization"폴더에 "res.ko-KR.resx"를 추가하고 액세스 한정자를 public으로 변경하고

    콘텐츠를 입력합니다.

    "res.ko-KR.resx"를 추가 했다면 PC 설정이 대한민국이라면 한글로 표시됩니다. 


    값 확인을 위해 "MainWindow.xaml"에 출력을 합시다.

    우선 데이터 참조를 위해 Localization을 참조합니다.

    xmlns:localization="clr-namespace:WPF_Multi_Language_Sample.Localization"

    <Window x:Class="WPF_Multi_Language_Sample.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:localization="clr-namespace:WPF_Multi_Language_Sample.Localization"
            mc:Ignorable="d"
            Title="MainWindow" 
            Height="450" Width="800">
        <Grid>
            <TextBox Text="{x:Static localization:res.apple}"/>
        </Grid>
    </Window>

    ko-KR
    en-US

    여기까지만 설정한다면 PC 국가 설정에 따라 자동으로 언어가 변경됩니다.


    언어 선택 및 변경하려면 아래 예제 참고.

    국가 코드를 선택하면 프로그램이 다시 시작되며 선택한 언어로 재구성되는 예제 

     

    선택한 언어 코드가 저장될 공간 생성 ( 프로젝트 속성 - 설정 )


    언어 코드가 선택가능한 UI 구성 ( MainWindow.xaml )

    <Window x:Class="WPF_Multi_Language_Sample.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            xmlns:localization="clr-namespace:WPF_Multi_Language_Sample.Localization"
            mc:Ignorable="d"
            Title="MainWindow" 
            Height="450" Width="800">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="30"/>
                <RowDefinition Height="30"/>
                <RowDefinition Height="1*"/>
            </Grid.RowDefinitions>
            <TextBox Text="{x:Static localization:res.apple}"/>
            <ComboBox x:Name="cbLanguage"
                      Grid.Row="1"
                      SelectionChanged="cbLanguage_SelectionChanged">
                <sys:String>ko_KR</sys:String>
                <sys:String>en_US</sys:String>
            </ComboBox>
        </Grid>
    </Window>

    코드 ( MainWindow.xaml.cs )

    using System.Windows;
    
    namespace WPF_Multi_Language_Sample
    {
        /// <summary>
        /// MainWindow.xaml에 대한 상호 작용 논리
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                //저장된 정보로 세팅 (저장된 값이 없으면 PC국가 설정값을 기본으로 함)
                Localization.res.Culture = Properties.Settings.Default.language != string.Empty ? 
                                            new System.Globalization.CultureInfo(Properties.Settings.Default.language) : System.Globalization.CultureInfo.CurrentCulture;
    
                InitializeComponent();
            }
    
            private void cbLanguage_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
            {
                //선택 정보 불러오기
                string selected = (string)((System.Windows.Controls.ComboBox)sender).SelectedItem;
    
                //선택 정보 저장하기
                Properties.Settings.Default.language = selected;
                Properties.Settings.Default.Save();
                
                //새 프로그램 실행
                System.Diagnostics.Process.Start(Application.ResourceAssembly.Location);
    
                //현재 프로그램 종료
                Application.Current.Shutdown();
    
            }
        }
    }


Posted by virtualgiraffe.