locked
How can i change current culture of my content page. RRS feed

  • Question

  • User1493959435 posted

    hi,

    i have three master pages and some contents pages in my project.

    in content page i have wrote below script:

    <script runat="server">
            
            Dim strBrowser As String
            
            Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
                    
                Dim curlang1 As String = ""
                
                curlang1 = (Thread.CurrentThread.CurrentCulture).TwoLetterISOLanguageName
                
                If curlang1 = "en" Then
                    
                    cont2img.ImageUrl = "../eng/images/contactus_banner.png"
                    cont2img.Width = 698
                    cont2img.Height = 180
                    entext.Visible = True '---> English content div         
               
                ElseIf curlang1.ToString = "pl" Then
               
                    cont2img.ImageUrl = "../Common/images/contactus_banner.jpg"
                    cont2img.Width = 950
                    cont2img.Height = 133
                    pltext.Visible = True '--->Polish content div
                End If
                
                        
            End Sub
            
            Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs)
                Dim curlang2 As String = ""
                
                Try
                    'Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages(0))   ''Get the language from Tools - Internet Option - Language Setting
                    
                    curlang2 = (Thread.CurrentThread.CurrentCulture).TwoLetterISOLanguageName
                    
                    If curlang2 = "en" Then
                        Page.MasterPageFile = "../eng/MasterPageIP.master"
                        Page.Title = "Contact Us"
                    ElseIf curlang2 = "pl" Then
                        Page.MasterPageFile = "../pol/MasterPageIP.master"
                        Page.Title = "Kontakt"
                    End If
                Catch ex As NullReferenceException
                    Response.Redirect("../eng/index.aspx")
                End Try
                
            End Sub
        </script>

    And i have a language flags in my master page. so i have created image button for language flag to perform on click action. where On click of language flag (polish) i like to change the content (English) of my page as per the selected flag click.

    hence i have wrote in master page:

    <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="../common/images/pol.gif" AlternateText="Polish" />


    And in code behind --- ImageButton1_Click

    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("pl-PL")
    Thread.CurrentThread.CurrentUICulture = New CultureInfo("pl-PL")

    Response.Write(Thread.CurrentThread.CurrentCulture)

    Dim geturl As String

            geturl = Request.Url.ToString()
            Response.Redirect(geturl)

    here the geturl is print with "pl-PL" but the page content is printed with English language.

    I want to change page content also as per flag seletion.


    Can anyone let me know / guide me about my query. Please reply soon.


    With Regards,

    RS-MCA




    Monday, November 29, 2010 1:55 AM

Answers

  • User-242433875 posted

    @RS-MCA:

    As I understood, you have content rendered in English(or some other language) in your page. Now, you can successfully change the culture setting of the page but not the content. In order to change the content language to some other language, you need to use some Language API like Google Language.

    Below is given a  sample that shows how to change content language from English to Spanish. You can build upon this sample.

    ASPX Code

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Automatic Text Conversion using Google Language API</title>
    </head>
        <script type="text/javascript" src="//www.google.com/jsapi"></script> 
        <script type="text/javascript">
            var defLang = "";
            var targetLang = "";
            var ControlID = "Translation";
            google.load("language", "1");
            function initialize() {
                var container = document.getElementById(ControlID);
                if (container != null) {
                    google.language.translate(container.innerHTML, defLang, targetLang, function(result) {
                        if (!result.error) {
                            container.innerHTML = "";
                            container.innerHTML = result.translation;
                        }
                    });
                } else {
                    alert("Invalid ControlID provided.");
                }
            }
        </script> 
    <body>
        <form id="form1" runat="server">
            <asp:Label ID="Translation" runat="server" Text="Its Friday, let's party."></asp:Label>
        </form>
    </body>
    </html>


    Code Behind

    Partial Class _Default
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            'You can find the whole list of code at following link
            'http://code.google.com/apis/ajax/playground/?exp=language#is_it_translatable
    
            'Variables to hold the primary & target language code for Google Language APIs
            Dim PrimaryGoogleLanguageCode As String = "en" 'English
            Dim TargetGoogleLanguageCode As String = "es" 'Spanish
    
            ' Define the name and type of the client script on the page. 
            Dim csName As [String] = "GoogleLangAPI"
            Dim csType As Type = Me.[GetType]()
    
            ' Get a ClientScriptManager reference from the Page class. 
            Dim cs As ClientScriptManager = Page.ClientScript
    
            ' Check to see if the client script is already registered. 
            If Not cs.IsClientScriptBlockRegistered(csType, csName) Then
                Dim csText As New StringBuilder()
                csText.Append("<script type=""text/javascript"">defLang=""")
                csText.Append(PrimaryGoogleLanguageCode)
                csText.Append(""";targetLang=""")
                csText.Append(TargetGoogleLanguageCode)
                csText.Append(""";google.setOnLoadCallback(initialize);</script>")
                cs.RegisterClientScriptBlock(csType, csName, csText.ToString())
            End If
        End Sub
    
    End Class


    For the list of language codes used by Google Language Translation service, refer the following URL : http://code.google.com/apis/ajax/playground/?exp=language#is_it_translatable

    To refer Google Language Translation API, refer : http://code.google.com/apis/ajaxlanguage/documentation/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 30, 2010 5:01 AM

All replies

  • User704184736 posted

     Hi,

    Check this article.

    http://blog.eworldui.net/post/2008/05/ASPNET-MVC---Localization.aspx

    Monday, November 29, 2010 6:15 AM
  • User129936097 posted

    I want to write Marathi on page when i select Marathi as a language . Which Culture sholud i sue ? Is there any culture "mr-MR". Also plese provide the list of culture supported by ASp.NET 2.0.

    I used Frech,Dutch. They are doing translation well but what about Hindi/Marathi or any other indain langauge ? Is there any other Culture availble in .NET ?


    Thank You,

    Pranav Unde.

    Monday, November 29, 2010 8:39 PM
  • User-242433875 posted

    @pranav_unde:

    the culture for Marathi would be "mr-IN"

    The complete list of culture can be accessed from the following URL: http://msdn.microsoft.com/en-us/goglobal/bb896001.aspx

    Tuesday, November 30, 2010 4:54 AM
  • User-242433875 posted

    @RS-MCA:

    As I understood, you have content rendered in English(or some other language) in your page. Now, you can successfully change the culture setting of the page but not the content. In order to change the content language to some other language, you need to use some Language API like Google Language.

    Below is given a  sample that shows how to change content language from English to Spanish. You can build upon this sample.

    ASPX Code

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Automatic Text Conversion using Google Language API</title>
    </head>
        <script type="text/javascript" src="//www.google.com/jsapi"></script> 
        <script type="text/javascript">
            var defLang = "";
            var targetLang = "";
            var ControlID = "Translation";
            google.load("language", "1");
            function initialize() {
                var container = document.getElementById(ControlID);
                if (container != null) {
                    google.language.translate(container.innerHTML, defLang, targetLang, function(result) {
                        if (!result.error) {
                            container.innerHTML = "";
                            container.innerHTML = result.translation;
                        }
                    });
                } else {
                    alert("Invalid ControlID provided.");
                }
            }
        </script> 
    <body>
        <form id="form1" runat="server">
            <asp:Label ID="Translation" runat="server" Text="Its Friday, let's party."></asp:Label>
        </form>
    </body>
    </html>


    Code Behind

    Partial Class _Default
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            'You can find the whole list of code at following link
            'http://code.google.com/apis/ajax/playground/?exp=language#is_it_translatable
    
            'Variables to hold the primary & target language code for Google Language APIs
            Dim PrimaryGoogleLanguageCode As String = "en" 'English
            Dim TargetGoogleLanguageCode As String = "es" 'Spanish
    
            ' Define the name and type of the client script on the page. 
            Dim csName As [String] = "GoogleLangAPI"
            Dim csType As Type = Me.[GetType]()
    
            ' Get a ClientScriptManager reference from the Page class. 
            Dim cs As ClientScriptManager = Page.ClientScript
    
            ' Check to see if the client script is already registered. 
            If Not cs.IsClientScriptBlockRegistered(csType, csName) Then
                Dim csText As New StringBuilder()
                csText.Append("<script type=""text/javascript"">defLang=""")
                csText.Append(PrimaryGoogleLanguageCode)
                csText.Append(""";targetLang=""")
                csText.Append(TargetGoogleLanguageCode)
                csText.Append(""";google.setOnLoadCallback(initialize);</script>")
                cs.RegisterClientScriptBlock(csType, csName, csText.ToString())
            End If
        End Sub
    
    End Class


    For the list of language codes used by Google Language Translation service, refer the following URL : http://code.google.com/apis/ajax/playground/?exp=language#is_it_translatable

    To refer Google Language Translation API, refer : http://code.google.com/apis/ajaxlanguage/documentation/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 30, 2010 5:01 AM
  • User-1653290890 posted

    Protected Overrides Sub InitializeCulture()
           
            If Session("Language") = "mr-IN" Then
                SetUICulture("mr-IN")
            Else
                SetUICulture("en")
            End If
            Me.Controls.Clear()
            MyBase.InitializeCulture()
        End Sub
    
        
        Public Sub SetUICulture(ByVal culture As String)
            'culture = "mr-IN"
            Try
                System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo(culture)
            Catch
                Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", "<script language='javascript'>alert('Local '" & culture & " 'is Not Supported.')</script>")
                'Response.Write("<script language='javascript'>alert('Local '" & culture & " 'is Not Supported')</script>")
            End Try
        End Sub
    

    Tuesday, November 30, 2010 5:03 AM