Get Locale country and language codes

Valorous Hero ,
Nov 15, 2008 Nov 15, 2008

Copy link to clipboard

Copied

I have checked the documentation, but nothing is jumping out at me. Is there a function that returns the locale country and language codes? GetLocale() returns the "locale name as it is represented in ColdFusion":

ie:
English (US)
Spanish (Standard)
...

What I am looking for is the ISO codes

en US
es ES
...

I can get the information using getPageContext(), but just wondered if I was overlooking a built-in CF function.
TOPICS
Server administration

Views

6.4K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 15, 2008 Nov 15, 2008

Copy link to clipboard

Copied

Hi,
The attached function from my library returns a query object with local information. You can use the query to fill a country listbox, but you can also SQL SELECT it to find the info you need.

cheers,
fober



<cffunction name="locales_query" output="Yes">
<cfargument name="query" type="variableName" default="rs_items">

<cfset rs_locales = QueryNew("DisplayName, Locale")>

<cfset z= 1>
<cfloop index="x" list="#Server.ColdFusion.SupportedLocales#">
<cfset temp= QueryAddRow(rs_locales)>
<cfset temp= QuerySetCell(rs_locales, "DisplayName", "#GetLocaleDisplayName(x)#", z)>
<cfset temp= QuerySetCell(rs_locales, "Locale", "#x#", z)>
<cfset z=z+1>
</cfloop>

<cfset "#query#"= rs_locales>
</cffunction>

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 15, 2008 Nov 15, 2008

Copy link to clipboard

Copied

Thanks, but it is not quite what I am looking for.

The problem with the functions I have seen is that they return locale name as it is represented in ColdFusion (ie English (US) ). But what I am looking for the ISO language and country codes.
http://java.sun.com/javase/6/docs/api/java/util/Locale.html#getCountry()

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 16, 2008 Nov 16, 2008

Copy link to clipboard

Copied

-==cfSearching==- wrote:
> The problem with the functions I have seen is that they return locale name
> as it is represented in ColdFusion
(ie English (US) ). But what I am
> looking for the ISO language and country codes.

...hmm this didn't go thru on the 16th. let me try again.

that should only be for the "traditional" cf supported locales (backwards
compatibility is always foremost in the cf team's minds), the rest should be in
normal java style locale ID (th_TH, ar_AE, etc).

no, there's nothing built-in in cf for what you want. you'll have to dip down
into java:

<cfscript>
localeObj=createObject("java","java.util.Locale");
locales=localeObj.getAvailableLocales();
writeOutput("<table border='1'>");
writeOutput("<tr align='center'><td
colspan='2'>Locale</td><td>Language</td><td>Country</td></tr>");
for (i=1; i LTE arrayLen(locales);i=i+1) {
thisLocale=locales ;
thisLanguage=thisLocale.getLanguage();
thisCountry=thisLocale.getCountry();
writeOutput("<td>#thisLocale.toString()#</td><td>#thisLocale.getDisplayName()#</td><td>#thisLanguage#</td><td>#thisCountry#</td></tr>");
}
writeOutput("</table>");
</cfscript>

note that this snippet doesn't show variants (eg. th_TH_TH) & shows language
only locales (en, fr, etc.), nor is it sorted in any order i can recognize. also
keep in mind that what this shows depends on the server's JRE version. if you
want a "better" source of locales, use the icu4j lib's ULocale class. it's based
on the latest CLDR & contains way more locales (and they actually tend to fix
locale bugs in your lifetime).

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 11, 2021 Feb 11, 2021

Copy link to clipboard

Copied

LATEST

I realize this is resurrecting a post from 13 years ago, but I came across it while helping someone, and I see there was a mistake in the code offered (to generate a list of locales from Java, rather than using the built-in CF server scope list of them).

 

The code above fails to work because this line:

thisLocale=locales ;

should be:

thisLocale=locales[i] ;

 Given the space that appears where this array reference should be, I won't be surprised if the original text DID have it, but it got lost somewhere along the way (perhaps in the conversion from the old forums to the new one), though of course that would be lamentable if it may mean that many code examples could suffer such a problem. I've not seen them, but it's worth noting. 

 

Anyway, just wanted to offer this correction in case someone else might come across it someday, like I did today. And thanks for it, whoever the "newsgroup_user" was, and to the rest shared here by cfsearching, whoever that is/was. 🙂


/Charlie (server troubleshooter, carehart.org)

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 16, 2008 Nov 16, 2008

Copy link to clipboard

Copied

oops, didn't realize it was you "cfsearching". you probably already knew that stuff.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 17, 2008 Nov 17, 2008

Copy link to clipboard

Copied

> oops, didn't realize it was you "cfsearching". you probably already knew that stuff.

No problem. Thanks for that though. I was trying to get the language and country codes for the current request. Before I dipped into java, I wanted to be sure there was not a CF function I overlooked. I ended up doing this:

<cfscript>
variables.locale = getPageContext().getResponse().getLocale();
if ( not structKeyExists(variables, "locale") ) {
variables.locale = getPageContext().getRequest().getLocale();
}
// this should return the default locale
WriteOutput("language="& variables.locale.getLanguage() &"<br>");
WriteOutput("country="& variables.locale.getCountry() &"<hr>");

SetLocale("es_ES");

variables.locale = getPageContext().getResponse().getLocale();
if ( not structKeyExists(variables, "locale") ) {
variables.locale = getPageContext().getRequest().getLocale();
}
// this should return the current locale
WriteOutput("language="& variables.locale.getLanguage() &"<br>");
WriteOutput("country="& variables.locale.getCountry() &"<hr>");
</cfscript>

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 17, 2008 Nov 17, 2008

Copy link to clipboard

Copied

-==cfSearching==- wrote:

> be sure there was not a CF function I overlooked. I ended up doing this:
>
> <cfscript>
> variables.locale = getPageContext().getResponse().getLocale();
> if ( not structKeyExists(variables, "locale") ) {
> variables.locale = getPageContext().getRequest().getLocale();
> }
> // this should return the default locale
> WriteOutput("language="& variables.locale.getLanguage() &"<br>");
> WriteOutput("country="& variables.locale.getCountry() &"
");
>
> SetLocale("es_ES");
>
> variables.locale = getPageContext().getResponse().getLocale();
> if ( not structKeyExists(variables, "locale") ) {
> variables.locale = getPageContext().getRequest().getLocale();
> }
> // this should return the current locale
> WriteOutput("language="& variables.locale.getLanguage() &"<br>");
> WriteOutput("country="& variables.locale.getCountry() &"
");
> </cfscript>

that snippet only shows "en" for my server's locale (when it's actually en_US).
i think you might be safer using the default core java locale instead.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 17, 2008 Nov 17, 2008

Copy link to clipboard

Copied

> that snippet only shows "en" for my server's locale (when it's actually en_US).

On my server it displays:
(default)
en
US
==========
(after setLocale)
es
ES

Are you saying it is displaying something different for you? (Bear with me, I have had very little sleep ;-)


Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 17, 2008 Nov 17, 2008

Copy link to clipboard

Copied

-==cfSearching==- wrote:
> Are you saying it is displaying something different for you? (Bear with me, I
> have had very little sleep ;-)

yup. default is en_US. your snippet shows just "en" for that default (once the
cf locale is changed it shows es_ES as expected).

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 18, 2008 Nov 18, 2008

Copy link to clipboard

Copied

> yup. default is en_US. your snippet shows just "en" for that default
> (once the cf locale is changed it shows es_ES as expected).

Weird. But thanks for the "heads up". I will use the java default instead.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 18, 2008 Nov 18, 2008

Copy link to clipboard

Copied

-==cfSearching==- wrote:
> Weird. But thanks for the "heads up". I will use the java default instead.

what are you trying to do anyway?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 18, 2008 Nov 18, 2008

Copy link to clipboard

Copied

> what are you trying to do anyway?

I needed to create a customizable chart with webcharts. To set the locale, its model requires the iso codes.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 18, 2008 Nov 18, 2008

Copy link to clipboard

Copied

-==cfSearching==- wrote:
>> what are you trying to do anyway?
>
> I needed to create a customizable chart with webcharts. To set the locale, its model requires the iso codes.

i see. what happens if you don't set a locale? does it use the server default
anyway?

actually on second thought, shouldn't that be client locale?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 18, 2008 Nov 18, 2008

Copy link to clipboard

Copied

> I needed to create a customizable chart with webcharts. To set
> the locale, its model requires the iso codes.

... So inside my function, I needed to determine the current locale if one was not specified.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 18, 2008 Nov 18, 2008

Copy link to clipboard

Copied

> i see. what happens if you don't set a locale? does it use
> the server default anyway?

If I do not set/change the Locale explicitly, it uses the server default from what I can tell. (ie en_US)

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 19, 2008 Nov 19, 2008

Copy link to clipboard

Copied

-==cfSearching==- wrote:
> If I do not set/change the Locale explicitly, it uses the server default from what I can tell. (ie en_US)

probably a dumb question, then why bother finding the server locale & setting it
"again"?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 19, 2008 Nov 19, 2008

Copy link to clipboard

Copied

PaulH wrote:
> probably a dumb question

Not a dumb question. There are few things going on.

Short story, I needed to customize the display of dates in a gantt chart on-the-fly. Webcharts uses the server locale by default. To change it, you pass in the desired locale information via xml. That xml is fed into the charting component and used to generate the final chart (That is why I needed the iso codes). That part works fine.

(ie
<locale lang="es" country="ES" variant=""/>
)

> , then why bother finding the server locale > & setting it "again"?


Since the webcharts documentation is a bit sparse on the topic of localization, I temporarily changed the locale to better understand its behavior and debug another issue. The issue relates to tool tips. For whatever reason, webcharts correctly formats the chart labels with whatever locale I select. But ... when I try and format dates inside a tool tip, webcharts always uses the default locale for formatting.

For example if I my xml contains:

<locale lang="es" country="ES" variant=""/>

The results are:
Chart labels display (spanish):: *** lunes, martes, miercoles, jueves, ...
Tool tips display (english):: *** monday, tuesday, wednesday, thursday, ...

You get the same results using the webcharts utility. The results are obviously wrong, so I am wondering if I stupidly overlooked a property or something ..

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 20, 2008 Nov 20, 2008

Copy link to clipboard

Copied

-==cfSearching==- wrote:

> <locale lang="es" country="ES" variant=""/>
>
> The results are:
> Chart labels display (spanish):: *** lunes, martes, miercoles, jueves, ...
> Tool tips display (english):: *** monday, tuesday, wednesday, thursday, ...
>
> You get the same results using the webcharts utility. The results are
> obviously wrong, so I am wondering if I stupidly overlooked a property or
> something ..

been a while since i used cfcharts (pretty much all our frontends are flex). i
don't see anything to set locales for tooltips (are these popups??). any chance
of substituting a function like flex?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 19, 2008 Nov 19, 2008

Copy link to clipboard

Copied

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 20, 2008 Nov 20, 2008

Copy link to clipboard

Copied

> i don't see anything to set locales for tooltips (are these popups??).
> any chance of substituting a function like flex?

Neither do I. Yes, if you view source they are just html that displays on mouseover. The values are formatted as en_US, so clearly webcharts is forgetting to take locale into account when it generates them.

> any chance of substituting a function like flex?

I am up for it, I just don't know much about flex. Any pointers?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 20, 2008 Nov 20, 2008

Copy link to clipboard

Copied

> i don't see anything to set locales for tooltips (are these popups??).
> any chance of substituting a function like flex?

Neither do I. Yes, if you view source they are just html that displays on mouseover. The values are formatted as en_US, so clearly webcharts is forgetting to take locale into account when it generates them.

> any chance of substituting a function like flex?

I am up for it, I just don't know much about flex. Any pointers?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 20, 2008 Nov 20, 2008

Copy link to clipboard

Copied

-==cfSearching==- wrote:
> > any chance of substituting a function like flex?
>
> I am up for it, I just don't know much about flex. Any pointers?

sorry, i meant is there "any chance of substituting a function"..for formatting
a tooltip in webcharts3d.."like flex".

but if really wanted you could redo it in flex:

for free
http://dougmccune.com/blog/2007/02/01/building-a-gantt-chart-component-in-flex/
http://flexgeek.wordpress.com/2007/06/13/simple-ganttchart-using-advanceddatagrid-of-flex-30-moxie/

or pay $800 US
http://www.ilog.com/products/ilogelixir/

since you've got cf on the backend it would be fairly easy to use it as your
locale resource provider & pass back the localized bits to the flex gantt
"widget". pretty much all the flex UI components have options to use a function
for labels, tooltips, etc. fairly well thought out :-)

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 27, 2008 Nov 27, 2008

Copy link to clipboard

Copied

Sorry for the delayed response. I got majorly side-tracked with a bunch of other things.

> sorry, i meant is there "any chance of substituting a function"..for
> formatting a tooltip in webcharts3d.."like flex".

Other than a few "kludegy" ideas, the only way I could think of was to modify the jar. I decided not to as I did not know if / how that affected licensing.

The flex examples look pretty good. Better than cfchart. I went the cfchart route because I know zero about flex 😉 Time to dig in and learn.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 27, 2008 Nov 27, 2008

Copy link to clipboard

Copied

-==cfSearching==- wrote:
> The flex examples look pretty good. Better than cfchart. I went the cfchart
> route because I know zero about flex 😉 Time to dig in and learn.

well we mostly build flex/cf apps these days, so yeah, i would recommend you
slurp down the flex kool-aid as fast as you can. the new real time collaboration
bits (cocomo) look like a sweet add-in for many of our GIS apps.

we often found that cfcharts were limiting (we do a *lot* of "science" based
apps) so for any serious server-side reporting, etc. we usually use jFreeChart
(it's annotation bits & box/whisker charts are especially nifty).

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines