locked
Web Pages Labels Tanslation RRS feed

  • Question

  • User-1193741727 posted

    How to translate web pages labels? For example if User A selects English Language while logging to the web application then all subsequent web pages labels for that user needs to be shown English. If User B selects French Language then labels should show in French. How to tackle this scenario? What are the options availble?

    TIA

    Tuesday, May 17, 2011 1:25 PM

Answers

  • User-1632285901 posted

    hi

     

    first u add for example image button with flag( just an idea for u ) just like that one for english one for french

     

     <asp:ImageButton ID="ImageButton2"   runat="server" OnClick="ImageButton2_Click"  ImageUrl="~/images/flags/flag_france.png" />

    the code of the imagebutton is 

     Protected Sub ImageButton2_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton2.Click
            Dim aCookie As New HttpCookie("langue")
            aCookie.Value = "fr-FR"
            aCookie.Expires = DateTime.Now.AddDays(1)
            Response.Cookies.Add(aCookie)
            ConfigurationManager.AppSettings("profile") = "fr-FR"
            Dim _Culture As String
            Session("Culture") = "fr"
            Me.Response.Redirect(Me.Request.AppRelativeCurrentExecutionFilePath)
            _Culture = Session("Culture").ToString().ToUpper()
            System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(_Culture)
            System.Threading.Thread.CurrentThread.CurrentUICulture = New CultureInfo(_Culture)
    
        End Sub

    and u need a detection in the page load of the language by this code

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
    
    
            Try
                Dim _Culture As String
                _Culture = Session("Culture").ToString().ToUpper()
                System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(_Culture)
                System.Threading.Thread.CurrentThread.CurrentUICulture = New CultureInfo(_Culture)
    
            Catch
    
            End Try
    
        End Sub

    and for english for sure u add another image button and another event onclik of the engloish button and a .resx file for every language 

    hope it helps

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 26, 2011 11:52 AM

All replies

  • User-1360095595 posted
    Resource files: http://msdn.microsoft.com/en-us/library/fw69ke6f(v=VS.100).aspx
    Tuesday, May 17, 2011 1:31 PM
  • User-1193741727 posted

    thanks for your answer.

    As I can see it depends on the browser sending the language and the Page using the culture for that language. That means the browser needs to have all the languages (in the Language preference) my web application can support so that it can send it. For example if my web application can support English and French, then I need to make sure that all the browsers accessing my web application has en-Us and fr-FR in the Langauge preference in the browser. Thats not possible.

    Is there any solution that does not require my web application users to have multiple Languages in their browser's Language preference, and still can access the web application for different languages? Users will be selecting Language from a drop down on the Login page and I am thinking of storing it in Session when a user successfully logged in and then use that Language in Session for the culture for all the subsequent pages. Is it possible?

    Tuesday, May 24, 2011 1:35 PM
  • User-1632285901 posted

    hi

     

    first u add for example image button with flag( just an idea for u ) just like that one for english one for french

     

     <asp:ImageButton ID="ImageButton2"   runat="server" OnClick="ImageButton2_Click"  ImageUrl="~/images/flags/flag_france.png" />

    the code of the imagebutton is 

     Protected Sub ImageButton2_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton2.Click
            Dim aCookie As New HttpCookie("langue")
            aCookie.Value = "fr-FR"
            aCookie.Expires = DateTime.Now.AddDays(1)
            Response.Cookies.Add(aCookie)
            ConfigurationManager.AppSettings("profile") = "fr-FR"
            Dim _Culture As String
            Session("Culture") = "fr"
            Me.Response.Redirect(Me.Request.AppRelativeCurrentExecutionFilePath)
            _Culture = Session("Culture").ToString().ToUpper()
            System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(_Culture)
            System.Threading.Thread.CurrentThread.CurrentUICulture = New CultureInfo(_Culture)
    
        End Sub

    and u need a detection in the page load of the language by this code

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
    
    
            Try
                Dim _Culture As String
                _Culture = Session("Culture").ToString().ToUpper()
                System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(_Culture)
                System.Threading.Thread.CurrentThread.CurrentUICulture = New CultureInfo(_Culture)
    
            Catch
    
            End Try
    
        End Sub

    and for english for sure u add another image button and another event onclik of the engloish button and a .resx file for every language 

    hope it helps

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 26, 2011 11:52 AM
  • User2021055510 posted

    Hi lovhassan,

                  Have the resource files in all languages which you want to include. You can use the meta:resourcekey attribute for applying text from resource files. The resource file name must be filename.aspx.culture.resx, where culture may be en-US or hi-IN, etc...

    For example,

    <asp:Label ID="lbNote" runat="server" Text="" meta:resourcekey="lbNoteResource1"></asp:Label>

    You must have the property in the resource file like "lbNoteResource1.Text"

     

    To apply the culture you must override the InitializeCulture event of the page class..

    protected override void InitializeCulture()

    {

    base.InitializeCulture();

    string language = GetLanguage();

    this.UICulture = language;

    this.Culture = language;

    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);

    Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);

    }

     

    Regards,

    Tamilmani.

    Saturday, May 28, 2011 12:23 AM
  • User-1632285901 posted

    hi tamilmani

    yes for sure the resource key must be added in every text label in addition to the code that i posted 

     

    like that

     <asp:Label ID="Label4" runat="server" Text="<%$ Resources:traduction,Francais%>" 

    Regards

     

    Saturday, May 28, 2011 5:13 AM