ASP.NET Tutorial/I18N/Resource file

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Explicit Localization Expressions

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblMessage.Visible = true;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Simple Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button
        id="btnSubmit"
        Text="Click Here!"
        OnClick="btnSubmit_Click"
        Runat="server" />
    <br /><br />
    <asp:Label
        id="lblMessage"
        Text="Thank You!"
        Visible="false"
        Runat="server" />
    </div>
    </form>
</body>
</html>
A localizable version of the above page.
<%@ Page Language="C#" UICulture="auto" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblMessage.Visible = true;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Localizable Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button
        id="btnSubmit"
        Text="<%$ Resources:ClickHere %>"
        OnClick="btnSubmit_Click"
        Runat="server" />
    <br /><br />
    <asp:Label
        id="lblMessage"
        Text="<%$ Resources:ThankYou %>"
        Visible="false"
        Runat="server" />
    </div>
    </form>
</body>
</html>
            
Associate a resource file with the page. 
All the resource files that you want to associate with a page must be added to a special folder named App_LocalResources. 
You create the App_LocalResources folder in the same folder as the page that you want to localize. 
You associate a resource file in the App_LocalResources folder with a particular page by using the following file naming convention:
page_name.[culture name].resx
For example, all the following resource files are associated with the LocalizablePage.aspx page:
LocalizablePage.aspx.resx
LocalizablePage.aspx.es-PR.resx
LocalizablePage.aspx.es.resx

The first resource file is the default resource file. 
Finally, the third resource file name includes the neutral culture name es (Spanish). 
If a user"s preferred language is Spanish, but not Puerto Rican Spanish, then the contents of this resource file are loaded.

File: App_LocalResources\LocalizablePage.aspx.es.resx
Name      Value
ClickHere  ccc
ThankYou  ttt


Implicit Localization Expressions

File: LocalizablePageImplicit.aspx
Always create a default localization file. 
If you don"t create a default localization file, other culture-specific localization files are ignored.

<%@ Page Language="C#" UICulture="auto" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblMessage.Visible = true;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Localizable Page Implicit</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button
        id="btnSubmit"
        meta:resourceKey="btnSubmit"
        Text="Click Me!"
        ToolTip="Click to show message"
        OnClick="btnSubmit_Click"
        Runat="server" />
    <br /><br />
    <asp:Label
        id="lblMessage"
        meta:resourceKey="lblMessage"
        Text="Thank You!"
        Visible="false"
        Runat="server" />
    </div>
    </form>
</body>
</html>

File: App_LocalResources\LocalizablePageImplicit.aspx.es.resx
Name                 Value
btnSubmit.Text       ttt
btnSubmit.ToolTip   tt
lblMessage.Text       t


Retrieving Local Resources Programmatically

File: ProgramLocal.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
    void Page_Load()
    {
        string welcomeMessage = (string)GetLocalResourceObject("welcomeMessage");
        lblMessage.Text = String.Format(welcomeMessage, "Steve");
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Program Local Resource</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label
        id="lblMessage"
        Runat="server" />
    </div>
    </form>
</body>
</html>
File: App_LocalResources\ProgramLocal.aspx.es.resx
Name          Value
welcomeMessage  Welcome {0} to our website!


Using Local Resources with Page Properties

<%@ Page Language="C#" UICulture="auto" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title><asp:Literal Text="<%$ Resources:Title %>" runat="Server" /></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Page Explicit Localization</h1>

    </div>
    </form>
</body>
</html>


You also can use implicit resource expressions when setting the page title.

<%@ Page Language="C#" UICulture="auto" meta:resourceKey="page" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Page Title</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Page Implicit Localization</h1>
    </div>
    </form>
</body>
</html>