본문 바로가기

Windows Developer/Asp.net

[asp.net] 다국어 설정


위와 같이 기본 폼 세팅을 마친 후에 로컬 리소스를 생성한다 
그럼 Default.aspx.resx 리소스 파일이 생성되고 한글과 미국 두 언어를 하기 위해서 
Default.aspx.en-US.resx을 만들었다 (en-US는 미국 국가 설정에 맞는 확장명이다)




위에는 Defaulst.aspx.resx파일이고 밑에는 영문을 위한 Default.aspx.en-US.resx의 내용을 
언어 세팅을 해준다.



위와 같이 세팅을 마친후에
Default.aspx에 코드 부분을 다음과 같이 작성해 주자

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="다국어_테스트._Default" culture="auto" meta:resourcekey="PageResource1" uiculture="auto" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<script runat="server">
    Protected Overrides Sub InitializeCulture()
        If Request.Form("Listbox1") IsNot Nothing Then
            Dim selectedLanguage As [String] = Request.Form("ListBox1")
            UICulture = selectedLanguage
            Culture = selectedLanguage
            
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(selectedLanguage)
            System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo(selectedLanguage)
        End If
        MyBase.InitializeCulture()
    End Sub
</script>


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:ListBox ID="ListBox1" runat="server" meta:resourcekey="ListBox1Resource1">
            <asp:ListItem meta:resourcekey="ListItemResource1" Value="ko-KR">한국</asp:ListItem>
            <asp:ListItem meta:resourcekey="ListItemResource2" Value="en-US">미국</asp:ListItem>
        </asp:ListBox>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Label ID="Label1" runat="server" meta:resourcekey="Label1Resource1" 
            Text="한글"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" meta:resourcekey="Button1Resource1" 
            Text="버튼" />
    
    </div>
    </form>
</body>
</html>

진하게 표시된 부분은 리스트 박스에서 선택된 언어에 맞게 InitializeCulture() 를 재정의 해준다



결과는 다음과 같다

여기서 위에 InitializeCulture() 의 내용을 버튼 클릭 이벤트에 넣어서 생성한다면 결과는 한글밖에 지원이 되지 않는다.
꼭 InitializeCulture()를 재정의 해줘야 한다.