Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-04-2007, 03:59 PM
Senior Member
 
Join Date: Jun 2007
Posts: 114
Albert is on a distinguished road
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"

Code:
<% String langChoice = "eng"; session.setAttribute( "langPref", langChoice ); %>
Every page has this include in it which checks the language for "eng" or "fr"
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.
Code:
<% String langChoice; if (session.getValue("langPref") == "eng") langChoice = "fr"; else langChoice = "eng"; session.setAttribute( "langPref", langChoice ); %>
Thanks.
Albert
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-04-2007, 04:01 PM
Member
 
Join Date: Jun 2007
Posts: 92
Marcus is on a distinguished road
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

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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-04-2007, 04:04 PM
Member
 
Join Date: Jun 2007
Posts: 95
Felissa is on a distinguished road
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
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
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Blocks Language 0.1 JavaBean Java Announcements 0 09-23-2007 12:21 AM
The Frink Language 2007-08-04 levent Java Announcements 0 08-05-2007 08:15 PM
V language 0.004 JavaBean Java Announcements 0 07-19-2007 04:18 PM
TOM programming language 2.5 JavaBean Java Announcements 0 07-14-2007 09:31 PM
The Frink Language 2007-07-09 JavaBean Java Announcements 0 07-09-2007 05:32 PM


All times are GMT +3. The time now is 05:10 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org