• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 390
  • Last Modified:

Language bar swap

I need to have bar like this one:
<div id="lang"><p><a href="#">English</a> | <a href="#">Polski</a> | <a href="#">Deutsch</a></p></div>

Open in new window

For example I have website on Spanish language version.
When I switch to English, I wish Spanish to be displayed on bar instead.
I am looking for how to optimize this process, I can use include and create language bar for each website version, but I don't think it's the best way to do it with such small component.
What is the best, the most efficient way to do it?
0
SSupreme
Asked:
SSupreme
  • 6
  • 4
  • 2
3 Solutions
 
Hugh McCurdyCommented:
What are you trying to optimize?  Development speed?  Execution speed?

How are you deciding which language you are in?  I presume the user selects but how?  (Or is that to be determined?)
0
 
SSupremeAuthor Commented:
I need to Optimize this process in all ways.
It should replace whole bar or values when you switch to another version, for example
Spanish version -> English | Polski | Deutsch
click english
English version -> Polski | Spanish | Deutsch

order doesn't matter.
There are few ways of doing, I would like witch one is the best in this case.
0
 
Hugh McCurdyCommented:
Optimizing in all ways isn't generally possible.  (I'm sure there are exceptions but I can't think of one.)  The classic optimization choice is speed vs size.  Doesn't mean you can't do well on both but you can't optimize for both.  (In the contest in my assembly lang. class, I had the smallest and 4th fastest.  But the professor told us that nobody would win both categories.)

Can you share your vision of what would happen when the user selects a different language?  Same page loads but with a different banner and in a different language?  Something else?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Ray PaseurCommented:
http://www.laprbass.com/RAY_temp_ssupreme.php
<?php // RAY_temp_ssupreme.php
error_reporting(E_ALL);

// Spanish version -> English | Polski | Deutsch

// SET THE ARRAY OF POSSIBLE LANGUAGES
$langs = array
( 'es' => 'Spanish'
, 'en' => 'English'
, 'pl' => 'Polski'
, 'de' => 'Deutsch'
)
;

// SET THE DEFAULT LANGUAGE
$lang     = 'en';
$language = $langs[$lang];

// IF THERE IS A GET ARGUMENT
if (isset($_GET["lang"]))
{
    if (array_key_exists($_GET["lang"], $langs))
    {
        $lang     = $_GET["lang"];
        $language = $langs[$lang];
    }
}

// WHAT LANGUAGE ARE WE USING?
echo "<br/>THE SITE IS NOW IN <b>$language</b><br/>";

// CREATE LINKS FOR THE LANGUAGES
$out[] = $langs[$lang];
unset($langs[$lang]);
foreach ($langs as $lang_code => $lang_name)
{
    $out[] = "<a href=\"{$_SERVER["PHP_SELF"]}?lang=$lang_code\">$lang_name</a>";
}
echo implode(' | ', $out);

Open in new window

0
 
Hugh McCurdyCommented:
Please try what Ray suggests and get back to us if it doesn't work for you.
0
 
SSupremeAuthor Commented:
Ray_Paseur, thanks.
I think it could work for me, but I am not sure how to use links after "?".
I was thinking:
site.com/home.php    < - index English version page, when I click Spanish it goes to
site.com/es/casa.php     < - Spanish version page, when click Deutsch
site.com/de/haus.php

Is it possible with current possible solution? -Don't answer this question yet.

I need only this part to be shown: "Spanish | English | Deutsch",
Ii language is Polski, etc.
Could you please adjust it?
Why do I need default language?
0
 
Hugh McCurdyCommented:
The information after the ? becomes the $_GET variable array.

www.foo.com?language=english&dialect=georgia

Would set
$_GET [ 'language' ] to "english"

Open in new window

and set  
$_GET [ 'dialect' ] to "georgia"

Open in new window


0
 
Hugh McCurdyCommented:
Wouldn't it be better to have the language banner to have each word in its own language?   EspaƱol | English | Deutsch

As for a default language, you don't actually need one but then what page would you display?  Just a page with a banner?  I think it would be far easier on the web developer (you) if you had a default language.  I would choose the language spoken by the most visitors that you expect.  If unsure, I'd choose English.

You might also want to set a cookie so you know what language the visitor used last time and default to that on the next visit.
0
 
Ray PaseurCommented:
You could easily set up separate directories or domains for the different languages.  Example of how php.net does it:
http://php.net/manual/en/book.strings.php
http://php.net/manual/de/book.strings.php
http://php.net/manual/es/book.strings.php

Since I do not have different sub-directories or sub-domains on my server, I just used the URL "GET" arguments to carry the language signal.

Regarding this: I need only this part to be shown: "Spanish | English | Deutsch", please remove line 33 from the script I posted.

Regarding this: Why do I need default language?  You need to initialize EVERY variable and document EVERY assumption.  That's a basic principle of computer science.

If you are new to PHP programming, you might enjoy this book.  It won't make you a pro, but it will get you something of a foundation in how PHP works.
http://www.sitepoint.com/books/phpmysql4/

Best regards, ~Ray
0
 
Hugh McCurdyCommented:
Regarding this: Why do I need default language?  You need to initialize EVERY variable and document EVERY assumption.  That's a basic principle of computer science.

Ray is quite correct.  I was thinking you could have a default language of null but I wondered what you'd possibly display.

Failure to initialize all variables can (read will) lead to program bugs.  Failure to document can confuse the maintenance engineer (which could be you a year from now).  I document directives that I get from my boss or from customers too.  I want to know who insisted I write the program the way I did.

/** If the report won't fit on the page, crash the program per <customer name> who didn't want to pay for, or even think about, a complete solution.  Oct 7, 1997 */
Then when the customer's employee called me in (say) 2004, I had a defense.

BTW, my "crash" was really a report that said "Jack <not his real name> said this would never happen."  Document all assumptions especially when they don't come from you.
0
 
SSupremeAuthor Commented:
Regarding this: I need only this part to be shown: "Spanish | English | Deutsch"
It works.

Regarding this: Why do I need default language?
I asked this question because lets say I am at some point in the world, I don't need to know where I am to go to East, North, West or South, but I can always know where I come from.
On site default page is English, I don't need to know it. But if I go to Polski, I will know that I came from English page.

I some cases you don't need to know every variable and document every assumption, as you might not care for various reason.
0
 
SSupremeAuthor Commented:
Thanks
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 6
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now