[RESOLVED] 2 questions regarding resource pages

Hi - I;ve not user resource pages in .net before but have been tasked with converting an existing app to make it multilingual. I have two specific questions about getting it working how I want.

1. Explicitly setting the culture: I don't want to rely on the browser to set the culture for my users I want to do it via code. I've tried to do this within the Page_Load method and have tried a couple of different ways, none of which seemed to have worked:

System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-FR");

Page.Culture = "fr-FR";

The page is still displaying data from the en-GB file I have (which is what my browser is set to).

2: I have some static text in my HTML that I would like to make multi-lingual. I have tried using the following server side control to achieve this but it always displays what is in the "Text" attribute rather than looking up the value "LiteralResource1" from the page's resource file

        <asp:Localize runat="server"
            ID="WelcomeMessage"
            Text="Welcome!" meta:resourcekey="LiteralResource1" />
I have gone through a few of Microsoft's help pages and examples for this but just can't seem to get it to work so any help would be greatly appreciated.

Thanks,

 Brian

 

 

Aspx Code: 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ResourceExample.aspx.cs"
    Inherits="SampleWeb.ResourceExample" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Multi langauge example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table border="1px">
            <tr>
                <td style="width: 120px" align="right">
                    Change langauge :</td>
                <td>
                    <asp:DropDownList ID="ddlLanguage" runat="server" AutoPostBack="True">
                        <asp:ListItem Value="Auto">Auto</asp:ListItem>
                        <asp:ListItem Value="ar-EG">Arabic</asp:ListItem>
                        <asp:ListItem Value="de-DE">German</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td align="right">
                    Back :
                </td>
                <td>
                    <asp:Button ID="btnBack" runat="server" Text="<%$ Resources:MyResource, ButtonBack %>" />
                </td>
            </tr>
            <tr>
                <td align="right">
                    Delete :
                </td>
                <td>
                    <asp:Button ID="btnDelete" runat="server" Text="<%$ Resources:MyResource, ButtonDelete %>" />
                </td>
            </tr>
            <tr>
                <td align="right">
                    Ok :
                </td>
                <td>
                    <asp:Button ID="btnOk" runat="server" Text="<%$ Resources:MyResource, ButtonOk %>" />
                </td>
            </tr>
            <tr>
                <td align="right">
                    Cancel :
                </td>
                <td>
                    <asp:Button ID="btnCancel" runat="server" Text="<%$ Resources:MyResource, ButtonCancel %> " />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

 Code Behind:

using System.Threading;
using System.Globalization;

namespace SampleWeb
{
    public partial class ResourceExample : System.Web.UI.Page
    {
        protected override void InitializeCulture()
        {
            string strlang = Request["ddlLanguage"];

            if (!string.IsNullOrEmpty(strlang))
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(strlang);
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(strlang);
            }
        }
    }
}


Apart from the above files, I have added resource files for each langauges with Name and value.

Hope the above code will solves your first problem.

Thanks for your reply - I actually found a way of getting it to work without including C# code in the page itself.

What I was doing wrong is you need to override the InitializeCulture method and change the culture there rather than in Page_Load.

Also I wasn't including the .text in my keynames in the resource file which is why asp:Localize wasn't working :)

thoughts on "[RESOLVED] 2 questions regarding resource pages"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>