23 Replies Latest reply: Nov 27, 2008 9:28 PM by Newsgroup_User RSS

    Get Locale country and language codes

    -==cfSearching==- Community Member
      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.
        • 1. Re: Get Locale country and language codes
          fober1 Community Member
          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>
          • 2. Re: Get Locale country and language codes
            -==cfSearching==- Community Member
            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()
            • 3. Re: Get Locale country and language codes
              Newsgroup_User Community Member
              -==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>#t hisLanguage#</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).
              • 4. Re: Get Locale country and language codes
                Newsgroup_User Community Member
                oops, didn't realize it was you "cfsearching". you probably already knew that stuff.
                • 5. Re: Get Locale country and language codes
                  -==cfSearching==- Community Member
                  > 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>
                  • 6. Re: Get Locale country and language codes
                    Newsgroup_User Community Member
                    -==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.

                    • 7. Re: Get Locale country and language codes
                      -==cfSearching==- Community Member
                      > 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 ;-)


                      • 8. Re: Get Locale country and language codes
                        Newsgroup_User Community Member
                        -==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).
                        • 9. Re: Get Locale country and language codes
                          -==cfSearching==- Community Member
                          > 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.
                          • 10. Re: Get Locale country and language codes
                            Newsgroup_User Community Member
                            -==cfSearching==- wrote:
                            > Weird. But thanks for the "heads up". I will use the java default instead.

                            what are you trying to do anyway?
                            • 11. Re: Get Locale country and language codes
                              -==cfSearching==- Community Member
                              > 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.
                              • 12. Re: Get Locale country and language codes
                                -==cfSearching==- Community Member
                                > 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.
                                • 13. Re: Get Locale country and language codes
                                  Newsgroup_User Community Member
                                  -==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?
                                  • 14. Re: Get Locale country and language codes
                                    -==cfSearching==- Community Member
                                    > 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)
                                    • 15. Re: Get Locale country and language codes
                                      Newsgroup_User Community Member
                                      -==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"?

                                      • 16. Re: Get Locale country and language codes
                                        -==cfSearching==- Community Member
                                        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 ..
                                        • 18. Re: Get Locale country and language codes
                                          Newsgroup_User Community Member
                                          -==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?

                                          • 19. Re: Get Locale country and language codes
                                            -==cfSearching==- Community Member
                                            > 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?
                                            • 20. Re: Get Locale country and language codes
                                              -==cfSearching==- Community Member
                                              > 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?
                                              • 21. Re: Get Locale country and language codes
                                                Newsgroup_User Community Member
                                                -==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 :-)
                                                • 22. Re: Get Locale country and language codes
                                                  -==cfSearching==- Community Member
                                                  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.

                                                  • 23. Re: Get Locale country and language codes
                                                    Newsgroup_User Community Member
                                                    -==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).