|By Ray Camden||
|February 25, 2004 12:00 AM EST||
Welcome to another installment of Extending ColdFusion. In this edition, we are going to look at one of the ways you can add localization to your Web site. What do we mean by localization? Localization or L10N (L10N is an abbreviation for the 10 letters between the "L" and "N" in localization) describes the process of adapting an application to a specific locale.
You can think of L10N as the process of applying a locale or language "skin" to an application. That can mean many things like date, currency, and number formatting; calendars; text direction, and so on. Typically localization is done after a process called internationalization. Internationalization or I18N (I18N is an abbreviation for the 18 letters between the "I" and "N" in internationalization) is the design and development of an application so that it functions in at least two locales. You can think of I18N as making an application language or locale neutral.
In this article we are going to focus on one aspect of localization, and that is the site user interface and static text. In a later edition we will talk about how dynamic content can be localized. We are going to talk specifically to the concept of "resource bundles." These are files that contain a set of translations. For example, here is a representation of a simple English resource bundle:
Not very exciting, but the basic concept here is that we have a list of keys (the strings on the left side of the equal signs) and values (the strings on the right side). This becomes clearer when we look at the French version of the bundle:
As you can see, we have the exact same keys, but the strings on the right-hand side, the actual values, are the French versions. Extending this a bit farther, you can use a larger set of keys and values, one key for each piece of text on your Web site. To add support for a new language, you simply create a new resource bundle and do the translations. You should understand that managing the translation of resource bundles can become quite a task for anything complicated. One of the more popular tools for this task is IBM's Java-based rbManager, which you can download from http://oss.software.ibm.com/icu4j/demo_tools/RBManager.html.
One note about resource bundles: we use a Java object, java.util.PropertyResourceBundle, to handle parsing in the resouce bundle. This Java object requires that the resource bundle files be encoded using escaped ASCII. For example, here is the Thai version of "Cancel": \u0E22\u0E01\u0E40\ u0E25\u0E34\u0E0. Besides the rbManager tool mentioned above, Sun provides a command-line tool, native2ascii, that handles the creation of properly escaped Unicode text. This program is included in the "bin" directory of standard Java installs. You can find documentation for it at: http://java.sun.com/j2se/1.4.1/docs/tooldocs/windows/native2ascii.html.
How do we use this in ColdFusion? Listing 1 demonstrates a simple CFC that we will use to load resource bundles and display them on our Web site. Let's begin by looking at the getResourceBundle method. This method will take a resource bundle and return a structure of keys and values. The method takes two arguments. The first argument is the file name of the resource bundle. (We actually do some magic with this argument, which I will go into a bit later.) The second argument is optional, and represents the locale. Java uses a different format for locales than ColdFusion. Notice how the default is "en_US." In CFML, this is the same as English (US).
After initializing our arguments, we then have a set of var statements. Do not forget that it is vital to var scope any variable created with a CFC method. As I said above, we are going to do some magic with the resource bundle file name. If the file doesn't exist as passed, we check to see if a locale version exists. What do we mean by that? Imagine you have a set of resource bundles in your c:\projectX\resources\text folder. Each one is named "language_en_US.properties", where the only part of the name that changes is the locale version. In other words, the French version would be "language_fr_FR.properties." The getResourceBundle method allows us to simply pass in "c:\projectX\resources\text\language.properties" as the file name. When it discovers that the file does not exist, it will automatically try to load "c:\projectX\resource\text\language_en_US.properties". This is because the rbLocale argument defaults to en_US; to load the French version you would simply pass in fr_FR.
Once the method finds the file (it will throw an error if it cannot), we then use a set of Java objects we created in the constructor area of the CFC:
variables.rB = createObject("java", "java.util.PropertyResourceBundle");
variables.fis = CreateObject("java", "java.io.FileInputStream");
These objects then enable getResourceBundle method to load the properties file:
and then initialize the PropertyResourceBundle object:
Once we have done that, we can use another method, getKeys(), to get the keys of the resource bundle. We then simply loop over our keys and create a structure of the values. Once we have the structure we return it.
So, let's take a quick look at how this could be used on a sample page. Listing 2 contains a simple form, in this case, one with just a submit and cancel button. However, the labels for these buttons need to be localized. To get the correct strings, we use <cfinvoke> to get the resource bundle from a property file, and to specifically grab the fr_FR version. We can then use this returned structure to populate the values of our buttons. As you can see from the code, switching to English would be a trivial change.
Let's take a look at another method in the CFC, loadResourceBundle. This is an extremely short method. All it does is take a file and an optional locale. It calls the getResourceBundle method we described earlier, and then stores it in the CFC's local variable scope. This allows us to create an instance of the CFC and have it parse the resource bundle once.
Finally, the method getResource allows us to grab one particular string from the structure. Why would you use this when you could getResourceBundle? This method will slightly modify the result when debugMode is detected. Instead of returning just the string, it will actually return the string wrapped in * characters. Visually, this makes it easy to see which parts of your page are localized and which are not. This method will also "swallow" any errors that involve missing resources, so if you try to retrieve a resource for the French locale that doesn't exist, you will get a blank string. (This may or may not be preferable. What's nice is that you can modify the CFC to handle it differently. You may want it to retrieve the en_US version if one doesn't exist for the fr_FR version. You could also have it optionally log a warning, or e-mail a localization team.)
One more example before we end. In the code in Listing 2, we had a hard-coded locale. Normally you probably want your user to select a locale. You may think it's a good idea to store the resource bundle in the user's session scope. However, this means that every user in locale N will have a copy of the bundle, which isn't efficient. Listing 3 shows a modified version of Listing 2. This time we allow the user to select a locale. We store the user's preference, but keep the bundles stored in the application scope.
The author wishes to give special thanks to Paul Hastings, who he says is, without a doubt, the localization/internationalization master, and was a big help in writing this article. His blog can be found at http://cfg11n.blogspot.com. (Watch for a series by Paul in future issues of CFDJ.)
SYS-CON Events announced today that Interoute, owner-operator of one of Europe's largest networks and a global cloud services platform, has been named “Bronze Sponsor” of SYS-CON's 18th Cloud Expo, which will take place on June 7-9, 2015 at the Javits Center in New York, New York. Interoute is the owner-operator of one of Europe's largest networks and a global cloud services platform which encompasses 12 data centers, 14 virtual data centers and 31 colocation centers, with connections to 195 ad...
Feb. 8, 2016 12:45 PM EST Reads: 351
As enterprises work to take advantage of Big Data technologies, they frequently become distracted by product-level decisions. In most new Big Data builds this approach is completely counter-productive: it presupposes tools that may not be a fit for development teams, forces IT to take on the burden of evaluating and maintaining unfamiliar technology, and represents a major up-front expense. In his session at @BigDataExpo at @ThingsExpo, Andrew Warfield, CTO and Co-Founder of Coho Data, will dis...
Feb. 8, 2016 12:30 PM EST Reads: 130
SYS-CON Events announced today that Commvault, a global leader in enterprise data protection and information management, has been named “Bronze Sponsor” of SYS-CON's 18th International Cloud Expo, which will take place on June 7–9, 2016, at the Javits Center in New York City, NY, and the 19th International Cloud Expo, which will take place on November 1–3, 2016, at the Santa Clara Convention Center in Santa Clara, CA. Commvault is a leading provider of data protection and information management...
Feb. 8, 2016 10:45 AM EST Reads: 377
The cloud promises new levels of agility and cost-savings for Big Data, data warehousing and analytics. But it’s challenging to understand all the options – from IaaS and PaaS to newer services like HaaS (Hadoop as a Service) and BDaaS (Big Data as a Service). In her session at @BigDataExpo at @ThingsExpo, Hannah Smalltree, a director at Cazena, will provide an educational overview of emerging “as-a-service” options for Big Data in the cloud. This is critical background for IT and data profes...
Feb. 8, 2016 09:30 AM EST Reads: 148
SYS-CON Events announced today that VAI, a leading ERP software provider, will exhibit at SYS-CON's 18th International Cloud Expo®, which will take place on June 7-9, 2016, at the Javits Center in New York City, NY. VAI (Vormittag Associates, Inc.) is a leading independent mid-market ERP software developer renowned for its flexible solutions and ability to automate critical business functions for the distribution, manufacturing, specialty retail and service sectors. An IBM Premier Business Part...
Feb. 7, 2016 02:00 PM EST Reads: 565
SYS-CON Events announced today that Alert Logic, Inc., the leading provider of Security-as-a-Service solutions for the cloud, will exhibit at SYS-CON's 18th International Cloud Expo®, which will take place on June 7-9, 2016, at the Javits Center in New York City, NY. Alert Logic, Inc., provides Security-as-a-Service for on-premises, cloud, and hybrid infrastructures, delivering deep security insight and continuous protection for customers at a lower cost than traditional security solutions. Ful...
Feb. 7, 2016 01:45 PM EST Reads: 368
Fortunately, meaningful and tangible business cases for IoT are plentiful in a broad array of industries and vertical markets. These range from simple warranty cost reduction for capital intensive assets, to minimizing downtime for vital business tools, to creating feedback loops improving product design, to improving and enhancing enterprise customer experiences. All of these business cases, which will be briefly explored in this session, hinge on cost effectively extracting relevant data from ...
Feb. 7, 2016 01:30 PM EST
With the Apple Watch making its way onto wrists all over the world, it’s only a matter of time before it becomes a staple in the workplace. In fact, Forrester reported that 68 percent of technology and business decision-makers characterize wearables as a top priority for 2015. Recognizing their business value early on, FinancialForce.com was the first to bring ERP to wearables, helping streamline communication across front and back office functions. In his session at @ThingsExpo, Kevin Roberts...
Feb. 7, 2016 12:00 PM EST Reads: 349
With an estimated 50 billion devices connected to the Internet by 2020, several industries will begin to expand their capabilities for retaining end point data at the edge to better utilize the range of data types and sheer volume of M2M data generated by the Internet of Things. In his session at @ThingsExpo, Don DeLoach, CEO and President of Infobright, will discuss the infrastructures businesses will need to implement to handle this explosion of data by providing specific use cases for filte...
Feb. 7, 2016 10:15 AM EST Reads: 122
SYS-CON Events announced today that Fusion, a leading provider of cloud services, will exhibit at SYS-CON's 18th International Cloud Expo®, which will take place on June 7-9, 2016, at the Javits Center in New York City, NY. Fusion, a leading provider of integrated cloud solutions to small, medium and large businesses, is the industry's single source for the cloud. Fusion's advanced, proprietary cloud service platform enables the integration of leading edge solutions in the cloud, including clou...
Feb. 6, 2016 03:30 PM EST Reads: 729
Most people haven’t heard the word, “gamification,” even though they probably, and perhaps unwittingly, participate in it every day. Gamification is “the process of adding games or game-like elements to something (as a task) so as to encourage participation.” Further, gamification is about bringing game mechanics – rules, constructs, processes, and methods – into the real world in an effort to engage people. In his session at @ThingsExpo, Robert Endo, owner and engagement manager of Intrepid D...
Feb. 5, 2016 09:00 PM EST Reads: 790
Eighty percent of a data scientist’s time is spent gathering and cleaning up data, and 80% of all data is unstructured and almost never analyzed. Cognitive computing, in combination with Big Data, is changing the equation by creating data reservoirs and using natural language processing to enable analysis of unstructured data sources. This is impacting every aspect of the analytics profession from how data is mined (and by whom) to how it is delivered. This is not some futuristic vision: it's ha...
Feb. 2, 2016 02:00 PM EST Reads: 416
WebRTC has had a real tough three or four years, and so have those working with it. Only a few short years ago, the development world were excited about WebRTC and proclaiming how awesome it was. You might have played with the technology a couple of years ago, only to find the extra infrastructure requirements were painful to implement and poorly documented. This probably left a bitter taste in your mouth, especially when things went wrong.
Feb. 2, 2016 04:30 AM EST Reads: 856
Learn how IoT, cloud, social networks and last but not least, humans, can be integrated into a seamless integration of cooperative organisms both cybernetic and biological. This has been enabled by recent advances in IoT device capabilities, messaging frameworks, presence and collaboration services, where devices can share information and make independent and human assisted decisions based upon social status from other entities. In his session at @ThingsExpo, Michael Heydt, founder of Seamless...
Feb. 1, 2016 05:00 AM EST Reads: 946
The IoT's basic concept of collecting data from as many sources possible to drive better decision making, create process innovation and realize additional revenue has been in use at large enterprises with deep pockets for decades. So what has changed? In his session at @ThingsExpo, Prasanna Sivaramakrishnan, Solutions Architect at Red Hat, discussed the impact commodity hardware, ubiquitous connectivity, and innovations in open source software are having on the connected universe of people, thi...
Jan. 31, 2016 09:00 PM EST Reads: 731
WebRTC: together these advances have created a perfect storm of technologies that are disrupting and transforming classic communications models and ecosystems. In his session at WebRTC Summit, Cary Bran, VP of Innovation and New Ventures at Plantronics and PLT Labs, provided an overview of this technological shift, including associated business and consumer communications impacts, and opportunities it may enable, complement or entirely transform.
Jan. 31, 2016 07:15 PM EST Reads: 1,153
There are so many tools and techniques for data analytics that even for a data scientist the choices, possible systems, and even the types of data can be daunting. In his session at @ThingsExpo, Chris Harrold, Global CTO for Big Data Solutions for EMC Corporation, showed how to perform a simple, but meaningful analysis of social sentiment data using freely available tools that take only minutes to download and install. Participants received the download information, scripts, and complete end-t...
Jan. 31, 2016 10:00 AM EST Reads: 1,224
For manufacturers, the Internet of Things (IoT) represents a jumping-off point for innovation, jobs, and revenue creation. But to adequately seize the opportunity, manufacturers must design devices that are interconnected, can continually sense their environment and process huge amounts of data. As a first step, manufacturers must embrace a new product development ecosystem in order to support these products.
Jan. 31, 2016 10:00 AM EST Reads: 816
Manufacturing connected IoT versions of traditional products requires more than multiple deep technology skills. It also requires a shift in mindset, to realize that connected, sensor-enabled “things” act more like services than what we usually think of as products. In his session at @ThingsExpo, David Friedman, CEO and co-founder of Ayla Networks, discussed how when sensors start generating detailed real-world data about products and how they’re being used, smart manufacturers can use the dat...
Jan. 30, 2016 07:45 PM EST Reads: 793
When it comes to IoT in the enterprise, namely the commercial building and hospitality markets, a benefit not getting the attention it deserves is energy efficiency, and IoT’s direct impact on a cleaner, greener environment when installed in smart buildings. Until now clean technology was offered piecemeal and led with point solutions that require significant systems integration to orchestrate and deploy. There didn't exist a 'top down' approach that can manage and monitor the way a Smart Buildi...
Jan. 30, 2016 03:45 PM EST Reads: 1,274