Localization in Visual COBOL
Several examples of how to utilize COBOL in a Microsoft .NET environment have been circulating for a number of years. Some of these examples are quite dated and due to Framework enhancements, (2.0, 3.0, 4.0, 4.5 and now 4.6) the use of the namespaces as well as the syntax used to access them has changed significantly. Micro Focus recently released Visual COBOL 2.3, our flag-ship product for Visual Studio 2015. With a number of changes that have occurred in both .NET and Visual COBOL an update of all our previous articles is in order. So over the next couple of months we will be updating all of our previous articles to the latest versions and introducing new articles with new content. We hope you find these articles educational and valuable to you and a benefit to your organization.
Overview Do you create applications for use in multiple locations and languages? This article will show you how to use the power of LOCALIZATION within Visual Studio .NET to create one screen and have it display different languages. The source code was created with Microsoft Visual Studio.NET 2015 and Micro Focus Visual COBOL 2.3 HF1.
With many companies being multi-national, developers are having to create applications not only in their native language, but also in languages for the countries in which their company has offices. Many times these additional languages present unique challenges due to the presence of local variants to the base language. In the past developers created the application in their language and copied the application to a new name and changed the presentation to the language where the application was going to be deployed. This resulted in having the same program exist for different languages, same core logic, but different presentation layer.
Through the use LOCALIZATION in Visual Studio.NET you can now create one code-base and have multiple presentation layers in different languages use the same code-base. Our example will use a simple WinForm that will enable you to view a screen in either English or German. Please remember though, the intent of the article is to show you how to enable multiple languages, and not present you with a completed application.
Our application has two screens. An Introductory screen:
and a presentation screen:
It is the presentation screen we will enhance to display in German. We will not go into the specifics how to create a WinForm application using Visual COBOL. Please refer to the online documentation for assistance.
Microsoft has enabled multi-language capabilities within Visual Studio.NET via a combination of the LOCALIZABLE and LANGUAGE settings for the Form you are working on. By default, your language setting is set to the local area where you are. By changing these variables, you can create language targeted screens.
Within Visual Studio.NET 2015 there are two ways to create a foreign language representation of your screens. One way is to use the utility "WINRES" and the other is to use the Designer. I chose to use the Designer so I can visually see what the changes will be to my screen and if any additional enhancements to the form will be required by changing the text being displayed. You begin by selecting your Form and reviewing the properties, looking under the “Design” area for the LOCALIZABLE and LANGUAGE settings as such:
Notice the LANGUAGE and LOCALIZABLE settings are set to English(United States) and True respectively. This isn’t the default behavior. When you create a new form the settings are ‘Default’ and ‘False’ respectively. Make sure to set these two properties to enable localization but also to set your ‘home’ language. I want to create a form in German so we need to change the LANGUAGE to Germany (Germany) by using the drop-down list. Notice the different versions of German available to you? You can target the specific dialect of a language by first reviewing the options available to you and then selecting the closest version of that language.
For further information on what language settings are available see the CultureInfo Class in the .NET Framework help.
When you select a new language you will notice the creation of several new files in the Solution Explorer. These files are XML based project files that will contain the language specific items for the screen. The following image shows the files:
If you were to look at these files in VS.NET you will see they appear as spreadsheets with the name, value and other pieces of information necessary for VS.NET to display the new values. Take a moment and review the files in the solution provided. You will see the values for the labels already present.
The ’Demo.resx’ file is the original file with all the controls and some properties associated with each of the controls. If you look towards the bottom of the file you will see the Labels we created along with the ‘Text’ property for each label and its current value.
In order to present a screen in German we need to update the Text property for all our labels in this file. The simplest way to do this is to ‘split screen’ your work area and have the existing ‘Demo.resx’ file on one side, and the ‘Demo-de-DE.resx’ on the other. Then it’s a simple matter of either copying/pasting each cell from one side to the other, or re-keying the information with the new values for German for the ‘Value’.
The Driver Program
Since I do not want to change the code page my computer is using I created the driver program to demonstrate the use of the multi-language capabilities. The driver program, FORM1.COB, has two methods (one for each click event of the command buttons) that set a variable, create a new Globalization instantiation and finally show the form using the new Globalization instance.
LangDemo was established in the Class level Working-Storeage Section as a reference to the DEMO form. We also defined a flag (ws-lang-ind) to tell us what language we are going to be displaying.
We instantiate the form, passing it a flag telling it which language we want to use and then show the form. The real secret is in the "NEW" method of the DEMO class.
An overloaded "NEW" method was created in the DEMO program.
The original "NEW" method is shown for comparison. Two variables were added, an object reference to the CULTUREINFO class (objGlobalization) and a LINKAGE parameter to accept the value being passed from the driver program (lang-ind).
The language indicator being passed in is evaluated. The values used for the instantiation of the CultureInfo class were determined from the documentation and from the value created for the RESX files. The properties for the CURRENTCULTURE and CURRENTUICULTURE must be set to the new culture info we established prior to invoking the InitializeComponent method. When the form is displayed, depending on which language was selected will determine which form is displayed. Windows will now display the proper information for the correct language.
With the release of Visual COBOL 2.3 the syntax utilized within Visual Studio and relating to .NET has been updated to reflect more of the way C# developers are used to seeing method and parameter definitions. The following example shows the same ‘NEW’ method updated to reflect the changes in Visual COBOL 2.3:
If you’ll notice, the Linkage Section has been removed along with the variable declaration for the language indicator, lang-ind. Instead the language indicator has been defined in the method header and is defined using the standard .NET data type of character. The overall appearance now is more in-line with what a C# developer would be used to seeing. Both examples of the ‘NEW’ method are included in the sample provided. Take some time and review the versions and see which way works for you.
That's it! Once you see how it is done the process is so simple, yet very, very efficient and effective! Unzip the source file, rebuild the solution and execute the process without debugging the first time so you see what is displayed. Next step into the code and visually see what is occurring. Finally, and this should put everything into perspective for you, select the English language in the LANGUAGE property, then select a new language, say Italian. Notice the files that are created and then modify the screen in the Designer to say 'Italian' instead of 'German'. Then add a button to the driver program to call your new Italian screen. Finally update the overloaded "NEW" method to create an Italian instantiation of the CultureInfo class, recompile the solution and see what happens.
Maintenance Note: Once you have created the new screens you can provide a translator or someone familiar with the local language, the spreadsheet representation of your screen and they can 'tweak and tune' the language to be more accurate. You can then maintain the specific language items in the spreadsheet.
I hope you find this article interesting and useful! Happy Coding!