0

My issue: that I have two pages

1- page.aspx for English language mode

2- page-ar.aspx for Arabic language mode

And I have LinkeButton on click I pass session;

Session["lang"] = "ar";

or

Session["lang"] = "en";

What I need to get page.aspx name and add this string "-ar" to goto Arabic mode or to remove "-ar" from page-ar.aspx to goto English mode

And take in your consideration maybe there's some query string in pageurl.

4 Answers 4

2

You could use the following code in the code-behind page (e.g. Page_Load):

protected string LinkUrl;

protected void Page_Load(object sender, EventArgs e)
{
    var language = (string) Session["lang"] ?? "en";

    LinkUrl = (language == "ar")
                ? Page.ResolveUrl("~/page-ar.aspx")
                : Page.ResolveUrl("~/page.aspx");
}

On the page markup you can then place a link like this:

 <a href="<%= LinkUrl %>">Language Demo</a>
Sign up to request clarification or add additional context in comments.

1 Comment

Does this take querystring into consideration?
2

this the answer after the Mr/sheKhar help and with my own search

i have two button

one for Arabic and other for English mode

when user click on English button

    protected void english_Click(object sender, EventArgs e)
{
    string Path = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
    System.IO.FileInfo Info = new System.IO.FileInfo(Path);
    string pageName = Info.Name;

    if (Session["lang"].ToString() == "ar")
    {
        string enlink = pageName.Substring(0, pageName.Length - 8) + ".aspx";
        Session["lang"] = "en";

        var page = (Page)HttpContext.Current.CurrentHandler;
        string QueryString = page.ClientQueryString;
        if (!(string.IsNullOrEmpty(QueryString)))
        {
            Response.Redirect(enlink + "?" + QueryString);
        }
        else
        {
            Response.Redirect(enlink);
        }
    }

}

and when user click on arabic button

 protected void arabic_Click(object sender, EventArgs e)
{

    string Path = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
    System.IO.FileInfo Info = new System.IO.FileInfo(Path);
    string pageName = Info.Name;

    if (Session["lang"].ToString() == "en")
    {
        string arlink= pageName.Substring(0, pageName.Length - 5) + "-ar.aspx";
        Session["lang"] = "ar";
        //
        var page = (Page)HttpContext.Current.CurrentHandler;
        string QueryString = page.ClientQueryString; // this code get The Query String 

        if (!(string.IsNullOrEmpty(QueryString)))
        {
            Response.Redirect(arlink +"?"+ QueryString);
        }
        else
        {
            Response.Redirect(arlink);
        }
    }

}

wish this code helping someone :)

Comments

1
string url = HttpContext.Current.Request.Url.AbsoluteUri;
// http://localhost:1302/TESTERS/Default6.aspx

string path = HttpContext.Current.Request.Url.AbsolutePath;
// /TESTERS/Default6.aspx

string host = HttpContext.Current.Request.Url.Host;
// localhost

You can create a function which can return the current page name as follows

public string GetCurrentPageName()
{
    string Path = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
    System.IO.FileInfo Info = new System.IO.FileInfo(Path);
    string pageName = Info.Name;
    return pageName;
} 

Now finaly you can create a function and pass the session value as follows

 public string GetCurrentPageName(string fileName)
{
     string Path = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
    System.IO.FileInfo Info = new System.IO.FileInfo(Path);
    string pageName = Info.Name;
    if (fileName == "ar")
        return pageName.Substring(0, pageName.Length - 7) + ".aspx";
    else
        return pageName.Substring(0, pageName.Length - 5) + "_ar.aspx";    
 } 

Pass the session value in the above function and it will work.

Note:- I have not tested it.

Comments

1

If you are at the beginning of designing your system I would suggest you to use resource .resx files.

To make the UI multilanguage with resources check out this article: http://support.microsoft.com/kb/917414

For the data that you pull from the database you will have to use a lot of if statements.

If (arabic) {
     Select arabic data
}
else
{
     Select english data
}

If you consider to use resx files I can help you with more information.

With this solution there are no string manipulations and querystrings will be fine everything will work.

1 Comment

thanks man but i'm really my design was finished but i will need to use file resource.resx in the next project :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.