页面中某些超链接控件上的本地化资源未发生变化

发布于 2024-11-09 00:44:36 字数 2927 浏览 4 评论 0原文

在此处输入图像描述

上面是正在开发的站点的屏幕截图...

我们有一个 DropdownList 控件,并在其 SelectedIndexChanged 上回发,然后我们更改站点文化,然后加载相应的资源文件。

DropDownList ASP.NET代码

<asp:DropDownList ID="ddlLanguage" runat="server" CssClass="select-language" AutoPostBack="true">
       <asp:ListItem Value="en-US" Text="English" title="/images/Flag_USA.gif"></asp:ListItem>
       <asp:ListItem Value="it-IT" Text="Italiano" title="/images/Flag_Italian.gif"></asp:ListItem>
       <asp:ListItem Value="fr-FR" Text="Française" title="/images/Flag_French.gif"></asp:ListItem>
</asp:DropDownList>

所有网页继承的公共类

using System;
using System.Web;
using System.Threading;
using System.Globalization;

public class languagebase : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        try
        {
            string LanguageCode = Request["ctl00$ucMenu$ddlLanguage"]; // Language Drop Down Control in Front End
            if (!LanguageCode.IsNullOrEmpty())
            {
                setCulture(LanguageCode);  // Set Culture language from drop down
                Request.Cookies["LanguageCode"].Value = LanguageCode; // Update REQUEST Cookie language from drop down
                SetCookies(LanguageCode); // Set Cookie language from drop down
            }
        }
        catch(Exception ex)
        {
            setCulture("en-US"); // Set default language 
            Request.Cookies["LanguageCode"].Value = "en-US"; // Update REQUEST Cookie language to default
            SetCookies("en-US"); // Set default language 
        }
        base.InitializeCulture();
    }

    private static void setCulture(string LanguageValue)
    {
       Thread.CurrentThread.CurrentUICulture = new CultureInfo(LanguageValue);
       Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(LanguageValue);
    }

    public void SetCookies(string strLanguage)
    {
        System.Web.HttpContext.Current.Response.Cookies["LanguageCode"].Value = strLanguage;
        System.Web.HttpContext.Current.Response.Cookies["LanguageCode"].Expires = DateTime.Now.AddDays(15);
    }
}

扩展方法(仅供参考)

public static Boolean IsNullOrEmpty(this String original)
{
    return string.IsNullOrEmpty(original);
}

示例控件哪个本地化资源未加载

<li>
     <img src="/images/my-listing.png" alt="" align="absmiddle" /><asp:HyperLink ID="hlnkMyProperties" runat="server" meta:resourcekey="hlnkMyProperties"></asp:HyperLink>
</li>

手头的问题

<块引用>

在这里,如果我从浏览器的地址栏重新加载页面,则资源会正确加载。 我只是想像在切换文化时某些控件如何可能具有英语资源而某些控件具有旧的意大利语资源?

我希望我已经解释得很好了。

Enter image description here

Above is the screenshot of the site in development...

We have a DropdownList control and on its SelectedIndexChanged it postbacks, and we then change the site culture and it then loads the respective resources files.

DropDownList ASP.NET Code

<asp:DropDownList ID="ddlLanguage" runat="server" CssClass="select-language" AutoPostBack="true">
       <asp:ListItem Value="en-US" Text="English" title="/images/Flag_USA.gif"></asp:ListItem>
       <asp:ListItem Value="it-IT" Text="Italiano" title="/images/Flag_Italian.gif"></asp:ListItem>
       <asp:ListItem Value="fr-FR" Text="Française" title="/images/Flag_French.gif"></asp:ListItem>
</asp:DropDownList>

Common class inherited by all of the web pages

using System;
using System.Web;
using System.Threading;
using System.Globalization;

public class languagebase : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        try
        {
            string LanguageCode = Request["ctl00$ucMenu$ddlLanguage"]; // Language Drop Down Control in Front End
            if (!LanguageCode.IsNullOrEmpty())
            {
                setCulture(LanguageCode);  // Set Culture language from drop down
                Request.Cookies["LanguageCode"].Value = LanguageCode; // Update REQUEST Cookie language from drop down
                SetCookies(LanguageCode); // Set Cookie language from drop down
            }
        }
        catch(Exception ex)
        {
            setCulture("en-US"); // Set default language 
            Request.Cookies["LanguageCode"].Value = "en-US"; // Update REQUEST Cookie language to default
            SetCookies("en-US"); // Set default language 
        }
        base.InitializeCulture();
    }

    private static void setCulture(string LanguageValue)
    {
       Thread.CurrentThread.CurrentUICulture = new CultureInfo(LanguageValue);
       Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(LanguageValue);
    }

    public void SetCookies(string strLanguage)
    {
        System.Web.HttpContext.Current.Response.Cookies["LanguageCode"].Value = strLanguage;
        System.Web.HttpContext.Current.Response.Cookies["LanguageCode"].Expires = DateTime.Now.AddDays(15);
    }
}

Extension Method (just for sake of info)

public static Boolean IsNullOrEmpty(this String original)
{
    return string.IsNullOrEmpty(original);
}

Example Control on which Localized Resource is not loading

<li>
     <img src="/images/my-listing.png" alt="" align="absmiddle" /><asp:HyperLink ID="hlnkMyProperties" runat="server" meta:resourcekey="hlnkMyProperties"></asp:HyperLink>
</li>

Problem at hand

Here, if I reload the page from the browser's addressbar then resources are loaded correctly.
I am just imaging how it is possible that the certain controls have English resources and some have old Italian resources when switching the culture?

I hope I have explained it well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

写给空气的情书 2024-11-16 00:44:37

我将 HyperLink 控件更改为以下语法,瞧!现在正在工作。

<li>
    <img src="/images/my-listing.png" alt="" align="absmiddle" /><a ID="hlnkMyProperties" runat="server"><%= GetLocalResourceObject("hlnkMyProperties")%></a>
</li>

I changed the HyperLink control to the following syntax and voila! It's working now.

<li>
    <img src="/images/my-listing.png" alt="" align="absmiddle" /><a ID="hlnkMyProperties" runat="server"><%= GetLocalResourceObject("hlnkMyProperties")%></a>
</li>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文