Wednesday, 20 November 2013

Creating a multy Locale site in ColdFusion

 -- To create sites that would show date, time, currency and number as per the localization, it may be bit tricky. We may have to write logic for each nation separately. But it really not that easy. To ease this process, we can play a small trick in ColdFusion.
 
-- ColdFusion provides a function named ‘SetLocale()’.  This function sets the current session to some particular localization as per your request. And once you set the locale, now you can get the time, date, currency and number in that particular local format also you can verify whether some value is properly formatted or not.

-- To get all those above values we have some predefined methods in ColdFusion as: LSIsCurrency, LSCurrencyFormat, LSDateFormat, LSEuroCurrencyFormat, LSIsDate, LSParseDateTime, LSIsNumeric, LSNumberFormat, LSParseCurrency, LSParseEuroCurrency, LSParseNumber, LSTimeFormat.

-- This setting of localization stays as such for the current session only and once the session times out, the localization is set to default localization as per it’s set in ColdFusion server. This value can also be overridden using 'SetLocale()' function.

-- So the idea for creating a multiple localized site is: for each session, you can fetch the localization of the user in OnSessionStart method, may be using some API or having a database that would consist of user IPs and their localization names. Or simply we can have a dropdown in home page, where the user would pick the localization as per his/her choice.


-- Once you get the localization details, you supposed to have another look up table for localization and locale name. Available locale names that can be used in ColdFusion are:

Chinese (China)
French (Belgian)
Korean
Chinese (Hong Kong)
French (Canadian)
Norwegian (Bokmal)
Chinese (Taiwan)
French (Standard)
Norwegian (Nynorsk)
Dutch (Belgian)
French (Swiss)
Portuguese (Brazilian)
Dutch (Standard)
German (Austrian)
Portuguese (Standard)
English (Australian)
German (Standard)
Spanish (Modern)
English (Canadian)
German (Swiss)
Spanish (Standard)
English (New Zealand)
Italian (Standard)
Swedish
English (UK)
Italian (Swiss)

English (US)
Japanese


-- So once you get the locale name, now you can use SetLocale(locale_name) method to set the locale in OnSessionStart method. So this will be set just once and will stay effective for entire session. Hence I think this is the right place to set this.

-- Now as we have already set the locale, so by using above mentioned methods(Ls….), we would be able to deal with the locale specific formats easily.

Here is a Small example to show how to set locale and use time, date, currency and number as per the locale.

<cfoutput>
    <cfif StructKeyExists(form, 'localeText')>\
        <cfif Len(Trim(form.localeText))>\
            <cfset setLocale(form.localeText)>
        <cfelse>
            <b>Please select one language</b>
        </cfif>
    </cfif>

    <form method="post" action="">
        Enter your local:
        <select type="text" name="localeText" id="text" onchange="javascript: this.form.submit()">
            <option value="">-- Select Language --</option>
            <option value="Chinese (China)" >Chinese (China)</option>
            <option value="Chinese (Hong Kong)">Chinese (Hong Kong)</option>
            <option value="French (Belgian)">French (Belgian)</option>
            <option value="Dutch (Belgian)">Dutch (Belgian)</option>
            <option value="English (Australian)">English (Australian)</option>
            <option value="English (Canadian)">English (Canadian)</option>
            <option value="English (New Zealand)">English (New Zealand)</option>
            <option value="English (UK)">English (UK)</option>
            <option value="English (US)">English (US)</option>
            <option value="Japanese">Japanese</option>
            <option value="Swedish">Swedish</option>
        </select>
        <input type="submit" style="display: none;">
    </form>


    <p>
        The locale is now <b> #GetLocale()#</b><br />
        Your Date Now: #LsDateFormat(now())#<br />
        Your Time Now: #LsTimeFormat(now())#<br />
        Your Currency: #LsCurrencyFormat(350)#<br />
        Your Number View: #LsNumberFormat(350.12, '.__')#<br />
    </P>
</cfoutput>

Initial Form Look(Taking default US Locale):

So on choosing Chinese (China), the output would be:
So on choosing Japanese, the output would be:
So on choosing Swedish, the output would be:

2 comments:

Embedding Power BI Report Using ColdFusion

Recently I got an opportunity to embed power BI reports in ColdFusion Application. Please find the steps and requirements below for implem...