User's Guide

Fixing Source Code

Overview

Besides scanning your code to detect internationalization issues, Globalyzer provides several tools and features that you can use to fix the problems.

Types of Issues

The kinds of internationalization issues that Globalyzer can detect include:

Embedded Strings -- Think of embedded strings as displayable text, or text that end users will see when they use your application, such as error messages, button names, or tool tips. When the scanner detects embedded strings in your source, they are added to the Scan Results table and displayed when you select Embedded Strings from the Results dropdown.

Locale-Sensitive Methods -- These are methods or functions that could pose an internationalization problem. For example, methods that produce date or currency strings could be an issue if they do not support the current locale's preference. Methods that sort text pose another kind of problem since text may sort differently for different languages. Click here for more information on locale-sensitive methods. Click here for information on using the Quick Summary Report to link to detailed information on your detected locale-sensitive methods.

Static File References -- The problem with image and other static file references is similar to that of embedded text, in that the data may need to change for each locale. For example, images may contain translatable text; a style sheet file may have font settings that would need to be adjusted based on locale. In these cases, the static file would need to be duplicated, localized, stored in locale-sensitive directories and then retrieved dynamically based on locale. Globalyzer helps you to identify and locate source code references to these locale-sensitive static files.

General Patterns -- Globalyzer lets you define custom string patterns that the scanner will look for. For instance, when scanning HTML code, you might define a general pattern to detect instances of "value=".

Fixing Issues

After you perform a scan, the issues that were detected appear in the Scan Results table. Typically, you sort and filter the table before fixing source code. Click here for more information on using the Scan Results table.

Double-click on a line in the Scan Results table to open the corresponding source file and highlight the Issue. Depending on the type of issue, you have several possible ways to fix it.

  • Fixing Embedded String Issues -- If the issue is an embedded string, the simplest way to fix it is to externalize it. The techniques for externalizing code are described in the following sections.
  • Fixing Locale-Sensitive Methods, Static Files References, and General Patterns -- To fix any of these issues, you must directly edit the source file using Globalyzer's Editor view.
Ignoring Issues and Lines

Sometimes there are active issues reported that you know aren't really issues, and it may be impossible to modify the rule set to filter them. As described earlier, you can select these issues from Scan Results and change their status. Changing the status in this way is only temporary, however. If the rule set or source file is modified, those status settings may be lost. But if you update these issues to Dashboard as False Positive, you could reload Dashboard False Positives to any Globalyzer Workbench later. Another permanent way to ignore these issues is to insert comments telling Globalyzer to ignore these issues. As described earlier, this can be done one line at a time from the source file by right clicking and selecting an ignore comment to insert via Fix Code.

A more robust way to insert ignore comments is via Scan Results. You can select one or multiple issues in Scan Results, right click, and select Fix Code=>Ignore This Issue or Fix Code=>Ignore This Line. Globalyzer will then add a special comment at the end of each code line, telling it to ignore this issue or to ignore all the issues found on this line.

In the screen capture below, the user wants to ignore the embedded string "US" in the file ExampleMain.java:

Ignore This Line is available for all Scan Types: Embedded Strings, Locale-Sensitive Methods, Static File References, and General Patterns. Ignore This Issue is only available for Embedded Strings. If a source line contains two embedded string issues and one locale-sensitive method issue, choosing to ignore one string issue will leave the other string, as well as the locale-sensitive method, active. If instead the line is ignored, then both strings will be removed from the Embedded Strings Scan Results and the method will be removed from the Locale-Sensitive Method Scan Results.

Updating Issues to Dashboard as False Positives

Update Issues to Dashboard as False Positives make it possible for Globalyzer to synchronize false positives across the suite. It is available for all Scan Types: Embedded Strings, Locale-Sensitive Methods, Static File References, and General Patterns. Right click on selected issues and select Update to Dashboard as False Positive from dropdown menu, those issues status will be changed to dfp if update successful. And you could reload Dashboard False Positives to any Globalyzer Workbench later. Right-click on the scan results or click Project menu and select Reload Dashboard False Positives from the dropdown menu to reload the dfp status for the currently selected project. See the confirm information in Console.

Using the All Predicted Scan Summary

After you scan code, a summary containing information about the scan is generated. This summary is displayed in the Predicted Active Scan Summary view in the lower left of the Globalyzer main window.

The summary displayed in the All Predicted Scan Summary view contains the following categories of information:

  • a summary of the number of lines and files scanned
  • static file references that were detected
  • pattern matches that were detected
  • locale-sensitive methods that were detected
  • embedded strings that were detected

Tip: The Locale-Sensitive Method section contains links that allow you to view detailed information about the method. This information can be useful in assisting you to re-code or replace the methods.

Configuring Globalyzer to Fix Source Code

Before you use Globalyzer to fix source code, you need to configure the scans.

Select Scan > Manage Scans ... from the menu bar.

Select the scan you want to configure and click Modify.... Click Next until you reach Globalyzer Scan I18n Settings. This is where you can configure the settings in the String Externalization Resource Files sections.

Editing Source Code Directly

When you double-click an issue in the Scan Results table, the source file containing the issue is automatically opened and the issue is highlighted.

You edit text in the Editor view. The editor provides powerful editing functions. You may want to add plugins depending on your software language and your application.

To save a source file, use the Save or Save As menu functions or buttons in the toolbar.

Externalizing an Embedded String

Externalizing refers to the process of removing embedded strings from source code and placing them into a resource file.

Globalyzer's String Externalization feature lets you automatically replace embedded text (such as hardcoded button and menu names) with a method call to retrieve the text from a resource file. This feature automates the tedious process of typing a retrieval method, assigning a key, and editing a resource file for each instance of display text.

When you externalize a string, the string is automatically removed from the source file, assigned a unique key, placed in a resource file, and replaced in the source file by a method designed to recall the string from the resource file.

Managing the Resource Files

The resource files are placed, by default, in the same directory you set as your Base Directory when you created your Project. You can change this in the Globalyzer Scan I18n Settings page of your Manage Scans wizard.

In the Globalyzer Scan I18n Settings pane, you can also specify several settings that affect how resource files are handled. Some of the key ones are:

Resource File Type -- You can set the type to the kind of format you need. There are several options, depending on the Scan's associated programming language. Two common resource file types are .properties and .resx. The first tells Globalyzer to create a Java style resource file, and the second, a .NET resource file.

Resource File Frequency -- If set to Project, one resource file is created for the entire project. If set to Source File, a new, uniquely named resource file is created for each source file that contains externalized text.

Resource File Name -- Lets you set the name of a Project resource file.

Some Resource File types can group externalized strings by file name, some Resource File types are able to append externalized strings to an existing resource file. The following table lists the Resource File types that can group and/or append externalized strings:

.js .po .properties .rc   Can group by source file and append
.msg .resx .xml   Can only append

In the case where both grouping and appending are possible, select Windows=>Preferences=>Globalyzer to control how you want strings added to the resource file. For more information on Preferences, click here.

Simple Example

In Java, the line:

JButton myButton = new JButton("Cancel");

in a file named Catalog.java will look something like this (depending on how Globalyzer is configured) after the string "Cancel" is externalized:

JButton myButton = new JButton(myResource.getString("CATA_CANCEL_1");

Notice that the text has been replaced by a method call that takes a string keyword. In the Java properties file, the following line is added:

CATA_CANCEL_1=Cancel

Notice that the name/value pair consists of the automatically generated keyword and the actual display text. Translators will then create locale-specific properties files and translate the English display text.

To externalize a string, make sure the string is highlighted, then do one of the following:

Click the Externalize Selected String button in the Toolbar.

In the Scan Results view, select the issue(s), right-click and choose Fix Code=>Externalize String from the menu.

In the Scan Results view, select the issue(s), and select Fix Code=>Externalize Selected Scan Results from the menu bar.

Note: After you externalize a string, the source file is automatically saved and the issue's status updated.

Externalizing Concatenated Strings

Globalyzer now detects when strings are part of a string concatenation. Concatenated strings need to be refactored before being externalized.

As Globalyzer scans your source code, it sets a detected string's Scan Results priority to the new string concatenation priority 'C' when at least one of the following conditions are true:

  • string literal starts or ends with white space
  • string literal is preceded or followed by (language specific) concatenation characters
  • string literal is a parameter to a Locale-Sensitive Method configured to have priority 0 (the string concatenation priority)

For example, given the following code:

String ua = user + " is " + age + " years old.";

Globalyzer will find two strings: " is " and " years old." and assign them both priority 'C'.

You cannot simply externalize the two strings independently to a resource file and have the file sent out for translation. The translator will end up translating the two strings out of context. The word order may be different by language and the grammar may be different, too. For instance, in French, a person has four years. Without the context or the full sentence, a translator could not guess how "is" will be used.

Most programming languages support some kind of parameter substitution. For example, in Java the above code could be refactored using MessageFormat.format:

String ua = MessageFormat.format("{0} is {1} years old.", user, age);

After refactoring, rescan the file. The new string, "{0} is {1} years old." is detected and can now be externalized. The translator will know the proper translation given the full context of the string.

Note: Once externalized, the entry will be removed from the Active Scan Results table.

Externalizing Multiple Strings

This feature greatly speeds up the string externalization process. To externalize multiple strings, select multiple rows containing Embedded Strings in the Scan Results table, then do one of the following:

Click the Externalize Selected Scan Results button in the Toolbar.

Right-click over the Scan Results table and pick Fix Code=>Externalize Selected Scan Results.

Select Fix Code=>Externalize Selected Scan Results from the main menu.

Batch externalization can also be done from the Project Explorer view. Select files and/or directories from the Project Explorer, right click, and select Externalize Strings from ... . All active embedded strings from the selected files/directories that are displayed on the current page of Scan Results will be externalized.


Warning: When you externalize strings in Single or Batch mode, the affected source files are automatically saved. Be sure to check that the string is a valid string that the user will see before you externalize. You can do this by double-clicking on the string in the Scan Results table to open the corresponding source code file. In addition to ensuring that the string is fully-selected in the source file, you can use the context of the code to verify that it is a string that will be displayed to an end user, and therefore requires string externalization.

Note: If a string is several lines long, Globalyzer will display an error and will not include the string in the batch externalization process. To externalize these long strings, use Single String Externalization, but be sure to check that the entire string is selected in the source file before clicking the Externalize button.

Inserting Externalized String Comments

The Externalized String Comment is a text string that can be inserted into your source code when you externalize a string. The string you externalized is appended to the end of this comment.

For example: // GLOBALYZER_EXTERNALIZED_TEXT: Cancel

To configure Globalyzer to insert an Externalized String Comment into a source file when a string is externalized, check Automatically Insert Externalized String Comment in the Windows > Preferences > Globalyzer dialog.

The Externalized String Comment will be inserted right above the current code line.

You can change the default text associated with the Externalized String Comment. Click the Scan > Manage Scans ... menu item. In the wizard, go to the Globalyzer Scan I18n Settings page to find the Externalized String Comment.

 

 User's Guide Contents