JavaScript Internationalization Programming
Locale, Format, and Time Zone

The following are general points of JavaScript internationalization.
About .NET Ajax Library, jQuery, and other libraries, refer to each information resources.
  1. User Locale (language and region) Detection

    For switching format and messages corresponding to users' languages and regions, you need to detect users' locales (language and region information).
    The general programming language such as Java and PHP have the functions to get these information, but JavaScript don't have equivalent features.
    In general JavaScript programming, user languages are detected by user browser language information,
    for example, whether one's Firefox is English version or Japanese.
    You should note that this information is, in most of browsers, not a language information set in the preference screen.
    The following is a simple code to get a language code from browser language information.

    function wwnaviGetLang(){
        return (navigator.userLanguage||navigator.browserLanguage||navigator.language).substr(0,2);
    }
    
    This code means the user who uses Japanese Firefox is supposed to use Japanese.
    You should note that OS language settings and language parameters in HTTP request to servers are not reflected.

    The regions, in some cases, are set in the third and the following characters in the code above, but it's not guaranteed.
    In other ways, you can get the representative region by time difference from UTC, but this is not exact one.


    *If you want to reflect browser language settings, you can use a jQuery library detecting HTTP request language accessing to a remote host (aware of browser language switching),
    named jQuery-Browser-Language.

    The following is a sample code using that library.
    $.browserLanguage(function( language , acceptHeader ){
        var l  = acceptHeader; l = l.substring(0,2);
        alert(l); // this will return 'en', 'ja'... parsing HTTP request headers, 
                  // the same values as browser language settings.
    });
    
    *If you want to do the step above without jQuery library, refer to Internationalization by JSON.

  2. Format Of Date And Currency

    Java, PHP and other popular programming languages have the functions and classes to format data corresponding to locales, but JavaScript doesn't have such functions.
    If you want to switch the format based on user language and regions, you have to create such functions by your own.
    Programming these process for all languages is tough, but the popular libraries like .NET Ajax and jQuery have common functions for that kind of process.

    (*jQuery has a Glob, a library for multilingual date and currency.)

    We recommend you to check if the codes of date and currency formatting don't use fixed format and use the libraries above if its switching is required.

    About format, refer to String Format and Culture Specific Expressions.


  3. User Time Zone Detection

    JavaScript doesn't have functions to get time zone, but has a functions to get time difference between UTC (Universal Time, Coordinated) and a client PC).
    This enables you to convert the common time in the server to client PC time,
    and you can detect the representative user regions by this time difference too.

    The following code is to get the time difference between UTC and client PCs by hours.

    function getTzOff() {
        var date = new Date();
        return tzoff = ( date.getHours() - date.getUTCHours() + 24 ) % 24;      
    }
    
    You can expect user representative regions in JavaScript by creating the following mapped data between time difference and regions.
    0=GB
    1=IT
    2=FI
    3=RU
    4=AE
    5=PK
    6=BD
    7=ID
    8=PH
    9=JP
    10=AU
    11=NC
    12=NZ
    13=TO
    14=TO
    15=US
    16=US
    17=US
    18=US
    19=US
    20=CL
    21=BR
    22=BR
    23=GL
    
    (If you change the country codes to time zone IDs above, 
    you can expect user representative time zones too.)
    
    *You have to note that one time difference has several different regions.
    In actual cases, you should create preferences for users to set their own regions later.



Go to Internationalization Programming Top


Copyright (C) 2012 Kokusaika JP, Inc.
All rights reserved. No reproduction or republication without written permission.