locked
Toggle between French and English RRS feed

  • Question

  • User871198859 posted

    Hello everyone,

    I'm fairly new to ASP.NET, I've been reading some of the prior posts, but still confused on how to create a bilingual site. What I want to achieve is the following:

    I have a master page which ideally would contain a Link Button, that when a user clicks on it, would toggle/switch the language on both the master page and the page in the placecontentholder for example test.aspx.

     
    However, I'm not too sure how to achieve this or what is the best method to do so. Currently I have created resource files for my project (english and french).

     

    Any help would be much appreciated!

    Thank you

    Joe
     

    Saturday, August 4, 2007 2:12 PM

Answers

  • User871198859 posted

    Using reponse.redirect(request.url.tostring)  I've been able to refresh the content page, however, my master page is not refreshed.

    Does anyone know how to refresh the master page as well?

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 6, 2007 1:54 PM

All replies

  • User-319574463 posted

     Look at http://www.guysmithferrier.com/ and in particular at Guy Smith's book ISBN 0321341384. The book is available online at Safari books at http://safari.oreilly.com/0321341384

    Also look at http://www.asp.net/learn/videos/video-40.aspx - it is video on creating multi-lingual sites.

    You should switch between languages according to the users language preference set in his browser. 

    Saturday, August 4, 2007 2:33 PM
  • User871198859 posted

     I figured some of it out, but now I have the current problem:

    I currently retrieve the language in a cookie, when the cookie exists, the link button works like a charm, it does the switch.

    If the cookie does not exist, and I click on the toggle button, nothing happens. I have to click on it twice before the switch is done. 

     

    Any ideas?

    Joe 

    Saturday, August 4, 2007 4:54 PM
  • User764555827 posted

    Hi Joe,

    That bug is due to the fact that you set the new language after it has been used for the page. The easiest way to fix this is to cause a new processing of the page once you set a cookie.

    Here's a code example: 

            HttpContext context = HttpContext.Current;
            // Code where you set the cookie
            context.Server.Transfer(context.Request.Path); // Re-process current page from the start

    Hope this helps,

    Arnaud
     

    Sunday, August 5, 2007 5:50 PM
  • User-1841212424 posted

     You could create two directories, one in English and one in french and have two MasterPages that are similar exepct for the link...

    Sunday, August 5, 2007 6:03 PM
  • User871198859 posted

    Using reponse.redirect(request.url.tostring)  I've been able to refresh the content page, however, my master page is not refreshed.

    Does anyone know how to refresh the master page as well?

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 6, 2007 1:54 PM
  • User764555827 posted

     Can you post the code you use to do the translation, including the method it's in?

    Monday, August 6, 2007 2:40 PM
  • User871198859 posted

    Hello,

    Here are the 2 functions that I use:

    Public Function GetLanguage() As String

    Dim strLang As String

    If Not Request.Cookies("Language") Is Nothing Then

    strLang = Server.HtmlEncode(Request.Cookies("Language").Value)

     

    Else

    strLang = "en"

    Response.Cookies("Language").Value = strLang

    Response.Cookies("Language").Expires = DateTime.Now.AddDays(NB_DAYS_COOKIE)

    End If

    Return strLang

    End Function

    ------------------------------------------------------------------------------------------------------------------

    Public Sub ToggleLanguage()

    Dim strLang As String

    Dim mpReportLogo As Image

    Dim mpCompanyLogo As Image

    'get the controls from the master page

    mpReportLogo = CType(Master.FindControl("imgTitle"), Image)mpCompanyLogo = CType(Master.FindControl("imgRightLogo"), Image)

    strLang = GetLanguage() 'get the language as set in the cookie

    If strLang = "en" Then

    mpCompanyLogo.ImageUrl = "~/images/logo-en.jpg"

    mpReportLogo.ImageUrl = Server.MapPath("./images/") & "IT0007-head-left-en.jpg"

    ElseIf strLang = "fr" Then

    mpCompanyLogo.ImageUrl = "~/images/logo-fr.jpg"

    mpReportLogo.ImageUrl = Server.MapPath("./images/") & "IT0007-head-left-fr.jpg"

    End If

    'set the language in the cookie

    Response.Cookies("Language").Value = strLang

    Response.Cookies("Language").Expires = DateTime.Now.AddDays(NB_DAYS_COOKIE)

    Response.Redirect(Request.Url.ToString) 'refresh the page  --> this only refreshes the controls in the content holder and not the controls in the Master page

     

    Thanks!

     

    Monday, August 6, 2007 3:23 PM
  • User764555827 posted

    Alright, your code looks fine (by the way, the image source is localized, so you can place it as a resource instead of passing it in your code).

     Now, the problem surely comes from the fact that you don't call ToggleLanguage() from the right place. You need to call it from the InitializeCulture() method, which is an overload of the base Page.

     

    Monday, August 6, 2007 4:10 PM
  • User871198859 posted

    Hi Aweil,

     

    1. The toggle is responsible for the switch, so I only call it when the user chooses to switch the language to english or french. 

    In my Protect Sub Overrides Sub InitializeCulture() I have the following:

    Me.UICULTURE = getlanguage()
    MYBase.InitiliazeCulture()
     

    2. As for the images, I have simply hard coded them for the time being, but how would I incorporate them in my resource file?  My goal would be when the user clicks on the link, it changes the logos as well as the language. The only problem i have is that the logos are located in a master page and as I'm new to .net, not to sure how to do this.

     

    Thanks for your help! 

     

     

    Monday, August 6, 2007 7:34 PM
  • User871198859 posted

    Hello,

     I managed to get everything working. However, I've created the toggle button on the conten page, and I think it would be a better idea to the language/toggle button on the master page. However, I'm not too sure how to achieve this.

    The problems I have right now are:

    1. I'm only allowed to have one server-side Form tag, how would I create an asplink button in this case on my master page and still have a form tag on my content pages?

    2. If I move the language code to the master page, how do I tell which resource file to use, or is this done automatically?

     

    Thanks a lot!

    Monday, August 13, 2007 8:12 AM