/    Sign up×
Community /Pin to ProfileBookmark

Language detection

Im trying to make a laguage detection, this code detect the language from the web browser, and it works almost ok, but in MAC dosent work I test the page in OS X and OS 9, some one can give me a hand?

Thank you for STOP in my question nad for any help!

  • * One more thing someone can tell me how some web sites can detect your country? And how can I detect the language from the OS ?
  • ——————————–CODE——————————

    <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
    <html>
    <head>
    <title></title>
    <META HTTP-EQUIV=”Pragma” CONTENT=”no-cache”>
    <META HTTP-EQUIV=”Expires” CONTENT=”-1″>
    <meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>

    <script>

    //Enter ISO 639-2 letter Language codes to detect (see: [url]http://www.w3.org/WAI/ER/IG/ert/iso639.htm[/url]):

    var langcodes=new Array(“en”, “fr”, “es”, “pt”, “de”, “it”, “default”)

    //Enter corresponding redirect URLs (last one is for default URL):

    var langredirects=new Array(“en.php”, “fr.php”, “es.php”, “pt.php”, “de.php”, “it.php”, “en.php”)

    var languageinfo=navigator.language? navigator.language : navigator.userLanguage

    var gotodefault=1

    function redirectpage(dest){

    if (window.location.replace)

    window.location.replace(dest)

    else

    window.location=dest

    }

    for (i=0;i<langcodes.length-1;i++){

    if (languageinfo.substr(0,2)==langcodes[i]){

    redirectpage(langredirects[i])

    gotodefault=0

    break

    }

    }

    if (gotodefault)

    redirectpage(langredirects[langcodes.length-1])

    </script>
    </head>
    <body>
    <br>
    <br>
    <br>
    </body>
    </html>

    to post a comment
    JavaScript

    3 Comments(s)

    Copy linkTweet thisAlerts:
    @Khalid_AliJun 17.2003 — navigator object is probably the best bet JavaScript has its limitations...You might want to check into ActiveXControls for MS+IE stuff there may be a better solution to read OS lang
    Copy linkTweet thisAlerts:
    @_LOBO_authorJun 17.2003 — Thanks Khalid

    Can you please put a LINK where I can get more info about ActiveXControls for MS+IE and this controls work in NETSCAPE? You Know where I can find the code for make the language detection ?
    Copy linkTweet thisAlerts:
    @_LOBO_authorJun 17.2003 — This SCRIPT works fine in PC and MAC, tested in

    PC Win XP IE 6

    Mac OSX / 9 IE 5

    I found the aswere!


    <!-- ONE STEP TO INSTALL LANGUAGE:

  • 1. Copy the coding into the HEAD of your HTML document -->


  • <!-- STEP ONE: Paste this code into the HEAD of your HTML document -->

    <HEAD>

    <SCRIPT LANGUAGE="JavaScript1.2">

    <!-- Begin

    if (navigator.appName == 'Netscape')

    var language = navigator.language;

    else

    var language = navigator.browserLanguage;

    /*-------------------------------------------------------------

    Netscape instructions:


    In Netscape, find the language you want to redirect by going to

    Edit Menu --> Preferences...

    Then click the '+' box next to 'Navigator' and click 'Languages'

    Click 'Add' then find the languages you want and add them below.


    ---------------------------------------------------------------

    Microsoft Internet Explorer instructions:


    In MSIE, find the language you want to redirect by going to

    Tools Menu --> Internet Options...

    Then click the 'Languages' button near the bottom of the page.

    Click 'Add' then find the languages you want and add them below.

    -------------------------------------------------------------*/

    if (language.indexOf('en') > -1) document.location.href = 'English.html';

    else if (language.indexOf('nl') > -1) document.location.href = 'dutch.html';

    else if (language.indexOf('fr') > -1) document.location.href = 'french.html';

    else if (language.indexOf('de') > -1) document.location.href = 'german.html';

    else if (language.indexOf('ja') > -1) document.location.href = 'japanese.html';

    else if (language.indexOf('it') > -1) document.location.href = 'italian.html';

    else if (language.indexOf('pt') > -1) document.location.href = 'portuguese.html';

    else if (language.indexOf('es') > -1) document.location.href = 'Spanish.html';

    else if (language.indexOf('sv') > -1) document.location.href = 'swedish.html';

    else if (language.indexOf('zh') > -1) document.location.href = 'chinese.html';

    else

    document.location.href = 'English.html';

    // End -->

    </script>

    <p><center>

    <font face="arial, helvetica" size="-2">Free JavaScripts provided<br>

    by <a href="http://javascriptsource.com">The JavaScript Source</a></font>

    </center><p>

    <!-- Script Size: 1.95 KB -->
    ×

    Success!

    Help @_LOBO_ spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.28,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...