Results 1 to 4 of 4
  1. #1
    Albert is offline Senior Member
    Join Date
    Jun 2007
    Posts
    114
    Rep Power
    0

    Default Multiple Language in my web page

    I have a small site setup in both english and french. Both language content is on the same page... there is just an include to set a session variable to either "eng" or "fr" and then the page grabs the appropriate content depending on which language was selected.

    Here is basically how it's setup.
    There is an inital splash page to select your language... then the language is set.
    There is an include is in every page to check what language to display.
    To switch languages, there is a button to a language switch page, that changes it to either english or french.

    F or some reason the language select will notwork all the time.. just sometimes it seems. And sometimes I will have to click the "langaugage selection" link twice to switch languages. The code is below... but I'm wondering if there is a simpler way to do this, or maybe modify the existing code to have it function properly. Any help is greatly appreciated!

    Language Selection Page
    (sets the language initially) - there is an eng and fr page... both pages are the same exept one sets "eng" and one sets "fr"

    Java Code:
    <%
       String langChoice = "eng";
       session.setAttribute( "langPref", langChoice );
    %>
    Every page has this include in it which checks the language for "eng" or "fr"
    Java Code:
    <%
            String langChoice = request.getParameter( "lang" );
    
            if ( langChoice == null )
    			session.setAttribute( "langPref", session.getValue("langPref") );
    		else if ( langChoice.equals("fr") && session.getValue("langPref") == "eng" )
    			session.setAttribute( "langPref", langChoice );
    		else if ( langChoice.equals("eng") && session.getValue("langPref") == "fr" )
    			session.setAttribute( "langPref", langChoice );
            else if ( langChoice.equals("fr") )
    			session.setAttribute( "langPref", "fr" );
    		else
                session.setAttribute( "langPref", "eng" );
    
    %>
    Every page has a button to switch the language.
    Java Code:
    <% 
    
       String langChoice;
       if (session.getValue("langPref") == "eng") 
       langChoice = "fr";
       else
       langChoice = "eng";
    
       session.setAttribute( "langPref", langChoice );
    
    %>
    Thanks.
    Albert:rolleyes:

  2. #2
    Marcus is offline Member
    Join Date
    Jun 2007
    Posts
    92
    Rep Power
    0

    Default

    I haven't tested this but I think it is something to do with using the "==" operator.

    In other words, when you test a string for equality, you should either use "equals", or "equalsIgnoreCase".

    When you use the "==" operator on Strings/Objects, you are basically checking if a given variable points to the same construct as another variable in memory.

    so, you might need to change the following

    Java Code:
    if ( langChoice.equals("fr") && session.getValue("langPref").toString().equals( "eng" ) )
    Also, I would strongly recommend that you use cookies instead of session variables.
    Why you might ask? simply to store users preferences so you don't have to ask them a question they have already answered.
    Plus, it will reduce the amount of request and response overhead on the server..so, it is a win win situation.

    If you do decide to use cookies, make sure that you do a redirect...as the page that sets cookies won't be able to read it at the same time of writing it.

    Greetings
    Marcus:cool:

  3. #3
    Felissa is offline Member
    Join Date
    Jun 2007
    Posts
    95
    Rep Power
    0

    Default

    If you are still having problems...I have the following example that might of interest to you.

    I have two files...first one is index.jsp and the second is setlang.jsp

    index
    Java Code:
    <%@ page session="true" %>
    <%
        if( null == session.getValue( "lang" ) ) {
    %>
        Please Select a language <a href="setlang.jsp?lang=fr">french</a> | <a href="setlang.jsp?lang=eng">english</a> <br/>
    <%
        } else {
            
            if ("fr".equals( session.getValue("lang" ) ) ){
                out.println( "You're viewing this page in <b>French</b>, Change to " + "<a href=\"setlang.jsp?lang=eng\">English</a> <br/>" );
            } else {
                out.println( "You're viewing this page in <b>English</b>, Change to " + "<a href=\"setlang.jsp?lang=fr\">French</a> <br/>" );
            } //-- ends inner else
        } //-- ends else block
    %>
    setlang
    Java Code:
    <%@ page session="true" %>
    <%
    // your default language.
    String default_language = "fr";
    
    if( "eng".equals( request.getParameter( "lang" ) ) || "fr".equals( request.getParameter( "lang" ) ) ) {
        // if lang is equal to fr (french) or equal to en (english)...select the language
        // and redirect 
        session.putValue( "lang", request.getParameter( "lang" ) );
        // now we need to do a redirect
        // note: I am redirecting to project context.
        response.sendRedirect( request.getContextPath() );
    } else {
        // use the default language in this case.
        session.putValue( "lang", default_language );
    } //-- ends else block
    %>
    Greetings
    Felissa:p

  4. #4
    Java.Assmbly is offline Member
    Join Date
    Sep 2009
    Posts
    1
    Rep Power
    0

    Default

    Dear All,

    I also want to implement this in my project.
    Please guide me for this.


    I would appreciate any information.

    Thanks in advance.
    Samir

Similar Threads

  1. Blocks Language 0.1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 09-22-2007, 11:21 PM
  2. The Frink Language 2007-08-04
    By levent in forum Java Software
    Replies: 0
    Last Post: 08-05-2007, 07:15 PM
  3. V language 0.004
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-19-2007, 03:18 PM
  4. TOM programming language 2.5
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-14-2007, 08:31 PM
  5. The Frink Language 2007-07-09
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-09-2007, 04:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •