开发者

I would like to have a spanish/english link on a master page that will take you to the current page in the other language and not to the 'home' page

I have a Visual Studio C# .NET project. I have two master templates. One for the original website in english. And a second master page for the Spanish version of the website. In both master pages, there's a link that says "English" (or Spanish). This link will take the user to the translation of the website in the other language.

But If I am in the "About Us" page in english, and I click in the SPANISH link, it will take me to the Home Page in spanish and not to the current page (in this case About Us) in spanish. How can I s开发者_如何转开发et the Link in the master page, to know what page translation i want ?

I have no idea how to code this thing.

Thanks.

SOLUTION - SOLVED

<script type="text/javascript">
        function SetLanguageLinkUrl() {
            var url = location.href;
            var newURL;

         // Create block of these, for each set of pages in different languages.
                if (url.indexOf("inicio") > 0)
                newURL = url.replace("/es/inicio.aspx", "/en/index.aspx");
                else if (url.indexOf("index") > 0)
                    newURL = url.replace("/en/index.aspx", "/es/inicio.aspx");

        // The Redirect
            window.location = newURL;

    }
</script>


Assuming you have an HtmlAnchor in your masterpage, you could replace the language folder in the link with the other language folder in your url.

void SetLanguageLinkUrl()
{
     string url = Request.Url.PathAndQuery.ToString().ToLowerInvariant();
     if(url.StartsWith("/en/"))
         languageLink.HRef = url.Replace("/en/", "/es/");
     else if(url.StartsWith("/es/"))
         languageLink.HRef = url.Replace("/es/", "/en/");
}

UPDATE

If you want to translate the url, you're maybe best having a dictionary of urls to use as a lookup table.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜