0

I somehow managed to make my company's website in 9 languages, using Visual Studio 2010 / VB / ASP.NET 4.0. I believe I'm using sessions. But you can tell for sure if you see the code provided below. I know this message is long, but I really need help.

It's a multilingual site, and I managed to put flags on the homepage. When I click a flag, the page's text changes to that language. When you click the French flag, it gets the information from the "FR" .resx resource file in my apps_GlobalResources folder. It stays on that language for the end-user's entire session. Great! Well, not so great.

The URL, for instance, about.aspx, remains about.aspx. Granted, the text changes to French, but I've been told that it's recommended to make it look like, if the client chooses French, for example, domain.com/about.aspx?lang=FR

But I have NO idea how to do this. If anybody can guide me clearly in the right direction, it'd be sincerely appreciated!!!

(For reference, if anybody is kind enough to delve into this issue, which may be a simple one, all of my files I've used in this are included below -- and they're kinda lengthy)

----Homepage.master (how I'm calling the languages -- just 2 samples)--

  <asp:LinkButton ID="LinkButton6" runat="server"
  CommandArgument="de" OnClick="RequestLanguageChange_Click"
  class="flagbutton">      
  <asp:Image ID="Image7" runat="server" ImageUrl="~/images/flagde.png"
  tooltip="View this website in Deutsch" title="View this website in Deutsch"/>
  <img class="map" src="images/flaghoverde.png" alt=""/>
  </asp:LinkButton>

  <asp:LinkButton ID="LinkButton5" runat="server"
  CommandArgument="fr" OnClick="RequestLanguageChange_Click"
  class="flagbutton">      
  <asp:Image ID="Image6" runat="server" ImageUrl="~/images/flagfr.png"
  tooltip="Voir ce site en français" title="Voir ce site en français"/>
  <img class="map" src="images/flaghoverfr.png" alt=""/>
  </asp:LinkButton>

---------------code behind homepage.master.vb---------------------

     Partial Public Class Homepage
    Inherits System.Web.UI.MasterPage
    Protected Sub Page_Load(sender As Object, e As EventArgs)
    End Sub

     Protected Sub RequestLanguageChange_Click(sender As Object, e As EventArgs)
    Dim senderLink As LinkButton = TryCast(sender, LinkButton)

    'store requested language as new culture in the session
    Session(Udev.MasterPageWithLocalization.Classes.Global.SESSION_KEY_CULTURE) = 
    senderLink.CommandArgument

    'reload last requested page with new culture
    Server.Transfer(Request.Path)
   End Sub
   End Class

---------------------BasePage.vb in App_Code folder--------------------

  Imports Microsoft.VisualBasic
  Imports System
  Imports System.Data
  Imports System.Configuration
  Imports System.Globalization
  Imports System.Threading
  Imports System.Web
  Imports System.Web.Security
  Imports System.Web.UI
  Imports System.Web.UI.WebControls
  Imports System.Web.UI.WebControls.WebParts
  Imports System.Web.UI.HtmlControls

  Namespace Udev.MasterPageWithLocalization.Classes
''' <summary>
''' Custom base page used for all web forms.
''' </summary>
Public Class BasePage
    Inherits Page
    Protected Overrides Sub InitializeCulture()
        'retrieve culture information from session
        Dim culture__1 As String =  
             Convert.ToString(Session([Global].SESSION_KEY_CULTURE))

        'check whether a culture is stored in the session
        If culture__1.Length > 0 Then
            Culture = culture__1
        End If

        'set culture to current thread
        Thread.CurrentThread.CurrentCulture =  
        CultureInfo.CreateSpecificCulture(culture__1)
        Thread.CurrentThread.CurrentUICulture = New CultureInfo(culture__1)

        'call base class
        MyBase.InitializeCulture()
    End Sub
   End Class
  End Namespace

----------------------Culture.vb in App_Code folder--------------------

  Imports Microsoft.VisualBasic
  Imports System
  Imports System.Data
  Imports System.Configuration
  Imports System.Web
  Imports System.Web.Security
  Imports System.Web.UI
  Imports System.Web.UI.WebControls
  Imports System.Web.UI.WebControls.WebParts
  Imports System.Web.UI.HtmlControls

  Namespace Udev.MasterPageWithLocalization.Classes
''' <summary>
''' This class provides ISO definitions for all cultures that are supported by this      
 application.
''' </summary>
Public Structure Culture
    'German - Switzerland definition
    Public Const DE As String = "de"
    'English - Great Britain definition
    Public Const EN As String = "en"
     End Structure
  End Namespace

------------------Global.vb in App_Code folder------------------------

  Imports System
  Imports System.Data
  Imports System.Configuration
  Imports System.Web
  Imports System.Web.Security
  Imports System.Web.UI
  Imports System.Web.UI.WebControls
  Imports System.Web.UI.WebControls.WebParts
  Imports System.Web.UI.HtmlControls

  Namespace Udev.MasterPageWithLocalization.Classes
''' <summary>
''' Summary description for Global
''' </summary>
Public Structure [Global]
    Public Const SESSION_KEY_CULTURE As String = "culture"
    End Structure
  End Namespace
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Jason Weber
  • 5,653
  • 6
  • 40
  • 73
  • 1
    http://stackoverflow.com/questions/8921758/how-does-internationalization-in-asp-net-work/8921792#8921792, http://stackoverflow.com/questions/2301352/what-should-i-know-and-consider-to-create-multi-language-web-site – Mitch Wheat Jan 30 '12 at 04:22
  • Andrew, I'm not intending to harm the forum in any way ... this forum has helped me more than I could possibly explain. I'm merely intimating to those who may respond to try to keep the language simple, as I may not understand syntax that's too complex, although I'm learning more each day. I'll take heed to your advice, however, and avoid that in the future. – Jason Weber Jan 30 '12 at 07:03
  • Thank you for the links, Mitch, but that doesn't really say how to add the querystrings like .../default.aspx?lang=ex-MX to the end of your URL, which is what I'm trying to do. But thank you for taking the time to respond. – Jason Weber Jan 30 '12 at 07:12
  • Got to ask, why would you want to do this? – tomfanning Jan 30 '12 at 08:23
  • @JasonWeber I don't think it's correct or helpful to ask for 'simple' language. You should, instead, hope for *accurate and correct* language, so that you can apply what you learn in the future. If people 'dumb down' what they tell you here, it won't give you any reference point for external information resources you may use. *You are using other resources to help you with your work, aren't you?* – Andrew Barber Jan 30 '12 at 10:09
  • Tom, many people on here have told me that it's preferable to have querystrings that depict the language after your URL -- not only for SE bots, but to help end-users realize they're in the /fr/ portion of the website, or the /de/ portion of the website. – Jason Weber Jan 30 '12 at 16:25

1 Answers1

1

Add something like this to your pages, at the top of OnInit (least preferred, as it means code-duplication). Ideally, this code would go to your base-page's oninit (preferred), or your master-pages's oninit (less preferred). Either way, it should work the way that you want.

if(this.Request.Querystring["lang"] == null)
{
    string path = this.Request.Url.AbsolutePath;
    string query = this.Request.Url.Query;
    if(query.length == 0)
        query = "?lang="; // concatenate language code here
    else
        query += "&lang="; // conc. lang. code here
    this.Response.Redirect(path + query, true);
    return;
}

This means: if the lang isn't in query, refresh the page, but this time with lang in query.

I hope I didn't misunderstand the question.


EDIT: VB version (oh, was it painful; I actually had to create a VB project for this):

Protected Overrides Sub OnInit(e As System.EventArgs)
    MyBase.OnInit(e)

    If Me.Request.QueryString("lang").Length = 0 Then
        Dim path As String = Me.Request.Url.AbsolutePath
        Dim query As String = Me.Request.Url.Query

        If query.Length = 0 Then
            query = "?lang=" ' + add lang here
        Else
            query += "&lang=" ' add lang
        End If

        Me.Response.Redirect(path + query, True)
        Return
    End If

End Sub

So, it looks like you don't have the OnOnit in your base class, but you can override it.

  • Thank you Hari! I'm going to try this ASAP! On my basepage.vb, where is the Onit? My basepage.vb is posted above in my question. This would be a lifesaver if it worked! Thanks again! – Jason Weber Jan 30 '12 at 08:25
  • I've searched through my entire solution, Hari -- I do appreciate your efforts, but I do not have any OnInit commands in any file. I replaced almost all system.web.ui inherits so that every page inherits basepage.vb. But in basepage.vb (posted above), there's no onit command, either. Again, thanks for the help, but I'm not sure how to implement your solution. – Jason Weber Jan 30 '12 at 16:34
  • I wrote that missing OnInit method for you - just paste the complete VB code from above to your BasePage class, right between lines `Inherits Page` and `Protected Overrides Sub InitializeCulture()`. Don't forget to concatenate the language code at those 2 marked places. –  Jan 30 '12 at 16:37
  • Thank you Hari! When I try to run this, it says: Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. – Jason Weber Jan 30 '12 at 16:52
  • If you have the C# version, I can convert it to VB, Hari. When I try to run your code through my VB verify tool, it says "EOF expected", so if you gave me the C# code, maybe I could use Teleflex to convert it to VB. Just a thought. Thanks again for all of your patience! – Jason Weber Jan 30 '12 at 17:03
  • Hari, I'm getting close! If you go to ussvision dot com, you'll see that the URL says lang= .... but doesn't say the language. But it's switching languages! – Jason Weber Jan 30 '12 at 17:28
  • I got it to say?lang=ru (for Russian) and whatever language, but when you go to another page, the "ru" disappears, and it just shows ?lang= .... – Jason Weber Jan 30 '12 at 17:34
  • I originally gave you c# version; you have both above. Make sure you paste it in BasePage.vb (between the 2 lines mentioned in my previous comment), make sure that you actually append the language code. If your default page (and all your pages) are inheriting from BasePage, then the code should work. The "object ref not set to instance" (briefly "null-ref" error) is generic, and w/o code trace, I cannot help you. It might be unrelated. Step through, put a breakpoint in the code that I gave you, make sure that it gets hit; watch for url changes and redirect. –  Jan 30 '12 at 18:46
  • Thanks for your help, Hari. It just says lang= in the URL, but I'll figure it out, I'm sure ... they don't want us to comment in here anymore since our discussion has gotten too long. But I sincerely appreciate all of your time and effort. Thanks again! – Jason Weber Jan 30 '12 at 22:37
  • Did you append lang code to those 2 places that I marked? I believe it'll be `"lang=" + Session(Udev.MasterPageWithLocalization.Classes.Global.SESSION_KEY_CULTURE)`, provided that second part does what I think it does. –  Jan 31 '12 at 04:21